nopaque/app/email.py

28 lines
710 B
Python
Raw Normal View History

from flask import current_app, render_template
2019-07-08 09:29:11 +00:00
from flask_mail import Message
2021-12-07 13:18:05 +00:00
from typing import Any, Text
2019-07-08 09:29:11 +00:00
from . import mail
from .decorators import background
2019-07-08 09:29:11 +00:00
2021-12-07 13:18:05 +00:00
def create_message(
recipient: str,
subject: str,
template: str,
**kwargs: Any
) -> Message:
subject_prefix: str = current_app.config['NOPAQUE_MAIL_SUBJECT_PREFIX']
msg: Message = Message(
f'{subject_prefix} {subject}',
recipients=[recipient]
)
2022-04-25 09:32:10 +00:00
msg.body = render_template(f'{template}.txt.j2', **kwargs)
msg.html = render_template(f'{template}.html.j2', **kwargs)
2020-04-21 08:26:44 +00:00
return msg
@background
2021-12-07 13:18:05 +00:00
def send(msg: Message, *args, **kwargs):
2021-09-22 11:51:45 +00:00
with kwargs['app'].app_context():
2019-07-08 09:29:11 +00:00
mail.send(msg)