Skip to content

Commit 3784811

Browse files
medmundsnessita
authored andcommitted
Refs #35514 -- Cleaned up email docs.
* Indented get_connection() details into its function block. * Updated introductory paragraph to better reflect current capabilities. * Changed link in "Quick examples" to go to descriptive text rather than reference for send_mail(). * Avoided implying that send_mail() is only reliable "in most cases." * Improved description of development SMTP server. * Expanded information on third-party backends. * Moved "Email backends" introductory paragraph above API methods and expanded it to match section content.
1 parent 5a0c099 commit 3784811

1 file changed

Lines changed: 50 additions & 35 deletions

File tree

docs/topics/email.txt

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ Sending email
33
=============
44

55
.. module:: django.core.mail
6-
:synopsis: Helpers to easily send email.
6+
:synopsis: Helpers to send email.
77

8-
Although Python provides a mail sending interface via the :mod:`smtplib`
9-
module, Django provides a couple of light wrappers over it. These wrappers are
10-
provided to make sending email extra quick, to help test email sending during
11-
development, and to provide support for platforms that can't use SMTP.
8+
Django provides wrappers for Python's :mod:`email` and :mod:`smtplib` modules
9+
to simplify composing and sending email. Django's email framework also supports
10+
swapping in different delivery mechanisms: you can direct email to the console
11+
or a file during development, or use community-maintained solutions for sending
12+
email directly through commercial email service providers.
1213

1314
The code lives in the ``django.core.mail`` module.
1415

@@ -27,10 +28,11 @@ plain text message::
2728
["to@example.com"],
2829
)
2930

30-
When additional email sending functionality is needed, use
31-
:class:`EmailMessage` or :class:`EmailMultiAlternatives`. For example, to send
32-
a multipart email that includes both HTML and plain text versions with a
33-
specific template and custom headers, you can use the following approach::
31+
When additional email sending functionality is needed, use the
32+
:ref:`EmailMessage <topic-email-message>` or :class:`EmailMultiAlternatives`
33+
class. For example, to send a multipart email that includes both HTML and plain
34+
text versions with a specific template and custom headers, you can use the
35+
following approach::
3436

3537
from django.core.mail import EmailMultiAlternatives
3638
from django.template.loader import render_to_string
@@ -111,7 +113,7 @@ attachments and multiple content types.
111113

112114
.. function:: send_mail(subject, message, from_email, recipient_list, *, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None)
113115

114-
In most cases, you can send email using ``django.core.mail.send_mail()``.
116+
``django.core.mail.send_mail()`` sends a single email message.
115117

116118
The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters
117119
are required.
@@ -290,7 +292,7 @@ setting.
290292
Older versions ignored ``fail_silently=True`` when a ``connection``
291293
was also provided. This now raises a ``TypeError``.
292294

293-
.. _emailmessage-and-smtpconnection:
295+
.. _topic-email-message:
294296

295297
The ``EmailMessage`` class
296298
--------------------------
@@ -744,10 +746,10 @@ manually open the connection, you can control when it is closed. For example::
744746

745747
When you manually open a backend's connection, you are responsible for ensuring
746748
it gets closed. The example above actually has a bug: if an exception occurs
747-
while sending the messages, the connection will not be closed. You could fix
748-
this with a try-finally statement. Or it's often more convenient to use the
749-
backend instance as a context manager, which will automatically call ``open()``
750-
and ``close()`` as needed.
749+
while sending the messages, the connection will not be closed. This can be
750+
fixed with a ``try``-``finally`` statement, but using the backend instance as a
751+
context manager is preferable, as it automatically calls ``open()`` and
752+
``close()`` as needed.
751753

752754
This is equivalent to the previous example, but uses the backend as a
753755
:ref:`context manager <context-managers>` to avoid leaving the connection open
@@ -781,6 +783,13 @@ Email backends
781783

782784
The actual sending of an email is handled by the email backend.
783785

