nopaque/app/email.py

26 lines
755 B
Python
Raw Permalink Normal View History

from flask import current_app, render_template
2019-07-08 09:29:11 +00:00
from flask_mail import Message
from threading import Thread
from app import mail
2019-07-08 09:29:11 +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(
body=render_template(f'{template}.txt.j2', **kwargs),
html=render_template(f'{template}.html.j2', **kwargs),
recipients=[recipient],
subject=f'{subject_prefix} {subject}'
)
2020-04-21 08:26:44 +00:00
return msg
def send(msg, *args, **kwargs):
def _send(app, msg):
with app.app_context():
mail.send(msg)
thread = Thread(target=_send, args=[current_app._get_current_object(), msg])
thread.start()
return thread