Use Thread to support email delivery in background
onlyphantom opened this issue · 0 comments
onlyphantom commented
Prevent the server from waiting (thus slowing down) while the email delivery (eg. Reset Password request) is being sent.
Old:
from flask_mail import Mail
def send_email(subject, sender, recipients, message):
msg = Message(subject, sender=sender, recipients=recipients)
msg.html = message
mail.send() # mail = Mail(app)
New:
def send_email_bg(app, message):
with app.app_context():
mail.send(message)
def send_email(subject, sender, recipients, message):
msg = Message(subject, sender=sender, recipients=recipients)
msg.html = message
Thread(target=send_email_bg, args=(app, msg)).start()