Sending attachment with Flask-Mail

4

Configuring flask and testing was quick and easy but sending attachment is very difficult if not impossible. Does anyone have a tip or example to send attachments with Flask-Mail?

My code looks like this:

mail = Mail(app)
MAIL_CONFIG = app.config

msg = Message(
    assunto, 
    sender = (MAIL_CONFIG['MAIL_NAME'], MAIL_CONFIG['MAIL_USERNAME']),
    reply_to = MAIL_CONFIG['MAIL_NAME'] + '<' + MAIL_CONFIG['MAIL_USERNAME'] + '>', 
    recipients = destinatarios
    )

msg.body = texto
msg.html = mensagem

# arquivo = 'downloads/bbbb.txt'

# with app.open_resource(arquivo) as fp:
#   msg.attach(arquivo, "text/plain", fp.read())

mail.send(msg)

If I uncomment the commented lines I get the following error:

  

TypeError: normalize () argument 2 must be unicode, not str

    
asked by anonymous 23.06.2016 / 16:06

1 answer

0

Make sure you have set MAIL_ASCII_ATTACHMENTS = True . Doing this forces you to use the unicode type in the filename: arquivo = u'downloads/bbbb.txt' (note the u before the string)

Or you can stop using MAIL_ASCII_ATTACHMENTS = True (the default is False ) and avoid files with unicode characters in the name.

    
06.07.2016 / 20:38