1.使用qq邮箱完成收发
先去qq邮箱设置->账户中找到服务器服务,开启pop3/smtp服务。
开启时需要验证身份,按步骤执行就好,最后会受到一个授权码,一定要记住这个授权码,很重要,后面要用,保存起来。
2.flask_mail初始化配置
像很多扩展一样,先导入,然后创建对象
from flask.ext.mail import Mail
mail = Mail(app)
配置邮箱
app.config['MAIL_SERVER'] = 'smtp.qq.com' # qq邮箱服务器
app.config['MAIL_PORT'] = 587 # 服务对应的端口
app.config['MAIL_USE_TLS'] = True # 启动传输层安全协议,默认False
app.config['MAIL_USERNAME'] = '[email protected]' # 邮件账户的用户名
app.config['MAIL_PASSWORD'] = '授权码'# 密码,这里的密码就是上面所说的*授权码*
发送邮件
from flask.ext.mail import Message
msg = Message('邮件主题', sender='发件人[email protected]', recipients=['收件人[email protected]'])
msg.body = '邮件内容'
msg.html = "邮件的html模板 body"
with app.app_context():
mail.send(msg) # 发送邮件
# 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'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = '授权码'
mail = Mail(app)
msg = Message('邮件主题', sender='[email protected]', recipients=['[email protected]'])
msg.body = '邮件内容'
msg.html = "邮件的html模板 body"
with app.app_context():
mail.send(msg)
if __name__ == '__main__':
app.run()