Sending attachment with Flask-Mail

Asked

Viewed 179 times

3

Setting up flask and testing was quick and easy but sending attachments is very difficult if not impossible. Someone has a tip or example for sending attachments with Flask-Mail?

My code is 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 blur the commented lines I have the following error:

Typeerror: normalize() argument 2 must be Unicode, not str

  • What you’ve tried so far?

1 answer

0

Check if you by chance set MAIL_ASCII_ATTACHMENTS = True. By doing so, you force yourself to use the type Unicode in the file name: 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.