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 sync
Move email send function to a background thread:
from threading import Thread
def send_async_email(app,msg):
with current_app.app_context():
mail.send(msg)
def send_email(to, subject, template, **kwargs):
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