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.

76 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(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 change_email(to, ctx):
  33. send("change-email", to, ctx)
  34. def alert(to, ctx, headers={}):
  35. send("alert", to, ctx, headers)
  36. def verify_email(to, ctx):
  37. send("verify-email", to, ctx)
  38. def report(to, ctx, headers={}):
  39. send("report", to, ctx, headers)
  40. def invoice(to, ctx, filename, pdf_data):
  41. ctx["SITE_ROOT"] = settings.SITE_ROOT
  42. subject = render('emails/invoice-subject.html', ctx).strip()
  43. text = render('emails/invoice-body-text.html', ctx)
  44. html = render('emails/invoice-body-html.html', ctx)
  45. msg = EmailMultiAlternatives(subject, text, to=(to, ))
  46. msg.attach_alternative(html, "text/html")
  47. msg.attach(filename, pdf_data, "application/pdf")
  48. msg.send()
  49. def deletion_notice(to, ctx, headers={}):
  50. send("deletion-notice", to, ctx, headers)