Flask Vue.js全栈开发|第14章:邮件支持
Synopsis: 使用 Flask-Mail 给用户发送邮件,用户注册时需要先通过邮件确认账户,不然不让他访问前端任何路由(防止用户注册时提供虚假邮箱地址)。同时,如果用户忘记了自己的账户密码,也可以通过邮件重置密码。注意,邮件中的链接会包含 JWT,确保用户只能确认自己的账户或重置自己的账户密码
代码已上传到 https://github.com/wangy8961/flask-vuejs-madblog/tree/v0.14 ,欢迎star
1. Flask-Mail
安装 Flask-Mail
插件:
(venv) D:\python-code\flask-vuejs-madblog\back-end>pip install flask-mail (venv) D:\python-code\flask-vuejs-madblog\back-end>pip freeze > requirements.txt
导入插件,修改 back-end/app/extensions.py
:
初始化插件,修改 back-end/app/__init__.py
:
from app.extensions import mail ... def configure_extensions(app): '''Configures the extensions.''' ... # Init Flask-Mail mail.init_app(app)
指定相关配置项,修改 back-end/config.py
:
class Config(object): ... # 邮件配置 MAIL_SERVER = os.environ.get('MAIL_SERVER') MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25) MAIL_USE_SSL = os.environ.get('MAIL_USE_SSL', 'false').lower() in ['true', 'on', '1'] MAIL_USERNAME = os.environ.get('MAIL_USERNAME') MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') MAIL_SENDER = os.environ.get('MAIL_SENDER') ...
详细使用方法请参考 Flask-Mail
文档
1.1 Python SMTP Server: smtpd
要测试发送邮件,只需要让 Flask-Mail
连接到 简单邮件传输协议(Simple Mail Transfer Protocol, SMTP)服务器
,并把邮件交给这个服务器发送即可
Python提供了 smtpd
模块来启动一个模拟的电子邮件服务器,打开一个新终端,输入如下命令:
此时,你本机就启动了一个邮件服务器,并运行在 5001
端口上。然后,在另一个终端设置相关环境变量:
(venv) D:\python-code\flask-vuejs-madblog\back-end>set MAIL_SERVER=localhost (venv) D:\python-code\flask-vuejs-madblog\back-end>set MAIL_PORT=5001 (venv) D:\python-code\flask-vuejs-madblog\back-end>flask shell Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32 App: app [production] Instance: D:\python-code\flask-vuejs-madblog\back-end\instance >>> from flask_mail import Message >>> from app.extensions import mail >>> msg = Message('test subject', sender='admin@madmalls.com', recipients=['wangy890601@163.com']) >>> msg.body = "testing" >>> msg.html = "<b>testing</b>" >>> mail.send(msg) send: 'ehlo [172.17.1.80]\r\n' reply: b'250-DESKTOP-II1RENJ\r\n' reply: b'250-8BITMIME\r\n' reply: b'250 HELP\r\n' reply: retcode (250); Msg: b'DESKTOP-II1RENJ\n8BITMIME\nHELP' send: 'mail FROM:<admin@madmalls.com>\r\n' reply: b'250 OK\r\n' reply: retcode (250); Msg: b'OK' send: 'rcpt TO:<wangy890601@163.com>\r\n' reply: b'250 OK\r\n' reply: retcode (250); Msg: b'OK' send: 'data\r\n' reply: b'354 End data with <CR><LF>.<CR><LF>\r\n' reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>' data: (354, b'End data with <CR><LF>.<CR><LF>') send: b'Content-Type: multipart/mixed; boundary="===============0699970148=="\r\nMIME-Version: 1.0\r\nSubject: test subject\r\nFrom: admin@madmalls.com\r\nTo: wangy890601@163.com\r\nDate: Wed, 28 Nov 2018 11:23:35 +0800\r\nMessage-ID: <154337540655.11996.13280566001020547532@DESKTOP-II1RENJ>\r\n\r\n--===============0699970148==\r\nContent-Type: multipart/alternative; boundary="===============0982821201=="\r\nMIME-Version: 1.0\r\n\r\n--===============0982821201==\r\nContent-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n\r\ntesting\r\n--===============0982821201==\r\nContent-Type: text/html; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n\r\n<b>testing</b>\r\n--===============0982821201==--\r\n\r\n--===============0699970148==--\r\n.\r\n' reply: b'250 OK\r\n' reply: retcode (250); Msg: b'OK' data: (250, b'OK') send: 'quit\r\n' reply: b'221 Bye\r\n' reply: retcode (221); Msg: b'Bye' >>>
说明 Flask-Mail
成功连接到本机的测试邮件服务器,并发送了邮件,你也可以去启动测试邮件服务器的终端中查看到发送过程
1.2 QQ邮箱
要使用 QQ 邮箱的 SMTP
服务来发送邮件,首先需要开启该服务(设置 -- 账户),并获取授权码
修改 back-end/.env
:
... MAIL_SERVER='smtp.qq.com' MAIL_PORT=465 MAIL_USE_SSL=1 MAIL_USERNAME='你的QQ邮箱地址' MAIL_PASSWORD='授权码' MAIL_SENDER='Madman <xxx@qq.com>'
重新开启一个终端,将使用 .env
中设置的环境变量:
(venv) D:\python-code\flask-vuejs-madblog\back-end>flask shell Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32 App: app [production] Instance: D:\python-code\flask-vuejs-madblog\back-end\instance >>> from flask_mail import Message >>> from app.extensions import mail >>> msg = Message('test subject', sender='2169618016@qq.com', recipients=['wangy890601@163.com']) >>> msg.body
2 条评论
评论者的用户名
评论时间charming
2019-03-26T17:14:13Z请问下还会更新吗
Ryan0v0
2020-04-29T08:17:04Z请问执行mail.send(msg)之后出现这种错误可能是什么原因呢? smtplib.SMTPServerDisconnected: please run connect() first