Development of an internal social media platform with personalised dashboards for students
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.

backends.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from django.core.files.base import ContentFile
  2. from django.core.mail.backends.base import BaseEmailBackend
  3. from .settings import get_default_priority
  4. class EmailBackend(BaseEmailBackend):
  5. def open(self):
  6. pass
  7. def close(self):
  8. pass
  9. def send_messages(self, email_messages):
  10. """
  11. Queue one or more EmailMessage objects and returns the number of
  12. email messages sent.
  13. """
  14. from .mail import create
  15. from .utils import create_attachments
  16. if not email_messages:
  17. return
  18. for email_message in email_messages:
  19. subject = email_message.subject
  20. from_email = email_message.from_email
  21. message = email_message.body
  22. headers = email_message.extra_headers
  23. # Check whether email has 'text/html' alternative
  24. alternatives = getattr(email_message, 'alternatives', ())
  25. for alternative in alternatives:
  26. if alternative[1].startswith('text/html'):
  27. html_message = alternative[0]
  28. break
  29. else:
  30. html_message = ''
  31. attachment_files = dict([(name, ContentFile(content))
  32. for name, content, _ in email_message.attachments])
  33. email = create(sender=from_email,
  34. recipients=email_message.to, cc=email_message.cc,
  35. bcc=email_message.bcc, subject=subject,
  36. message=message, html_message=html_message,
  37. headers=headers)
  38. if attachment_files:
  39. attachments = create_attachments(attachment_files)
  40. email.attachments.add(*attachments)
  41. if get_default_priority() == 'now':
  42. email.dispatch()