我的燒瓶項目基於Flask-Cookiecutter,我需要異步發送電子郵件。Flask-Mail - 基於Flask-Cookiecutter異步發送電子郵件
功能發送電子郵件配置Miguel’s Tutorial和同步發送工作正常,但我不知道,我怎麼能修改它發送異步。
我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在我的本地主機,它的起始命令view.py
@blueprint.route('/mailer', methods=['GET', 'POST'])
def mailer():
user = current_user.full_name
send_email(('[email protected]'),
'New mail', 'test.html',
user=user)
return "Mail has been send."
應用程序運行
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)
路線:
python manage.py server
當我打電話功能用於發送郵件,在控制檯輸出爲:
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.
感謝您的任何答覆。
,也有這個問題。 – Pegasus