nopaque/app/email.py

28 lines
821 B
Python
Raw Normal View History

from app import mail
from flask import current_app, Flask, render_template
2019-07-08 09:29:11 +00:00
from flask_mail import Message
from threading import Thread
from typing import Any
2019-07-08 09:29:11 +00:00
def create_message(recipient: str, subject: str, template: str, **kwargs: Any) -> Message:
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(app: Flask, msg):
with app.app_context():
2019-07-08 09:29:11 +00:00
mail.send(msg)
def send(msg: Message, *args, **kwargs):
thread = Thread(target=_send, args=[current_app._get_current_object(), msg])
thread.start()
return thread