email.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.emails
  4. ~~~~~~~~~~~~~~~~~~~~
  5. This module adds the functionality to send emails
  6. :copyright: (c) 2013 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from flask import render_template
  10. from flask.ext.mail import Message
  11. from flaskbb.extensions import mail
  12. from flaskbb.decorators import async
  13. def send_reset_token(user, token):
  14. send_email(
  15. subject="Password Reset",
  16. recipients=[user.email],
  17. text_body=render_template(
  18. "email/reset_password.txt",
  19. user=user,
  20. token=token
  21. ),
  22. html_body=render_template(
  23. "email/reset_password.html",
  24. user=user,
  25. token=token
  26. )
  27. )
  28. @async
  29. def send_async_email(msg):
  30. mail.send(msg)
  31. def send_email(subject, recipients, text_body, html_body, sender=""):
  32. if not sender:
  33. msg = Message(subject, recipients=recipients)
  34. else:
  35. msg = Message(subject, recipients=recipients, sender=sender)
  36. msg.body = text_body
  37. msg.html = html_body
  38. mail.send(msg)