2019-07-08 09:29:11 +00:00
|
|
|
from flask import current_app, render_template
|
|
|
|
from flask_mail import Message
|
2020-03-27 11:06:11 +00:00
|
|
|
from threading import Thread
|
2019-07-08 09:29:11 +00:00
|
|
|
from . import mail
|
|
|
|
|
|
|
|
|
2020-04-21 08:26:44 +00:00
|
|
|
def create_message(recipient, subject, template, **kwargs):
|
|
|
|
app = current_app._get_current_object()
|
|
|
|
sender = app.config['NOPAQUE_MAIL_SENDER']
|
|
|
|
subject_prefix = app.config['NOPAQUE_MAIL_SUBJECT_PREFIX']
|
|
|
|
msg = Message('{} {}'.format(subject_prefix, subject),
|
|
|
|
recipients=[recipient], sender=sender)
|
|
|
|
msg.body = render_template('{}.txt.j2'.format(template), **kwargs)
|
|
|
|
msg.html = render_template('{}.html.j2'.format(template), **kwargs)
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
|
|
def send(app, msg):
|
2019-07-08 09:29:11 +00:00
|
|
|
with app.app_context():
|
|
|
|
mail.send(msg)
|
|
|
|
|
|
|
|
|
2020-04-21 08:26:44 +00:00
|
|
|
def send_async(msg):
|
2020-04-20 12:24:36 +00:00
|
|
|
app = current_app._get_current_object()
|
2020-04-21 08:26:44 +00:00
|
|
|
thread = Thread(target=send, args=(app, msg))
|
2019-11-15 10:45:04 +00:00
|
|
|
thread.start()
|
|
|
|
return thread
|