from threading import Thread
from flask_mail import Message
from flask import current_app
from app import mail


def send_async_email(app, msg):
    with app.app_context():  # app.app_context gives a new thread access to app.config to be able to use mail config
        mail.send(msg)


def send_email(subject, recipients, text_body, html_body):
    msg = Message(subject, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    Thread(target=send_async_email, args=(current_app._get_current_object(), msg)).start()


