Skip to content

Commit 8f36420

Browse files
medmundsnessita
authored andcommitted
Refs #35514 -- Moved EmailMessage class up in email docs.
Moved the "Preventing header injection" discussion below sections on EmailMessage and related classes.
1 parent 89d2298 commit 8f36420

1 file changed

Lines changed: 48 additions & 48 deletions

File tree

docs/topics/email.txt

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -265,54 +265,6 @@ setting.
265265
Older versions ignored ``fail_silently=True`` when a ``connection``
266266
was also provided. This now raises a ``TypeError``.
267267

268-
Preventing header injection
269-
---------------------------
270-
271-
`Header injection`_ is a security exploit in which an attacker inserts extra
272-
email headers to control the "To:" and "From:" in email messages that your
273-
scripts generate.
274-
275-
The Django email functions outlined above all protect against header injection
276-
by forbidding newlines in header values. If any ``subject``, ``from_email`` or
277-
``recipient_list`` contains a newline (in either Unix, Windows or Mac style),
278-
the email function (e.g. :func:`send_mail`) will raise :exc:`ValueError` and,
279-
hence, will not send the email. It's your responsibility to validate all data
280-
before passing it to the email functions.
281-
282-
If a ``message`` contains headers at the start of the string, the headers will
283-
be printed as the first bit of the email message.
284-
285-
Here's an example view that takes a ``subject``, ``message`` and ``from_email``
286-
from the request's POST data, sends that to ``admin@example.com`` and redirects
287-
to "/contact/thanks/" when it's done::
288-
289-
from django.core.mail import send_mail
290-
from django.http import HttpResponse, HttpResponseRedirect
291-
292-
293-
def send_email(request):
294-
subject = request.POST.get("subject", "")
295-
message = request.POST.get("message", "")
296-
from_email = request.POST.get("from_email", "")
297-
if subject and message and from_email:
298-
try:
299-
send_mail(subject, message, from_email, ["admin@example.com"])
300-
except ValueError:
301-
return HttpResponse("Invalid header found.")
302-
return HttpResponseRedirect("/contact/thanks/")
303-
else:
304-
# In reality we'd use a form class
305-
# to get proper validation errors.
306-
return HttpResponse("Make sure all fields are entered and valid.")
307-
308-
309-
.. versionchanged:: 6.0
310-
311-
Older versions raised ``django.core.mail.BadHeaderError`` for some
312-
invalid headers. This has been replaced with :exc:`!ValueError`.
313-
314-
.. _Header injection: http://www.nyphp.org/phundamentals/8_Preventing-Email-Header-Injection.html
315-
316268
.. _emailmessage-and-smtpconnection:
317269

318270
The ``EmailMessage`` class
@@ -649,6 +601,54 @@ example::
649601
msg.content_subtype = "html" # Main content is now text/html
650602
msg.send()
651603

604+
Preventing header injection
605+
---------------------------
606+
607+
`Header injection`_ is a security exploit in which an attacker inserts extra
608+
email headers to control the "To:" and "From:" in email messages that your
609+
scripts generate.
610+
611+
The Django email functions outlined above all protect against header injection
612+
by forbidding newlines in header values. If any ``subject``, ``from_email`` or
613+
``recipient_list`` contains a newline (in either Unix, Windows or Mac style),
614+
the email function (e.g. :func:`send_mail`) will raise :exc:`ValueError` and,
615+
hence, will not send the email. It's your responsibility to validate all data
616+
before passing it to the email functions.
617+
618+
If a ``message`` contains headers at the start of the string, the headers will
619+
be printed as the first bit of the email message.
620+
621+
Here's an example view that takes a ``subject``, ``message`` and ``from_email``
622+
from the request's POST data, sends that to ``admin@example.com`` and redirects
623+
to "/contact/thanks/" when it's done::
624+
625+
from django.core.mail import send_mail
626+
from django.http import HttpResponse, HttpResponseRedirect
627+
628+
629+
def send_email(request):
630+
subject = request.POST.get("subject", "")
631+
message = request.POST.get("message", "")
632+
from_email = request.POST.get("from_email", "")
633+
if subject and message and from_email:
634+
try:
635+
send_mail(subject, message, from_email, ["admin@example.com"])
636+
except ValueError:
637+
return HttpResponse("Invalid header found.")
638+
return HttpResponseRedirect("/contact/thanks/")
639+
else:
640+
# In reality we'd use a form class
641+
# to get proper validation errors.
642+
return HttpResponse("Make sure all fields are entered and valid.")
643+
644+
645+
.. versionchanged:: 6.0
646+
647+
Older versions raised ``django.core.mail.BadHeaderError`` for some
648+
invalid headers. This has been replaced with :exc:`!ValueError`.
649+
650+
.. _Header injection: http://www.nyphp.org/phundamentals/8_Preventing-Email-Header-Injection.html
651+
652652
.. _topic-email-backends:
653653

654654
Email backends

0 commit comments

Comments
 (0)