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.

__init__.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 auth_user is None, use the EMAIL_HOST_USER setting.
  38. If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
  39. Note: The API for this method is frozen. New code wanting to extend the
  40. functionality should use the EmailMessage class directly.
  41. """
  42. connection = connection or get_connection(
  43. username=auth_user,
  44. password=auth_password,
  45. fail_silently=fail_silently,
  46. )
  47. mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)
  48. if html_message:
  49. mail.attach_alternative(html_message, 'text/html')
  50. return mail.send()
  51. def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
  52. auth_password=None, connection=None):
  53. """
  54. Given a datatuple of (subject, message, from_email, recipient_list), send
  55. each message to each recipient list. Return the number of emails sent.
  56. If from_email is None, use the DEFAULT_FROM_EMAIL setting.
  57. If auth_user and auth_password are set, use them to log in.
  58. If auth_user is None, use the EMAIL_HOST_USER setting.
  59. If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
  60. Note: The API for this method is frozen. New code wanting to extend the
  61. functionality should use the EmailMessage class directly.
  62. """
  63. connection = connection or get_connection(
  64. username=auth_user,
  65. password=auth_password,
  66. fail_silently=fail_silently,
  67. )
  68. messages = [
  69. EmailMessage(subject, message, sender, recipient, connection=connection)
  70. for subject, message, sender, recipient in datatuple
  71. ]
  72. return connection.send_messages(messages)
  73. def mail_admins(subject, message, fail_silently=False, connection=None,
  74. html_message=None):
  75. """Send a message to the admins, as defined by the ADMINS setting."""
  76. if not settings.ADMINS:
  77. return
  78. mail = EmailMultiAlternatives(
  79. '%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
  80. settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
  81. connection=connection,
  82. )
  83. if html_message:
  84. mail.attach_alternative(html_message, 'text/html')
  85. mail.send(fail_silently=fail_silently)
  86. def mail_managers(subject, message, fail_silently=False, connection=None,
  87. html_message=None):
  88. """Send a message to the managers, as defined by the MANAGERS setting."""
  89. if not settings.MANAGERS:
  90. return
  91. mail = EmailMultiAlternatives(
  92. '%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
  93. settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
  94. connection=connection,
  95. )
  96. if html_message:
  97. mail.attach_alternative(html_message, 'text/html')
  98. mail.send(fail_silently=fail_silently)