首先直接贴上代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'xxxx@qq.com'
app.config['MAIL_PASSWORD'] = '授权码'
mail = Mail(app)
msg = Message('test subject', sender='xxxx@qq.com',recipients=['xxxx@xxx.com'])
msg.body = u'我的第一个FLASK邮件测试'
with app.app_context():
mail.send(msg)
1.安装 Flask-Mail
(venv) $ pip install flask-mail
2.Flask-Mail SMTP服务器的配置
各项配置参照下图
QQ邮箱授权码获取
登录QQ邮箱,进入设置页面
切换到账户选项
开启PO3/IMAP服务
通过发送短信认证就可以拿到授权码啦 - -
运行脚本
把授权码填上,运行脚本就可以把邮件发送出去啦!!!!
就是这么简单!!!!!!!!!!!!!