I am attempting to send an email with Flask-Mail with a .txt file attachment. Thus far either I get an error or the email sends but the .txt. file is blank I've tried it two ways:
One way following the documentation:
with current_app.open_resource("sample.txt") as fp:
msg.attach("sample.txt","text/plain", fp.read())
This leads to an Error:
TypeError: 'exceptions.IOError' object is not callable
I also tried it without the open_resource method:
msg.attach("sample.txt","text/plain")
mail.send(msg)
This led to the email sending but the .txt. attachment was blank.
Full try/except block below
try:
msg = Message("New File",
sender="SENDER",
recipients=["RECIPIENT"])
msg.body = "Hello Flask message sent from Flask-Mail"
with current_app.open_resource("sample.txt") as fp:
msg.attach("sample.txt","text/plain", fp.read())
mail.send(msg)
except Exception as e:
return e
return "file was successfully sent"
What am I missing in order for the attachment to be properly sent?