Flask-Mail-基于Flask-Cookiecutter异步发送电子邮件 [英] Flask-Mail - Sending email asynchronously, based on Flask-Cookiecutter
问题描述
我的烧瓶项目基于 Flask-Cookiecutter ,我需要异步发送电子邮件.
My flask project is based on Flask-Cookiecutter and I need to send emails asynchronously.
发送电子邮件的功能由进行配置Miguel的教程和同步发送可以正常工作,但是我不知道如何修改它以便异步发送.
Function for sending email was configured by Miguel’s Tutorial and sending synchronously works fine, but i don’t know, how I can modify it for sending asynchronously.
我的app.py
def create_app(config_object=ProdConfig):
app = Flask(__name__)
app.config.from_object(config_object)
register_extensions(app)
register_blueprints(app)
register_errorhandlers(app)
return app
def register_extensions(app):
assets.init_app(app)
bcrypt.init_app(app)
cache.init_app(app)
db.init_app(app)
login_manager.init_app(app)
debug_toolbar.init_app(app)
migrate.init_app(app, db)
mail.init_app(app)
return None
我的view.py
from flask import current_app
@async
def send_async_email(current_app, msg):
with current_app.app_context():
print('##### spustam async')
mail.send(msg)
# Function for sending emails
def send_email(to, subject, template, **kwargs):
msg = Message(subject, recipients=[to])
msg.html = render_template('emails/' + template, **kwargs)
send_async_email(current_app, msg)
在view.py中路由
route in view.py
@blueprint.route('/mailer', methods=['GET', 'POST'])
def mailer():
user = current_user.full_name
send_email(('name@gmail.com'),
'New mail', 'test.html',
user=user)
return "Mail has been send."
应用程序在我的本地主机中运行,它以命令开头:
python manage.py server
当我调用发送邮件的功能时,控制台中的输出为:
When i call function for sending mail, output in console is:
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in a way. To solve
this set up an application context with app.app_context(). See the
documentation for more information.
感谢您的回答.
推荐答案
好吧,我为我的问题找到了解决方案,我在这里将其发布给其他开发人员:
Okay, i found solution for my question i posting it here for others developers:
我创建文件:email.py,代码为:
I create file: email.py with code:
from threading import Thread
from flask import current_app, render_template
from flask_mail import Message
from .extensions import mail
from time import sleep
def send_async_email(app, msg):
with app.app_context():
# block only for testing parallel thread
for i in range(10, -1, -1):
sleep(2)
print('time:', i)
print('====> sending async')
mail.send(msg)
def send_email(to, subject, template, **kwargs):
app = current_app._get_current_object()
msg = Message(subject, recipients=[to])
msg.html = render_template('emails/' + template, **kwargs)
thr = Thread(target=send_async_email, args=[app, msg])
thr.start()
return thr
我的view.py:
...
from app.email import send_email
...
@blueprint.route('/mailer', methods=['GET', 'POST'])
def mailer():
user = current_user.full_name
send_email(('name@gmail.com'),
'New mail', 'test.html',
user=user)
return "Mail has been send."
当我呼叫 http://localhost:5000/mailer 时,它将开始倒计时,几秒钟后便是邮件已发送.
And when i call http://localhost:5000/mailer it starts countdown and after few seconds is mail sent.
这篇关于Flask-Mail-基于Flask-Cookiecutter异步发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!