flask_mail是flask封装好的发送邮件的功能。
安装与初始化
pip install flask-mail
from flask_mail import Mail
mail = Mail()
def create_app():
app = Flask(__name__)
app.config.from_object('app.secure')
app.config.from_object('app.setting')
register_blueprint(app)
db.init_app(app)
mail.init_app(app) # 初始化 app
db.create_all(app=app)
return app
配置
MAIL_SERVER = 'smtp.qq.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USE_TLS = False
MAIL_USERNAME = '[email protected]'
MAIL_PASSWORD = 'mypassword'
MAIL_SUBJECT_PREFIX = '[fisher]'
MAIL_SENDER = 'fisher <[email protected]>'
发送邮件
这里用到了线程,异步发送邮件,防止因为网络原因,发送邮件时中间等待时间长,但要注意,因为线程隔离的原因,会导致currrent_app传到新的线程中为unbound状态,即没有app_context对象,会出现out of context错误,所以这里获取真实的flask对象。
from flask import current_app, render_template
from flask_mail import Message
from app import mail
def send_async_email(app, msg):
with app.app_context():
try:
mail.send(msg)
except Exception as e:
pass
def send_email(to, subject, template, **kwargs):
# msg = Message('测试邮件', sender='[email protected]', body='test', recipients=['459314922.com', ])
msg = Message('fihser' + ' ' + subject, sender=current_app.config['MAIL_USERNAME'], recipients=[to])
msg.html = render_template(template, **kwargs)
app = current_app._get_current_object() # 获取真实的app的对象,而非flask中的Local_proxy代理的app对象
th = Thread(target=send_async_email, args=[app, msg])
th.start()