flask中发送邮件

from flask import Flask
from flask_mail import Mail, Message
from flask import current_app, render_template
app = Flask(__name__)

app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'zzzz@qq.com'
app.config['MAIL_PASSWORD'] = 'xxxx'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)

with app.app_context():
msg = Message('Hello', sender=app.config['MAIL_USERNAME'],
recipients=['kongzzzz@126.com'])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)

@app.route("/")
def index():
msg = Message('Hello', sender=app.config['MAIL_USERNAME'],
recipients=['kongyzzzz@126.com'])
msg.body = "Hello Flask message sent from Flask-Mail"
msg.html = "This is <b>HTML</b>"
mail.send(msg)
return "Sent"


if __name__ == '__main__':
app.run(debug=True)

posted on 2022-11-01 20:22  孔扎根  阅读(90)  评论(0编辑  收藏  举报

导航