这是用于发送带附件的电子邮件的代码(sample.txt - test file),
但我没有收到任何邮件,也没有任何错误被抛出
我尝试发送没有附件的邮件,并且工作......
附件部分我做错了什么?
def sendmail():
try:
#...
msg.body = "Hello Flask message sent from Flask-Mail"
with app.open_resource("sample.txt") as fp:
msg.attach("sample.txt", "text/plain", fp.read())
mail.send(msg)
except Exception as e:
raise e
return "Check Your Inbox !!!"
答案 0 :(得分:0)
以下代码适用于我。我在示例中使用了gmail的SMTP服务器。 首先运行此程序
from flask import Flask
from flask_mail import Mail
from flask_mail import Message
# Configure these
sender = 'fred@example.com'
recip = 'fred@example.com'
image = 'example.png'
mailpass = 'aabbcc'
app = Flask(__name__)
mail = Mail(app)
app.extensions['mail'].server = 'smtp.gmail.com'
app.extensions['mail'].port = 587
app.extensions['mail'].use_tls = True
app.extensions['mail'].username = sender
app.extensions['mail'].password = mailpass
@app.route('/')
def sendmail():
msg = Message('This is a test email',
sender=sender,
recipients=[recip])
with app.open_resource(image) as f:
msg.attach('image.png', 'image/png', f.read())
mail.send(msg)
return 'mail sent'
app.run('localhost', 8888)
然后触发REST调用:
curl localhost:8888/
我没有从flask运行程序只是因为我在PyCharm中调试它。通过以上所有操作,图片将显示在我的Gmail视图中。