nopaque/app/email.py

22 lines
710 B
Python
Raw Normal View History

2019-07-08 09:29:11 +00:00
from flask import current_app, render_template
from flask_mail import Message
2020-03-27 11:06:11 +00:00
from threading import Thread
2019-07-08 09:29:11 +00:00
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))
2019-11-15 10:45:04 +00:00
thread.start()
return thread