Skip to content

Commit

Permalink
Added example of email sending with additional capabilities to docs/t…
Browse files Browse the repository at this point in the history
…opics/email.txt.

Co-authored-by: Mike Edmunds <[email protected]>
  • Loading branch information
GitRon and medmunds authored Sep 11, 2024
1 parent 96c9907 commit f481321
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions docs/topics/email.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ development, and to provide support for platforms that can't use SMTP.

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

Quick example
=============
Quick examples
==============

In two lines::
Use :func:`send_mail` for straightforward email sending. For example, to send a
plain text message::

from django.core.mail import send_mail

Expand All @@ -27,6 +28,39 @@ In two lines::
fail_silently=False,
)

When additional email sending functionality is needed, use
:class:`EmailMessage` or :class:`EmailMultiAlternatives`. For example, to send
a multipart email that includes both HTML and plain text versions with a
specific template and custom headers, you can use the following approach::

from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string

# First, render the plain text content.
text_content = render_to_string(
"templates/emails/my_email.txt",
context={"my_variable": 42},
)

# Secondly, render the HTML content.
html_content = render_to_string(
"templates/emails/my_email.html",
context={"my_variable": 42},
)

# Then, create a multipart email instance.
msg = EmailMultiAlternatives(
"Subject here",
text_content,
"[email protected]",
["[email protected]"],
headers={"List-Unsubscribe": "<mailto:[email protected]>"},
)

# Lastly, attach the HTML content to the email instance and send.
msg.attach_alternative(html_content, "text/html")
msg.send()

Mail is sent using the SMTP host and port specified in the
:setting:`EMAIL_HOST` and :setting:`EMAIL_PORT` settings. The
:setting:`EMAIL_HOST_USER` and :setting:`EMAIL_HOST_PASSWORD` settings, if
Expand Down

0 comments on commit f481321

Please sign in to comment.