.txt ファイルが添付された Flask-Mail でメールを送信しようとしています。これまでのところ、エラーが発生するか、メールが送信されますが.txt. ファイルは空白です。2 つの方法で試しました。
ドキュメントに従う1つの方法:
with current_app.open_resource("sample.txt") as fp:
msg.attach("sample.txt","text/plain", fp.read())
これにより、エラーが発生します。
TypeError: 'exceptions.IOError' object is not callable
また、open_resource メソッドなしで試してみました。
msg.attach("sample.txt","text/plain")
mail.send(msg)
これにより、電子メールが送信されましたが、.txt が送信されました。添付ファイルは空白でした。
以下の完全な try/except ブロック
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"
添付ファイルを適切に送信するために不足しているものは何ですか?