from application import app, mail
from threading import Thread
from flask.ext.mail import Message
__all__ = ['send_email', 'send_bulk_email', 'create_email']
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_bulk_async_email(app, emails):
with app.app_context():
with mail.connect() as conn:
for email in emails:
conn.send(email)
def create_email(subject, recipients, text_body, html_body, sender=('My Company', '[email protected]')):
email = Message(subject, sender=sender, recipients=recipients)
email.body = text_body
email.html = html_body
return email
def send_email(email):
thr = Thread(target=send_async_email, args=[app, email])
thr.start()
def send_bulk_email(emails):
thr = Thread(target=send_bulk_async_email, args=[app, emails])
thr.start()
The first two functions are used internally as the threaded function calls. The create_email function is used to generate Message objects, which are then passed back to the send_email or send_bulk_email function dependent on whether there is a singular or multiple emails, respectively.
I currently think merging the singular and multiple send functions would be overkill, and remove some of the readability, however I am also open to feedback on that opinion.