2009-11-03 20:53:26 +08:00
|
|
|
"""
|
|
|
|
Tools for sending email.
|
|
|
|
"""
|
|
|
|
from django.conf import settings
|
2015-06-16 02:07:31 +08:00
|
|
|
# Imported for backwards compatibility and for the sake
|
2009-11-03 20:53:26 +08:00
|
|
|
# of a cleaner namespace. These symbols used to be in
|
|
|
|
# django/core/mail.py before the introduction of email
|
|
|
|
# backends and the subsequent reorganization (See #10355)
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.core.mail.message import (
|
2015-06-16 02:07:31 +08:00
|
|
|
DEFAULT_ATTACHMENT_MIME_TYPE, BadHeaderError, EmailMessage,
|
|
|
|
EmailMultiAlternatives, SafeMIMEMultipart, SafeMIMEText,
|
|
|
|
forbid_multi_line_headers, make_msgid,
|
|
|
|
)
|
|
|
|
from django.core.mail.utils import DNS_NAME, CachedDnsName
|
|
|
|
from django.utils.module_loading import import_string
|
2011-09-11 05:00:25 +08:00
|
|
|
|
2013-10-18 19:25:30 +08:00
|
|
|
__all__ = [
|
|
|
|
'CachedDnsName', 'DNS_NAME', 'EmailMessage', 'EmailMultiAlternatives',
|
|
|
|
'SafeMIMEText', 'SafeMIMEMultipart', 'DEFAULT_ATTACHMENT_MIME_TYPE',
|
|
|
|
'make_msgid', 'BadHeaderError', 'forbid_multi_line_headers',
|
|
|
|
'get_connection', 'send_mail', 'send_mass_mail', 'mail_admins',
|
|
|
|
'mail_managers',
|
|
|
|
]
|
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
def get_connection(backend=None, fail_silently=False, **kwds):
|
2011-04-02 00:10:22 +08:00
|
|
|
"""Load an email backend and return an instance of it.
|
2009-11-03 20:53:26 +08:00
|
|
|
|
2017-01-26 03:02:33 +08:00
|
|
|
If backend is None (default), use settings.EMAIL_BACKEND.
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
Both fail_silently and other keyword arguments are used in the
|
|
|
|
constructor of the backend.
|
|
|
|
"""
|
2014-01-21 04:15:14 +08:00
|
|
|
klass = import_string(backend or settings.EMAIL_BACKEND)
|
2010-01-04 20:05:04 +08:00
|
|
|
return klass(fail_silently=fail_silently, **kwds)
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
def send_mail(subject, message, from_email, recipient_list,
|
|
|
|
fail_silently=False, auth_user=None, auth_password=None,
|
2013-07-28 23:11:04 +08:00
|
|
|
connection=None, html_message=None):
|
2009-11-03 20:53:26 +08:00
|
|
|
"""
|
|
|
|
Easy wrapper for sending a single message to a recipient list. All members
|
|
|
|
of the recipient list will see the other recipients in the 'To' field.
|
|
|
|
|
2017-01-26 03:02:33 +08:00
|
|
|
If auth_user is None, use the EMAIL_HOST_USER setting.
|
|
|
|
If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
Note: The API for this method is frozen. New code wanting to extend the
|
|
|
|
functionality should use the EmailMessage class directly.
|
|
|
|
"""
|
2016-03-29 06:33:29 +08:00
|
|
|
connection = connection or get_connection(
|
|
|
|
username=auth_user,
|
|
|
|
password=auth_password,
|
|
|
|
fail_silently=fail_silently,
|
|
|
|
)
|
|
|
|
mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)
|
2013-07-28 23:11:04 +08:00
|
|
|
if html_message:
|
|
|
|
mail.attach_alternative(html_message, 'text/html')
|
|
|
|
|
|
|
|
return mail.send()
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
|
|
|
|
auth_password=None, connection=None):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Given a datatuple of (subject, message, from_email, recipient_list), send
|
|
|
|
each message to each recipient list. Return the number of emails sent.
|
2009-11-03 20:53:26 +08:00
|
|
|
|
2017-01-26 03:02:33 +08:00
|
|
|
If from_email is None, use the DEFAULT_FROM_EMAIL setting.
|
|
|
|
If auth_user and auth_password are set, use them to log in.
|
|
|
|
If auth_user is None, use the EMAIL_HOST_USER setting.
|
|
|
|
If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
Note: The API for this method is frozen. New code wanting to extend the
|
|
|
|
functionality should use the EmailMessage class directly.
|
|
|
|
"""
|
2016-03-29 06:33:29 +08:00
|
|
|
connection = connection or get_connection(
|
|
|
|
username=auth_user,
|
|
|
|
password=auth_password,
|
|
|
|
fail_silently=fail_silently,
|
|
|
|
)
|
|
|
|
messages = [
|
|
|
|
EmailMessage(subject, message, sender, recipient, connection=connection)
|
|
|
|
for subject, message, sender, recipient in datatuple
|
|
|
|
]
|
2009-11-03 20:53:26 +08:00
|
|
|
return connection.send_messages(messages)
|
|
|
|
|
|
|
|
|
2010-12-06 22:21:51 +08:00
|
|
|
def mail_admins(subject, message, fail_silently=False, connection=None,
|
|
|
|
html_message=None):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Send a message to the admins, as defined by the ADMINS setting."""
|
2009-11-03 20:53:26 +08:00
|
|
|
if not settings.ADMINS:
|
|
|
|
return
|
2019-06-29 07:50:43 +08:00
|
|
|
if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.ADMINS):
|
|
|
|
raise ValueError('The ADMINS setting must be a list of 2-tuples.')
|
2016-03-29 06:33:29 +08:00
|
|
|
mail = EmailMultiAlternatives(
|
|
|
|
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
|
|
|
|
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
|
|
|
|
connection=connection,
|
|
|
|
)
|
2010-12-06 22:21:51 +08:00
|
|
|
if html_message:
|
|
|
|
mail.attach_alternative(html_message, 'text/html')
|
|
|
|
mail.send(fail_silently=fail_silently)
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
|
2010-12-06 22:21:51 +08:00
|
|
|
def mail_managers(subject, message, fail_silently=False, connection=None,
|
|
|
|
html_message=None):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Send a message to the managers, as defined by the MANAGERS setting."""
|
2009-11-03 20:53:26 +08:00
|
|
|
if not settings.MANAGERS:
|
|
|
|
return
|
2019-06-29 07:50:43 +08:00
|
|
|
if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.MANAGERS):
|
|
|
|
raise ValueError('The MANAGERS setting must be a list of 2-tuples.')
|
2016-03-29 06:33:29 +08:00
|
|
|
mail = EmailMultiAlternatives(
|
|
|
|
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
|
|
|
|
settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
|
|
|
|
connection=connection,
|
|
|
|
)
|
2010-12-06 22:21:51 +08:00
|
|
|
if html_message:
|
|
|
|
mail.attach_alternative(html_message, 'text/html')
|
|
|
|
mail.send(fail_silently=fail_silently)
|