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.

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