/packages/Django/django/core/mail/__init__.py

https://github.com/lmorchard/home-snippets-server-lib · Python · 111 lines · 79 code · 6 blank · 26 comment · 6 complexity · a6d405c8e5911cc8c98b615cbf39f985 MD5 · raw file

  1. """
  2. Tools for sending email.
  3. """
  4. from django.conf import settings
  5. from django.core.exceptions import ImproperlyConfigured
  6. from django.utils.importlib import import_module
  7. # Imported for backwards compatibility, and for the sake
  8. # of a cleaner namespace. These symbols used to be in
  9. # django/core/mail.py before the introduction of email
  10. # backends and the subsequent reorganization (See #10355)
  11. from django.core.mail.utils import CachedDnsName, DNS_NAME
  12. from django.core.mail.message import \
  13. EmailMessage, EmailMultiAlternatives, \
  14. SafeMIMEText, SafeMIMEMultipart, \
  15. DEFAULT_ATTACHMENT_MIME_TYPE, make_msgid, \
  16. BadHeaderError, forbid_multi_line_headers
  17. from django.core.mail.backends.smtp import EmailBackend as _SMTPConnection
  18. def get_connection(backend=None, fail_silently=False, **kwds):
  19. """Load an e-mail backend and return an instance of it.
  20. If backend is None (default) settings.EMAIL_BACKEND is used.
  21. Both fail_silently and other keyword arguments are used in the
  22. constructor of the backend.
  23. """
  24. path = backend or settings.EMAIL_BACKEND
  25. try:
  26. mod_name, klass_name = path.rsplit('.', 1)
  27. mod = import_module(mod_name)
  28. except ImportError, e:
  29. raise ImproperlyConfigured(('Error importing email backend module %s: "%s"'
  30. % (mod_name, e)))
  31. try:
  32. klass = getattr(mod, klass_name)
  33. except AttributeError:
  34. raise ImproperlyConfigured(('Module "%s" does not define a '
  35. '"%s" class' % (mod_name, klass_name)))
  36. return klass(fail_silently=fail_silently, **kwds)
  37. def send_mail(subject, message, from_email, recipient_list,
  38. fail_silently=False, auth_user=None, auth_password=None,
  39. connection=None):
  40. """
  41. Easy wrapper for sending a single message to a recipient list. All members
  42. of the recipient list will see the other recipients in the 'To' field.
  43. If auth_user is None, the EMAIL_HOST_USER setting is used.
  44. If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
  45. Note: The API for this method is frozen. New code wanting to extend the
  46. functionality should use the EmailMessage class directly.
  47. """
  48. connection = connection or get_connection(username=auth_user,
  49. password=auth_password,
  50. fail_silently=fail_silently)
  51. return EmailMessage(subject, message, from_email, recipient_list,
  52. connection=connection).send()
  53. def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
  54. auth_password=None, connection=None):
  55. """
  56. Given a datatuple of (subject, message, from_email, recipient_list), sends
  57. each message to each recipient list. Returns the number of e-mails sent.
  58. If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
  59. If auth_user and auth_password are set, they're used to log in.
  60. If auth_user is None, the EMAIL_HOST_USER setting is used.
  61. If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
  62. Note: The API for this method is frozen. New code wanting to extend the
  63. functionality should use the EmailMessage class directly.
  64. """
  65. connection = connection or get_connection(username=auth_user,
  66. password=auth_password,
  67. fail_silently=fail_silently)
  68. messages = [EmailMessage(subject, message, sender, recipient)
  69. for subject, message, sender, recipient in datatuple]
  70. return connection.send_messages(messages)
  71. def mail_admins(subject, message, fail_silently=False, connection=None):
  72. """Sends a message to the admins, as defined by the ADMINS setting."""
  73. if not settings.ADMINS:
  74. return
  75. EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
  76. settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
  77. connection=connection).send(fail_silently=fail_silently)
  78. def mail_managers(subject, message, fail_silently=False, connection=None):
  79. """Sends a message to the managers, as defined by the MANAGERS setting."""
  80. if not settings.MANAGERS:
  81. return
  82. EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
  83. settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
  84. connection=connection).send(fail_silently=fail_silently)
  85. class SMTPConnection(_SMTPConnection):
  86. def __init__(self, *args, **kwds):
  87. import warnings
  88. warnings.warn(
  89. 'mail.SMTPConnection is deprecated; use mail.get_connection() instead.',
  90. PendingDeprecationWarning
  91. )
  92. super(SMTPConnection, self).__init__(*args, **kwds)