我正在尝试理解本教程代码。
from flask import Flask
from flask_mail import Mail, Message
app =Flask(__name__)
mail=Mail(app) # <-- This
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'yourId@gmail.com'
app.config['MAIL_PASSWORD'] = '*****'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app) # <-- This
@app.route("/")
def index():
msg = Message('Hello', sender = 'yourId@gmail.com', recipients = ['id1@gmail.com'])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)
return "Sent"
if __name__ == '__main__':
app.run(debug = True)
在第 5 行和第 13 行,Mail 对象被实例化并分配给 mail。即使我在第 5 行注释掉第一个实例,我仍然可以发送电子邮件,所以我可以说这只是一个错字,还是有必要?