From 5d2cbf0f3420a1e443b4d15e9dd938b0c1c11d68 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sun, 6 Jun 2021 14:57:09 +0530 Subject: send mail with threading --- app/misc_func.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/misc_func.py b/app/misc_func.py index 11e2bb2..73e920d 100644 --- a/app/misc_func.py +++ b/app/misc_func.py @@ -1,4 +1,7 @@ +from threading import Thread from flask import flash +from flask_mailman import EmailMultiAlternatives +from app import app, mail def flash_errors(form): for field, errors in form.errors.items(): @@ -6,4 +9,19 @@ def flash_errors(form): flash(u"Error in the %s field - %s" % ( getattr(form, field).label.text, error - ), 'danger') \ No newline at end of file + ), 'danger') + +# Sauce: https://github.com/alectrocute/flaskSaaS/blob/master/app/toolbox/email.py + +def send(to, subject, body, body_html,thread=True): + sender = app.config["MAIL_FROM"] + message = EmailMultiAlternatives( + subject,body,sender,[to]) + message.attach_alternative(body_html,"text/html") + thr = Thread(target=send_async, args=[app,message]) + thr.start() + +def send_async(app, message): + with app.app_context(): + message.send() + print("Mail Sent") \ No newline at end of file -- cgit v1.2.3