安装 Flask-Mail : pip install Flask-Mail
demo.py(发送邮件):
from flask import Flask
from flask_mail import Mail, Message # 导入
app = Flask(__name__)
# 配置邮件:服务器/端口/传输层安全协议/邮箱名/密码(授权码)
app.config.update(
DEBUG = True,
MAIL_SERVER='smtp.qq.com',
MAIL_PROT=465,
MAIL_USE_TLS = True, # 加密协议
MAIL_USERNAME = '发送者@qq.com',
MAIL_PASSWORD = 'goyubxohbtzfbidd', # 授权码
)
# 实例化mail对象
mail = Mail(app)
@app.route('/')
def index():
# sender:发送方。 recipients:接收方列表
msg = Message("主题", sender='发送者@qq.com', recipients=['接收者[email protected]','接收者[email protected]'])
# 邮件内容
msg.body = "邮件主体内容"
# 发送邮件
mail.send(msg)
return "Sent Succeed"
if __name__ == "__main__":
app.run()