2017-09-26 07:16:46 +00:00
|
|
|
from threading import Thread
|
2017-10-09 07:09:22 +00:00
|
|
|
from flask import current_app
|
2017-09-26 07:16:46 +00:00
|
|
|
from flask_mail import Message
|
2017-10-09 07:09:22 +00:00
|
|
|
from app import mail
|
2017-09-26 07:16:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_async_email(app, msg):
|
|
|
|
with app.app_context():
|
|
|
|
mail.send(msg)
|
|
|
|
|
|
|
|
|
2017-11-20 07:57:09 +00:00
|
|
|
def send_email(subject, sender, recipients, text_body, html_body,
|
|
|
|
attachments=None, sync=False):
|
2017-09-26 07:16:46 +00:00
|
|
|
msg = Message(subject, sender=sender, recipients=recipients)
|
|
|
|
msg.body = text_body
|
|
|
|
msg.html = html_body
|
2017-11-20 07:57:09 +00:00
|
|
|
if attachments:
|
|
|
|
for attachment in attachments:
|
|
|
|
msg.attach(*attachment)
|
|
|
|
if sync:
|
|
|
|
mail.send(msg)
|
|
|
|
else:
|
|
|
|
Thread(target=send_async_email,
|
|
|
|
args=(current_app._get_current_object(), msg)).start()
|