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 TemplateEmail,
19 forbid_multi_line_headers,
20 make_msgid,
21)
22from .utils import DNS_NAME, CachedDnsName
23
24__all__ = [
25 "CachedDnsName",
26 "DNS_NAME",
27 "EmailMessage",
28 "EmailMultiAlternatives",
29 "TemplateEmail",
30 "SafeMIMEText",
31 "SafeMIMEMultipart",
32 "DEFAULT_ATTACHMENT_MIME_TYPE",
33 "make_msgid",
34 "BadHeaderError",
35 "forbid_multi_line_headers",
36 "get_connection",
37 "send_mail",
38 "send_mass_mail",
39]
40
41
42def get_connection(backend=None, fail_silently=False, **kwds):
43 """Load an email backend and return an instance of it.
44
45 If backend is None (default), use settings.EMAIL_BACKEND.
46
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)
52
53
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.
68
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.
72
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")
86
87 return mail.send()
88
89
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.
96
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.
101
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)