My flask project is based on Flask-Cookiecutter and I need to send emails asynchronously.
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.
My 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 appdef 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
my 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)
route in view.py
@blueprint.route('/mailer', methods=['GET', 'POST'])
def mailer():user = current_user.full_namesend_email(('[email protected]'),'New mail', 'test.html',user=user)return "Mail has been send."
Application run in my localhost and it's starting with command:
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.
Thanks for any answer.