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.

77 lines
1.8 KiB

  1. from threading import Thread
  2. from django.conf import settings
  3. from django.core.mail import EmailMultiAlternatives
  4. from django.template.loader import render_to_string as render
  5. class EmailThread(Thread):
  6. def __init__(self, subject, text, html, to, headers):
  7. Thread.__init__(self)
  8. self.subject = subject
  9. self.text = text
  10. self.html = html
  11. self.to = to
  12. self.headers = headers
  13. def run(self):
  14. msg = EmailMultiAlternatives(
  15. self.subject, self.text, to=(self.to,), headers=self.headers
  16. )
  17. msg.attach_alternative(self.html, "text/html")
  18. msg.send()
  19. def send(name, to, ctx, headers={}):
  20. ctx["SITE_ROOT"] = settings.SITE_ROOT
  21. subject = render("emails/%s-subject.html" % name, ctx).strip()
  22. text = render("emails/%s-body-text.html" % name, ctx)
  23. html = render("emails/%s-body-html.html" % name, ctx)
  24. t = EmailThread(subject, text, html, to, headers)
  25. if hasattr(settings, "BLOCKING_EMAILS"):
  26. # In tests, we send emails synchronously
  27. # so we can inspect the outgoing messages
  28. t.run()
  29. else:
  30. # Outside tests, we send emails on thread,
  31. # so there is no delay for the user.
  32. t.start()
  33. def login(to, ctx):
  34. send("login", to, ctx)
  35. def transfer_request(to, ctx):
  36. send("transfer-request", to, ctx)
  37. def set_password(to, ctx):
  38. send("set-password", to, ctx)
  39. def change_email(to, ctx):
  40. send("change-email", to, ctx)
  41. def alert(to, ctx, headers={}):
  42. send("alert", to, ctx, headers)
  43. def verify_email(to, ctx):
  44. send("verify-email", to, ctx)
  45. def report(to, ctx, headers={}):
  46. send("report", to, ctx, headers)
  47. def deletion_notice(to, ctx, headers={}):
  48. send("deletion-notice", to, ctx, headers)
  49. def sms_limit(to, ctx):
  50. send("sms-limit", to, ctx)