nopaque/app/email.py

22 lines
710 B
Python

from flask import current_app, render_template
from flask_mail import Message
from threading import Thread
from . import mail
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_email(to, subject, template, **kwargs):
app = current_app._get_current_object()
msg = Message(
'{} {}'.format(app.config['NOPAQUE_MAIL_SUBJECT_PREFIX'], subject),
recipients=[to], sender=app.config['NOPAQUE_MAIL_SENDER'])
msg.body = render_template('{}.txt.j2'.format(template), **kwargs)
msg.html = render_template('{}.html.j2'.format(template), **kwargs)
thread = Thread(target=send_async_email, args=(app, msg))
thread.start()
return thread