You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
669 B

  1. from threading import Thread
  2. from flask import current_app, render_template
  3. from flask_mail import Message
  4. from . import mail
  5. def send_async_email(app, msg):
  6. with app.app_context():
  7. mail.send(msg)
  8. def send_email(to, subject, template, **kwargs):
  9. app = current_app._get_current_object()
  10. msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
  11. sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
  12. msg.body = render_template(template + '.txt', **kwargs)
  13. msg.html = render_template(template + '.html', **kwargs)
  14. thr = Thread(target=send_async_email, args=[app, msg])
  15. thr.start()
  16. return thr