tools for sending email imported for backwards compatibility and for the sake of a cleaner namespace these symbols used to be in djangocoremail py before the introduction of email backends and the subsequent reorganization see 10355 load an email backend and return an instance of it if backend is none default use settings emailbackend both failsilently and other keyword arguments are used in the constructor of the backend 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 if fromemail is none use the defaultfromemail setting if authuser is none use the emailhostuser setting if authpassword is none use the emailhostpassword setting note the api for this method is frozen new code wanting to extend the functionality should use the emailmessage class directly given a datatuple of subject message fromemail recipientlist send each message to each recipient list return the number of emails sent if fromemail is none use the defaultfromemail setting if authuser and authpassword are set use them to log in if authuser is none use the emailhostuser setting if authpassword is none use the emailhostpassword setting note the api for this method is frozen new code wanting to extend the functionality should use the emailmessage class directly send a message to the admins as defined by the admins setting if not settings admins return if not allisinstancea list tuple and lena 2 for a in settings admins raise valueerrorthe admins setting must be a list of 2tuples mail emailmultialternatives ss settings emailsubjectprefix subject message settings serveremail a1 for a in settings admins connectionconnection if htmlmessage mail attachalternativehtmlmessage texthtml mail sendfailsilentlyfailsilently def mailmanagers subject message failsilentlyfalse connectionnone htmlmessagenone imported for backwards compatibility and for the sake 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 load an email backend and return an instance of it if backend is none default use settings email_backend both fail_silently and other keyword arguments are used in the constructor of the backend 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 if from_email is none use the default_from_email setting if auth_user is none use the email_host_user setting if auth_password is none use the email_host_password setting note the api for this method is frozen new code wanting to extend the functionality should use the emailmessage class directly given a datatuple of subject message from_email recipient_list send each message to each recipient list return the number of emails sent 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 note the api for this method is frozen new code wanting to extend the functionality should use the emailmessage class directly send a message to the admins as defined by the admins setting send a message to the managers as defined by the managers setting
from django.conf import settings





from django.core.mail.message import (
    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

__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",
]


def get_connection(backend=None, fail_silently=False, **kwds):
    
    klass = import_string(backend or settings.EMAIL_BACKEND)
    return klass(fail_silently=fail_silently, **kwds)


def send_mail(
    subject,
    message,
    from_email,
    recipient_list,
    fail_silently=False,
    auth_user=None,
    auth_password=None,
    connection=None,
    html_message=None,
):
    
    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
    )
    if html_message:
        mail.attach_alternative(html_message, "text/html")

    return mail.send()


def send_mass_mail(
    datatuple, fail_silently=False, auth_user=None, auth_password=None, connection=None
):
    
    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
    ]
    return connection.send_messages(messages)


def mail_admins(
    subject, message, fail_silently=False, connection=None, html_message=None
):
    
    if not settings.ADMINS:
        return
    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.")
    mail = EmailMultiAlternatives(
        "%s%s" % (settings.EMAIL_SUBJECT_PREFIX, subject),
        message,
        settings.SERVER_EMAIL,
        [a[1] for a in settings.ADMINS],
        connection=connection,
    )
    if html_message:
        mail.attach_alternative(html_message, "text/html")
    mail.send(fail_silently=fail_silently)


def mail_managers(
    subject, message, fail_silently=False, connection=None, html_message=None
):
    
    if not settings.MANAGERS:
        return
    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.")
    mail = EmailMultiAlternatives(
        "%s%s" % (settings.EMAIL_SUBJECT_PREFIX, subject),
        message,
        settings.SERVER_EMAIL,
        [a[1] for a in settings.MANAGERS],
        connection=connection,
    )
    if html_message:
        mail.attach_alternative(html_message, "text/html")
    mail.send(fail_silently=fail_silently)