mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
28 lines
843 B
Python
28 lines
843 B
Python
from flask import current_app, render_template
|
|
from flask_mail import Message
|
|
from threading import Thread
|
|
from . import mail
|
|
|
|
|
|
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):
|
|
with app.app_context():
|
|
mail.send(msg)
|
|
|
|
|
|
def send_async(msg):
|
|
app = current_app._get_current_object()
|
|
thread = Thread(target=send, args=(app, msg))
|
|
thread.start()
|
|
return thread
|