Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/core/mail/__init__.py: 17%
36 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2Tools for sending email.
3"""
4from django.conf import settings
6# Imported for backwards compatibility and for the sake
7# of a cleaner namespace. These symbols used to be in
8# django/core/mail.py before the introduction of email
9# backends and the subsequent reorganization (See #10355)
10from django.core.mail.message import (
11 DEFAULT_ATTACHMENT_MIME_TYPE,
12 BadHeaderError,
13 EmailMessage,
14 EmailMultiAlternatives,
15 SafeMIMEMultipart,
16 SafeMIMEText,
17 forbid_multi_line_headers,
18 make_msgid,
19)
20from django.core.mail.utils import DNS_NAME, CachedDnsName
21from django.utils.module_loading import import_string
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 "mail_admins",
38 "mail_managers",
39]
42def get_connection(backend=None, fail_silently=False, **kwds):
43 """Load an email backend and return an instance of it.
45 If backend is None (default), use settings.EMAIL_BACKEND.
47 Both fail_silently and other keyword arguments are used in the
48 constructor of the backend.
49 """
50 klass = import_string(backend or settings.EMAIL_BACKEND)
51 return klass(fail_silently=fail_silently, **kwds)
54def send_mail(
55 subject,
56 message,
57 from_email,
58 recipient_list,
59 fail_silently=False,
60 auth_user=None,
61 auth_password=None,
62 connection=None,
63 html_message=None,
64):
65 """
66 Easy wrapper for sending a single message to a recipient list. All members
67 of the recipient list will see the other recipients in the 'To' field.
69 If from_email is None, use the DEFAULT_FROM_EMAIL setting.
70 If auth_user is None, use the EMAIL_HOST_USER setting.
71 If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
73 Note: The API for this method is frozen. New code wanting to extend the
74 functionality should use the EmailMessage class directly.
75 """
76 connection = connection or get_connection(
77 username=auth_user,
78 password=auth_password,
79 fail_silently=fail_silently,
80 )
81 mail = EmailMultiAlternatives(
82 subject, message, from_email, recipient_list, connection=connection
83 )
84 if html_message:
85 mail.attach_alternative(html_message, "text/html")
87 return mail.send()
90def send_mass_mail(
91 datatuple, fail_silently=False, auth_user=None, auth_password=None, connection=None
92):
93 """
94 Given a datatuple of (subject, message, from_email, recipient_list), send
95 each message to each recipient list. Return the number of emails sent.
97 If from_email is None, use the DEFAULT_FROM_EMAIL setting.
98 If auth_user and auth_password are set, use them to log in.
99 If auth_user is None, use the EMAIL_HOST_USER setting.
100 If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
102 Note: The API for this method is frozen. New code wanting to extend the
103 functionality should use the EmailMessage class directly.
104 """
105 connection = connection or get_connection(
106 username=auth_user,
107 password=auth_password,
108 fail_silently=fail_silently,
109 )
110 messages = [
111 EmailMessage(subject, message, sender, recipient, connection=connection)
112 for subject, message, sender, recipient in datatuple
113 ]
114 return connection.send_messages(messages)
117def mail_admins(
118 subject, message, fail_silently=False, connection=None, html_message=None
119):
120 """Send a message to the admins, as defined by the ADMINS setting."""
121 if not settings.ADMINS:
122 return
123 if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.ADMINS):
124 raise ValueError("The ADMINS setting must be a list of 2-tuples.")
125 mail = EmailMultiAlternatives(
126 "%s%s" % (settings.EMAIL_SUBJECT_PREFIX, subject),
127 message,
128 settings.SERVER_EMAIL,
129 [a[1] for a in settings.ADMINS],
130 connection=connection,
131 )
132 if html_message:
133 mail.attach_alternative(html_message, "text/html")
134 mail.send(fail_silently=fail_silently)
137def mail_managers(
138 subject, message, fail_silently=False, connection=None, html_message=None
139):
140 """Send a message to the managers, as defined by the MANAGERS setting."""
141 if not settings.MANAGERS:
142 return
143 if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.MANAGERS):
144 raise ValueError("The MANAGERS setting must be a list of 2-tuples.")
145 mail = EmailMultiAlternatives(
146 "%s%s" % (settings.EMAIL_SUBJECT_PREFIX, subject),
147 message,
148 settings.SERVER_EMAIL,
149 [a[1] for a in settings.MANAGERS],
150 connection=connection,
151 )
152 if html_message:
153 mail.attach_alternative(html_message, "text/html")
154 mail.send(fail_silently=fail_silently)