Setting up the flask and testing it was quick and easy but sending attachments has been 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
Check if you happen to set
MAIL_ASCII_ATTACHMENTS = True . Doing this forces you to use the unicode type in the filename:
arquivo = u'downloads/bbbb.txt' (notice the
u before the string)
Or you can stop using
MAIL_ASCII_ATTACHMENTS = True (default is
False ) and avoid files with unicode characters in the name.