786+
Django comes with several email backends. With the exception of the SMTP
787+
backend, these are mainly useful during testing and development. If the
788+
built-in backends don't meet your needs there are :ref:`third-party packages
789+
<topic-third-party-email-backends>` available. You can also subclass one of the
790+
built-in backends to change its behavior, or even :ref:`write your own email
791+
backend <topic-custom-email-backend>`.
792+
784793
The email backend class has the following methods:
785794

786795
* ``open()`` instantiates a long-lived email-sending connection.
@@ -796,11 +805,6 @@ It can also be used as a context manager, which will automatically call
796805
``open()`` and ``close()`` as needed. An example is in
797806
:ref:`topics-sending-multiple-emails`.
798807

799-
Django ships with several email sending backends. With the exception of the
800-
SMTP backend (which is the default), these backends are only useful during
801-
testing and development. If you have special email sending requirements, you
802-
can :ref:`write your own email backend <topic-custom-email-backend>`.
803-
804808
.. _topic-email-smtp-backend:
805809

806810
SMTP backend
@@ -878,7 +882,7 @@ message that would be sent.
878882

879883
To specify this backend, put the following in your settings::
880884

881-
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
885+
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
882886

883887
This backend is not intended for use in production -- it is provided as a
884888
convenience that can be used during development and testing.
@@ -894,24 +898,35 @@ Dummy backend
894898
As the name suggests the dummy backend does nothing with your messages. To
895899
specify this backend, put the following in your settings::
896900

897-
EMAIL_BACKEND = "django.core.mail.backends.dummy.EmailBackend"
901+
EMAIL_BACKEND = "django.core.mail.backends.dummy.EmailBackend"
898902

899903
This backend is not intended for use in production -- it is provided as a
900904
convenience that can be used during development.
901905

906+
.. _topic-third-party-email-backends:
907+
902908
Third-party backends
903909
--------------------
904910

905-
.. admonition:: There are community-maintained solutions too!
911+
.. admonition:: There are community-maintained solutions!
906912

907913
Django has a vibrant ecosystem. There are email backends
908914
highlighted on the `Community Ecosystem`_ page. The Django Packages
909915
`Email grid`_ has even more options for you!
910916

917+
Third-party email backends are available that:
918+
919+
* Integrate directly with commercial email service providers' APIs (which often
920+
have extra functionality not available through SMTP).
921+
* Offload email sending to asynchronous task queues.
922+
* Add features to other email backends, such as enforcing do-not-send lists or
923+
logging sent messages.
924+
* Provide development and debugging tools, such as sandbox capture and
925+
in-browser email previews.
926+
911927
.. _Community Ecosystem: https://www.djangoproject.com/community/ecosystem/#email-notifications
912928
.. _Email grid: https://djangopackages.org/grids/g/email/
913929

914-
915930
.. _topic-custom-email-backend:
916931

917932
Defining a custom email backend
@@ -937,20 +952,20 @@ instance of the email backend that you can use.
937952

938953
.. function:: get_connection(backend=None, *, fail_silently=False, **kwargs)
939954

940-
By default, a call to ``get_connection()`` will return an instance of the
941-
email backend specified in :setting:`EMAIL_BACKEND`. If you specify the
942-
``backend`` argument, an instance of that backend will be instantiated.
955+
By default, a call to ``get_connection()`` will return an instance of the
956+
email backend specified in :setting:`EMAIL_BACKEND`. If you specify the
957+
``backend`` argument, an instance of that backend will be instantiated.
943958

944-
The keyword-only ``fail_silently`` argument controls how the backend should
945-
handle errors. If ``fail_silently`` is True, exceptions during the email
946-
sending process will be silently ignored.
959+
The keyword-only ``fail_silently`` argument controls how the backend should
960+
handle errors. If ``fail_silently`` is True, exceptions during the email
961+
sending process will be silently ignored.
947962

948-
All other keyword arguments are passed directly to the constructor of the
949-
email backend.
963+
All other keyword arguments are passed directly to the constructor of the
964+
email backend.
950965

951-
.. deprecated:: 6.0
966+
.. deprecated:: 6.0
952967

953-
Passing ``fail_silently`` as positional argument is deprecated.
968+
Passing ``fail_silently`` as positional argument is deprecated.
954969

955970
Configuring email for development
956971
=================================
@@ -969,7 +984,7 @@ The :ref:`file <topic-email-file-backend>` email backend can also be useful
969984
during development -- this backend dumps the contents of every SMTP connection
970985
to a file that can be inspected at your leisure.
971986

972-
Another approach is to use a "dumb" SMTP server that receives the emails
987+
Another approach is to use a mocked SMTP server that receives the emails
973988
locally and displays them to the terminal, but does not actually send
974989
anything. The :pypi:`aiosmtpd` package provides a way to accomplish this:
975990

0 commit comments

Comments
 (0)