email.py 652 B

12345678910111213141516171819202122
  1. from threading import Thread
  2. from flask.ext.mail import Message
  3. from app import app, mail
  4. def send(recipient, subject, body):
  5. '''
  6. Send a mail to a recipient. The body is usually a rendered HTML template.
  7. The sender's credentials has been configured in the config.py file.
  8. '''
  9. sender = app.config['ADMINS'][0]
  10. message = Message(subject, sender=sender, recipients=[recipient])
  11. message.html = body
  12. # Create a new thread
  13. thr = Thread(target=send_async, args=[app, message])
  14. thr.start()
  15. def send_async(app, message):
  16. ''' Send the mail asynchronously. '''
  17. with app.app_context():
  18. mail.send(message)