from app import mail
from threading import Thread
from flask_mail import Message
from flask import current_app, render_template
def send_async_email(app, msg):
with app.app_context():
try:
mail.send(msg)
except Exception as e:
pass
def send_mail(to, subject, template, **kwargs):
# Python email
# msg = Message('测试邮件', sender='aaa@qq.com', body='Test',
# recipients=['user@qq.com'])
msg = Message('[鱼书]' + ' ' + subject,
sender=current_app.config['MAIL_USERNAME'],
recipients=[to])
msg.html = render_template(template, **kwargs)
# current_app app = Flask()
app = current_app._get_current_object()
thr = Thread(target=send_async_email, args=[app, msg])
thr.start()
from flask import Flask
from flask_login import LoginManager
from app.models.book import db
from flask_mail import Mail
login_manager = LoginManager()
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)
login_manager.init_app(app)
login_manager.login_view = 'web.login'
login_manager.login_message = '请先登录或注册'
mail.init_app(app)
with app.app_context():
db.create_all()
return app
def register_blueprint(app):
from app.web.book import web
app.register_blueprint(web)
7月老师深入浅出剖析Flask核心机制,和你一起探讨Python高级编程
了解课程