2022-09-02 11:02:04 +00:00
|
|
|
from flask import current_app, render_template
|
2019-07-08 09:29:11 +00:00
|
|
|
from flask_mail import Message
|
2022-06-28 10:30:02 +00:00
|
|
|
from threading import Thread
|
2022-09-02 11:02:04 +00:00
|
|
|
from app import mail
|
2019-07-08 09:29:11 +00:00
|
|
|
|
|
|
|
|
2022-09-02 11:02:04 +00:00
|
|
|
def create_message(recipient, subject, template, **kwargs):
|
2021-12-07 13:18:05 +00:00
|
|
|
subject_prefix: str = current_app.config['NOPAQUE_MAIL_SUBJECT_PREFIX']
|
|
|
|
msg: Message = Message(
|
2022-06-28 10:30:02 +00:00
|
|
|
body=render_template(f'{template}.txt.j2', **kwargs),
|
|
|
|
html=render_template(f'{template}.html.j2', **kwargs),
|
|
|
|
recipients=[recipient],
|
|
|
|
subject=f'{subject_prefix} {subject}'
|
2022-02-15 08:25:34 +00:00
|
|
|
)
|
2020-04-21 08:26:44 +00:00
|
|
|
return msg
|
|
|
|
|
|
|
|
|
2022-09-02 11:02:04 +00:00
|
|
|
def send(msg, *args, **kwargs):
|
|
|
|
def _send(app, msg):
|
|
|
|
with app.app_context():
|
|
|
|
mail.send(msg)
|
2022-06-28 10:30:02 +00:00
|
|
|
|
|
|
|
thread = Thread(target=_send, args=[current_app._get_current_object(), msg])
|
|
|
|
thread.start()
|
|
|
|
return thread
|