Opened 7 years ago

Closed 7 years ago

Last modified 5 months ago

#28912 closed Cleanup/optimization (fixed)

EmailMessage.message() should not set a blank To: field when EmailMessage.to is empty

Reported by: Jon Dufresne Owned by: nobody
Component: Core (Mail) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

EmailMessage.message() always sets a To: header, see:

https://github.com/django/django/blob/a9e5ac823df8ba8b786b6450c967ca378c008d0e/django/core/mail/message.py#L257

In my project, emails are sent to Cc: or Bcc: recipients without a To: recipient.

Some mail backends are very strict about allowed headers and their values. For example, Amazon Simple Email Service (SES) validates all recipient headers to contain valid email addresses. It fails without delivery if any header contains an invalid address. For some details, see:

https://boto3.readthedocs.io/en/latest/reference/services/ses.html#SES.Client.send_raw_email

The message must include at least one recipient email address. The recipient address can be a To: address, a CC: address, or a BCC: address. If a recipient email address is invalid (that is, it is not in the format UserName@[SubDomain.]Domain.TopLevelDomain ), the entire message will be rejected, even if the message contains other recipients that are valid.

SES considers blank as invalid. When sending an email serialized by EmailMessage.message() to Cc: or Bcc: only addresses, SES fails with an error. To: avoid the error, Django can avoid setting the To header when EmailMessage.to is empty.

Change History (5)

comment:1 by Jon Dufresne, 7 years ago

Has patch: set

comment:2 by Claude Paroz, 7 years ago

Triage Stage: UnreviewedAccepted

Makes sense.

comment:3 by Claude Paroz, 7 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In da82939:

Fixed #28912 -- Made EmailMessage.message() omit an empty To header.

comment:5 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

In b909853:

Fixed #35033, Refs #28912 -- Fixed repeated headers in EmailMessage.

Fixed a regression which would cause multiple To, Cc, and
Reply-To headers in the result of EmailMessage.message() if
values were supplied for both to/cc/reply_to and the
corresponding extra_headers fields.

Updated related tests to check the generated message() has
exactly one of each expected header using get_all().

Regression in b03d5002955256c4b3ed7cfae5150eb79c0eb97e.

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