Skip to content

Commit

Permalink
Fixed #35033, Refs #28912 -- Fixed repeated headers in EmailMessage.
Browse files Browse the repository at this point in the history
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 b03d500.
  • Loading branch information
medmunds authored and sarahboyce committed Jul 11, 2024
1 parent c6d1f98 commit b909853
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
16 changes: 8 additions & 8 deletions django/core/mail/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ def message(self):
# Use cached DNS_NAME for performance
msg["Message-ID"] = make_msgid(domain=DNS_NAME)
for name, value in self.extra_headers.items():
if name.lower() != "from": # From is already handled
# Avoid headers handled above.
if name.lower() not in {"from", "to", "cc", "reply-to"}:
msg[name] = value
return msg

Expand Down Expand Up @@ -427,14 +428,13 @@ def _create_attachment(self, filename, content, mimetype=None):
def _set_list_header_if_not_empty(self, msg, header, values):
"""
Set msg's header, either from self.extra_headers, if present, or from
the values argument.
the values argument if not empty.
"""
if values:
try:
value = self.extra_headers[header]
except KeyError:
value = ", ".join(str(v) for v in values)
msg[header] = value
try:
msg[header] = self.extra_headers[header]
except KeyError:
if values:
msg[header] = ", ".join(str(v) for v in values)


class EmailMultiAlternatives(EmailMessage):
Expand Down
21 changes: 11 additions & 10 deletions tests/mail/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test_cc_headers(self):
cc=["[email protected]"],
headers={"Cc": "[email protected]"},
).message()
self.assertEqual(message["Cc"], "[email protected]")
self.assertEqual(message.get_all("Cc"), ["[email protected]"])

def test_cc_in_headers_only(self):
message = EmailMessage(
Expand All @@ -233,7 +233,7 @@ def test_cc_in_headers_only(self):
["[email protected]"],
headers={"Cc": "[email protected]"},
).message()
self.assertEqual(message["Cc"], "[email protected]")
self.assertEqual(message.get_all("Cc"), ["[email protected]"])

def test_reply_to(self):
email = EmailMessage(
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_from_header(self):
headers={"From": "[email protected]"},
)
message = email.message()
self.assertEqual(message["From"], "[email protected]")
self.assertEqual(message.get_all("From"), ["[email protected]"])

def test_to_header(self):
"""
Expand All @@ -393,7 +393,7 @@ def test_to_header(self):
headers={"To": "[email protected]"},
)
message = email.message()
self.assertEqual(message["To"], "[email protected]")
self.assertEqual(message.get_all("To"), ["[email protected]"])
self.assertEqual(
email.to, ["[email protected]", "[email protected]"]
)
Expand All @@ -408,7 +408,8 @@ def test_to_header(self):
)
message = email.message()
self.assertEqual(
message["To"], "[email protected], [email protected]"
message.get_all("To"),
["[email protected], [email protected]"],
)
self.assertEqual(
email.to, ["[email protected]", "[email protected]"]
Expand All @@ -421,7 +422,7 @@ def test_to_in_headers_only(self):
"[email protected]",
headers={"To": "[email protected]"},
).message()
self.assertEqual(message["To"], "[email protected]")
self.assertEqual(message.get_all("To"), ["[email protected]"])

def test_reply_to_header(self):
"""
Expand All @@ -436,7 +437,7 @@ def test_reply_to_header(self):
headers={"Reply-To": "[email protected]"},
)
message = email.message()
self.assertEqual(message["Reply-To"], "[email protected]")
self.assertEqual(message.get_all("Reply-To"), ["[email protected]"])

def test_reply_to_in_headers_only(self):
message = EmailMessage(
Expand All @@ -446,7 +447,7 @@ def test_reply_to_in_headers_only(self):
["[email protected]"],
headers={"Reply-To": "[email protected]"},
).message()
self.assertEqual(message["Reply-To"], "[email protected]")
self.assertEqual(message.get_all("Reply-To"), ["[email protected]"])

def test_multiple_message_call(self):
"""
Expand All @@ -461,9 +462,9 @@ def test_multiple_message_call(self):
headers={"From": "[email protected]"},
)
message = email.message()
self.assertEqual(message["From"], "[email protected]")
self.assertEqual(message.get_all("From"), ["[email protected]"])
message = email.message()
self.assertEqual(message["From"], "[email protected]")
self.assertEqual(message.get_all("From"), ["[email protected]"])

def test_unicode_address_header(self):
"""
Expand Down

0 comments on commit b909853

Please sign in to comment.