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