#37167 assigned Cleanup/optimization

(Officially) allow Address objects as email addresses

Reported by: Mike Edmunds Owned by: Mike Edmunds
Component: Core (Mail) Version: 6.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django should officially support Python email.headerregistry.Address objects in django.core.mail functions and the ADMINS, MANAGERS, DEFAULT_FROM_EMAIL, and SERVER_EMAIL settings.

This actually works now (for everything except ADMINS and MANAGERS), but isn't officially documented or tested. (And it would work for ADMINS and MANAGERS except that they specifically reject any non-str values to help avoid config errors.)

Forum request: https://forum.djangoproject.com/t/ticket-34753-document-how-to-properly-escape-to-in-email-messages/43551/10.

Background

Python's email.headerregistry.Address object can be used to compose an email address from parts (a display_name and addr_spec or username and domain). And for security, it should be used when building an email address from user-supplied parts (see #34753 and PR #21467).

Python's EmailMessage class accepts Address objects in all address headers. You can a set a message's "From" header to an Address, or its "Cc" header to a list of Address objects. (You can also set a "Date" header to a datetime object, etc.)

Django's EmailMessage class accidentally supports Address objects, in all headers since 6.0 and in all but "From" since at least 5.2 (maybe earlier). It currently converts the address objects to strings, and then Python parses them back into address objects.

Proposal

  • Update the ADMINS and MANAGERS recipient-type check in django.core.mail._send_server_message() to allow Address.
  • Change EmailMessage.message() and _set_list_header_if_not_empty() to avoid casting an Address to a str just so Python can parse it back again. (The cast is there primarily for lazy strings.)
  • Add tests!

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top