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.9 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. t.run()
  27. else:
  28. t.start()
  29. def login(to, ctx):
  30. send("login", to, ctx)
  31. def set_password(to, ctx):
  32. send("set-password", to, ctx)
  33. def change_email(to, ctx):
  34. send("change-email", to, ctx)
  35. def alert(to, ctx, headers={}):
  36. send("alert", to, ctx, headers)
  37. def verify_email(to, ctx):
  38. send("verify-email", to, ctx)
  39. def report(to, ctx, headers={}):
  40. send("report", to, ctx, headers)
  41. def invoice(to, ctx, filename, pdf_data):
  42. ctx["SITE_ROOT"] = settings.SITE_ROOT
  43. subject = render("emails/invoice-subject.html", ctx).strip()
  44. text = render("emails/invoice-body-text.html", ctx)
  45. html = render("emails/invoice-body-html.html", ctx)
  46. msg = EmailMultiAlternatives(subject, text, to=(to,))
  47. msg.attach_alternative(html, "text/html")
  48. msg.attach(filename, pdf_data, "application/pdf")
  49. msg.send()
  50. def deletion_notice(to, ctx, headers={}):
  51. send("deletion-notice", to, ctx, headers)