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.

base.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Base email backend class."""
  2. class BaseEmailBackend:
  3. """
  4. Base class for email backend implementations.
  5. Subclasses must at least overwrite send_messages().
  6. open() and close() can be called indirectly by using a backend object as a
  7. context manager:
  8. with backend as connection:
  9. # do something with connection
  10. pass
  11. """
  12. def __init__(self, fail_silently=False, **kwargs):
  13. self.fail_silently = fail_silently
  14. def open(self):
  15. """
  16. Open a network connection.
  17. This method can be overwritten by backend implementations to
  18. open a network connection.
  19. It's up to the backend implementation to track the status of
  20. a network connection if it's needed by the backend.
  21. This method can be called by applications to force a single
  22. network connection to be used when sending mails. See the
  23. send_messages() method of the SMTP backend for a reference
  24. implementation.
  25. The default implementation does nothing.
  26. """
  27. pass
  28. def close(self):
  29. """Close a network connection."""
  30. pass
  31. def __enter__(self):
  32. try:
  33. self.open()
  34. except Exception:
  35. self.close()
  36. raise
  37. return self
  38. def __exit__(self, exc_type, exc_value, traceback):
  39. self.close()
  40. def send_messages(self, email_messages):
  41. """
  42. Send one or more EmailMessage objects and return the number of email
  43. messages sent.
  44. """
  45. raise NotImplementedError('subclasses of BaseEmailBackend must override send_messages() method')