__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. Tools for sending email.
  3. """
  4. from django.conf import settings
  5. # Imported for backwards compatibility and for the sake
  6. # of a cleaner namespace. These symbols used to be in
  7. # django/core/mail.py before the introduction of email
  8. # backends and the subsequent reorganization (See #10355)
  9. from django.core.mail.message import (
  10. DEFAULT_ATTACHMENT_MIME_TYPE, BadHeaderError, EmailMessage,
  11. EmailMultiAlternatives, SafeMIMEMultipart, SafeMIMEText,
  12. forbid_multi_line_headers, make_msgid,
  13. )
  14. from django.core.mail.utils import DNS_NAME, CachedDnsName
  15. from django.utils.module_loading import import_string
  16. __all__ = [
  17. 'CachedDnsName', 'DNS_NAME', 'EmailMessage', 'EmailMultiAlternatives',
  18. 'SafeMIMEText', 'SafeMIMEMultipart', 'DEFAULT_ATTACHMENT_MIME_TYPE',
  19. 'make_msgid', 'BadHeaderError', 'forbid_multi_line_headers',
  20. 'get_connection', 'send_mail', 'send_mass_mail', 'mail_admins',
  21. 'mail_managers',
  22. ]
  23. def get_connection(backend=None, fail_silently=False, **kwds):
  24. """Load an email backend and return an instance of it.
  25. If backend is None (default), use settings.EMAIL_BACKEND.
  26. Both fail_silently and other keyword arguments are used in the
  27. constructor of the backend.
  28. """
  29. klass = import_string(backend or settings.EMAIL_BACKEND)
  30. return klass(fail_silently=fail_silently, **kwds)
  31. def send_mail(subject, message, from_email, recipient_list,
  32. fail_silently=False, auth_user=None, auth_password=None,
  33. connection=None, html_message=None):
  34. """
  35. Easy wrapper for sending a single message to a recipient list. All members
  36. of the recipient list will see the other recipients in the 'To' field.
  37. If from_email is None, use the DEFAULT_FROM_EMAIL setting.
  38. If auth_user is None, use the EMAIL_HOST_USER setting.
  39. If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
  40. Note: The API for this method is frozen. New code wanting to extend the
  41. functionality should use the EmailMessage class directly.
  42. """
  43. connection = connection or get_connection(
  44. username=auth_user,
  45. password=auth_password,
  46. fail_silently=fail_silently,
  47. )
  48. mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)
  49. if html_message:
  50. mail.attach_alternative(html_message, 'text/html')
  51. return mail.send()
  52. def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
  53. auth_password=None, connection=None):
  54. """
  55. Given a datatuple of (subject, message, from_email, recipient_list), send
  56. each message to each recipient list. Return the number of emails sent.
  57. If from_email is None, use the DEFAULT_FROM_EMAIL setting.
  58. If auth_user and auth_password are set, use them to log in.
  59. If auth_user is None, use the EMAIL_HOST_USER setting.
  60. If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
  61. Note: The API for this method is frozen. New code wanting to extend the
  62. functionality should use the EmailMessage class directly.
  63. """
  64. connection = connection or get_connection(
  65. username=auth_user,
  66. password=auth_password,
  67. fail_silently=fail_silently,
  68. )
  69. messages = [
  70. EmailMessage(subject, message, sender, recipient, connection=connection)
  71. for subject, message, sender, recipient in datatuple
  72. ]
  73. return connection.send_messages(messages)
  74. def mail_admins(subject, message, fail_silently=False, connection=None,
  75. html_message=None):
  76. """Send a message to the admins, as defined by the ADMINS setting."""
  77. if not settings.ADMINS:
  78. return
  79. if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.ADMINS):
  80. raise ValueError('The ADMINS setting must be a list of 2-tuples.')
  81. mail = EmailMultiAlternatives(
  82. '%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
  83. settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
  84. connection=connection,
  85. )
  86. if html_message:
  87. mail.attach_alternative(html_message, 'text/html')
  88. mail.send(fail_silently=fail_silently)
  89. def mail_managers(subject, message, fail_silently=False, connection=None,
  90. html_message=None):
  91. """Send a message to the managers, as defined by the MANAGERS setting."""
  92. if not settings.MANAGERS:
  93. return
  94. if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.MANAGERS):
  95. raise ValueError('The MANAGERS setting must be a list of 2-tuples.')
  96. mail = EmailMultiAlternatives(
  97. '%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
  98. settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
  99. connection=connection,
  100. )
  101. if html_message:
  102. mail.attach_alternative(html_message, 'text/html')
  103. mail.send(fail_silently=fail_silently)