Plain is headed towards 1.0! Subscribe for development updates →

plain.mail

Everything you need to send email.

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