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.

55 lines
1.2 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, name, to, ctx):
  7. Thread.__init__(self)
  8. self.name = name
  9. self.to = to
  10. self.ctx = ctx
  11. def run(self):
  12. self.ctx["SITE_ROOT"] = settings.SITE_ROOT
  13. subject = render('emails/%s-subject.html' % self.name, self.ctx)
  14. subject = subject.strip()
  15. text = render('emails/%s-body-text.html' % self.name, self.ctx)
  16. html = render('emails/%s-body-html.html' % self.name, self.ctx)
  17. msg = EmailMultiAlternatives(subject, text, to=(self.to, ))
  18. msg.attach_alternative(html, "text/html")
  19. msg.send()
  20. def send(name, to, ctx):
  21. t = EmailThread(name, to, ctx)
  22. if hasattr(settings, "BLOCKING_EMAILS"):
  23. t.run()
  24. else:
  25. t.start()
  26. def login(to, ctx):
  27. send("login", to, ctx)
  28. def set_password(to, ctx):
  29. send("set-password", to, ctx)
  30. def alert(to, ctx):
  31. send("alert", to, ctx)
  32. def verify_email(to, ctx):
  33. send("verify-email", to, ctx)
  34. def report(to, ctx):
  35. send("report", to, ctx)