|
| 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 = import_module(path) |
| 32 | + except ImportError, e: |
| 33 | + raise ImproperlyConfigured(('Error importing email backend %s: "%s"' |
| 34 | + % (path, e))) |
| 35 | + try: |
| 36 | + cls = getattr(mod, 'EmailBackend') |
| 37 | + except AttributeError: |
| 38 | + raise ImproperlyConfigured(('Module "%s" does not define a ' |
| 39 | + '"EmailBackend" class' % path)) |
| 40 | + return cls(fail_silently=fail_silently, **kwds) |
| 41 | + |
| 42 | + |
| 43 | +def send_mail(subject, message, from_email, recipient_list, |
| 44 | + fail_silently=False, auth_user=None, auth_password=None, |
| 45 | + connection=None): |
| 46 | + """ |
| 47 | + Easy wrapper for sending a single message to a recipient list. All members |
| 48 | + of the recipient list will see the other recipients in the 'To' field. |
| 49 | + |
| 50 | + If auth_user is None, the EMAIL_HOST_USER setting is used. |
| 51 | + If auth_password is None, the EMAIL_HOST_PASSWORD setting is used. |
| 52 | + |
| 53 | + Note: The API for this method is frozen. New code wanting to extend the |
| 54 | + functionality should use the EmailMessage class directly. |
| 55 | + """ |
| 56 | + connection = connection or get_connection(username=auth_user, |
| 57 | + password=auth_password, |
| 58 | + fail_silently=fail_silently) |
| 59 | + return EmailMessage(subject, message, from_email, recipient_list, |
| 60 | + connection=connection).send() |
| 61 | + |
| 62 | + |
| 63 | +def send_mass_mail(datatuple, fail_silently=False, auth_user=None, |
| 64 | + auth_password=None, connection=None): |
| 65 | + """ |
| 66 | + Given a datatuple of (subject, message, from_email, recipient_list), sends |
| 67 | + each message to each recipient list. Returns the number of e-mails sent. |
| 68 | + |
| 69 | + If from_email is None, the DEFAULT_FROM_EMAIL setting is used. |
| 70 | + If auth_user and auth_password are set, they're used to log in. |
| 71 | + If auth_user is None, the EMAIL_HOST_USER setting is used. |
| 72 | + If auth_password is None, the EMAIL_HOST_PASSWORD setting is used. |
| 73 | + |
| 74 | + Note: The API for this method is frozen. New code wanting to extend the |
| 75 | + functionality should use the EmailMessage class directly. |
| 76 | + """ |
| 77 | + connection = connection or get_connection(username=auth_user, |
| 78 | + password=auth_password, |
| 79 | + fail_silently=fail_silently) |
| 80 | + messages = [EmailMessage(subject, message, sender, recipient) |
| 81 | + for subject, message, sender, recipient in datatuple] |
| 82 | + return connection.send_messages(messages) |
| 83 | + |
| 84 | + |
| 85 | +def mail_admins(subject, message, fail_silently=False, connection=None): |
| 86 | + """Sends a message to the admins, as defined by the ADMINS setting.""" |
| 87 | + if not settings.ADMINS: |
| 88 | + return |
| 89 | + EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message, |
| 90 | + settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS], |
| 91 | + connection=connection).send(fail_silently=fail_silently) |
| 92 | + |
| 93 | + |
| 94 | +def mail_managers(subject, message, fail_silently=False, connection=None): |
| 95 | + """Sends a message to the managers, as defined by the MANAGERS setting.""" |
| 96 | + if not settings.MANAGERS: |
| 97 | + return |
| 98 | + EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message, |
| 99 | + settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS], |
| 100 | + connection=connection).send(fail_silently=fail_silently) |
| 101 | + |
| 102 | + |
| 103 | +class SMTPConnection(_SMTPConnection): |
| 104 | + def __init__(self, *args, **kwds): |
| 105 | + import warnings |
| 106 | + warnings.warn( |
| 107 | + 'mail.SMTPConnection is deprecated; use mail.get_connection() instead.', |
| 108 | + DeprecationWarning |
| 109 | + ) |
| 110 | + super(SMTPConnection, self).__init__(*args, **kwds) |
0 commit comments