文章内容
2018/9/7 17:51:25,作 者: 黄兵
flask 使用gmail发送邮件
最近在flask框架下面需要使用gmail发送邮件。
具体代码如下:
MAIL_SERVER = os.environ.get('MAIL_SERVER', 'smtp.gmail.com')
MAIL_PORT = int(os.environ.get('MAIL_PORT', '587'))
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS', 'true').lower() in \
['true', 'on', '1']
MAIL_USERNAME = os.environ.get('MAIL_USERNAME', '[email protected]')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD', 'password')
FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
FLASKY_MAIL_SENDER = 'Flasky Admin '
FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
SSL_REDIRECT = False 但是在调试的时候出现了很多问题。
1、首先是连接超时,这个在国内很正常了,google的一些服务被限制访问;
2、账号或者密码错误:参考官方文档:允许安全性较低的应用访问您的帐号
3、还有端口的问题,我引用了这篇问答回答,不多说:
- The server is "smtp.gmail.com".
- The port must match the type of security used.
- If using STARTTLS with
MAIL_USE_TLS = True, then useMAIL_PORT = 587. - If using SSL/TLS directly with
MAIL_USE_SSL = True, then useMAIL_PORT = 465. - Enable either STARTTLS or SSL/TLS, not both.
- If using STARTTLS with
- Depending on your Google account's security settings, you may need to generate and use an app password rather than the account password. This may also require enabling 2-step verification. You should probably set this up anyway.
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USERNAME = '[email protected]'
MAIL_PASSWORD = 'app password generated in step 3'基本问题就这些,还有其他什么问题没有说道,欢迎大家在下面补充。
参考资料:
1、Configure Flask-Mail to use GMail
黄兵个人博客原创。
转载请注明出处:黄兵个人博客 - flask 使用gmail发送邮件
评论列表