Skip to content

Commit bd127b1

Browse files
bsiemned-deily
authored andcommitted
[3.8] bpo-37482: Fix email address name with encoded words and special chars (GH-14561) (GH-15380)
Special characters in email address header display names are normally put within double quotes. However, encoded words (=?charset?x?...?=) are not allowed withing double quotes. When the header contains a word with special characters and another word that must be encoded, the first one must also be encoded. In the next example, the display name in the From header is quoted and therefore the comma is allowed; in the To header, the comma is not within quotes and not encoded, which is not allowed and therefore rejected by some mail servers. From: "Foo Bar, France" <[email protected]> To: Foo Bar, =?utf-8?q?Espa=C3=B1a?= <[email protected]> https://bugs.python.org/issue37482 (cherry picked from commit df0c21f) Co-authored-by: bsiem <[email protected]>
1 parent 23985c6 commit bd127b1

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Lib/email/_header_value_parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,9 @@ def _refold_parse_tree(parse_tree, *, policy):
27222722
wrap_as_ew_blocked -= 1
27232723
continue
27242724
tstr = str(part)
2725+
if part.token_type == 'ptext' and set(tstr) & SPECIALS:
2726+
# Encode if tstr contains special characters.
2727+
want_encoding = True
27252728
try:
27262729
tstr.encode(encoding)
27272730
charset = encoding

Lib/test/test_email/test_headerregistry.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,30 @@ def test_set_message_header_from_group(self):
15271527

15281528
class TestFolding(TestHeaderBase):
15291529

1530+
def test_address_display_names(self):
1531+
"""Test the folding and encoding of address headers."""
1532+
for name, result in (
1533+
('Foo Bar, France', '"Foo Bar, France"'),
1534+
('Foo Bar (France)', '"Foo Bar (France)"'),
1535+
('Foo Bar, España', 'Foo =?utf-8?q?Bar=2C_Espa=C3=B1a?='),
1536+
('Foo Bar (España)', 'Foo Bar =?utf-8?b?KEVzcGHDsWEp?='),
1537+
('Foo, Bar España', '=?utf-8?q?Foo=2C_Bar_Espa=C3=B1a?='),
1538+
('Foo, Bar [España]', '=?utf-8?q?Foo=2C_Bar_=5BEspa=C3=B1a=5D?='),
1539+
('Foo Bär, France', 'Foo =?utf-8?q?B=C3=A4r=2C?= France'),
1540+
('Foo Bär <France>', 'Foo =?utf-8?q?B=C3=A4r_=3CFrance=3E?='),
1541+
(
1542+
'Lôrem ipsum dôlôr sit amet, cônsectetuer adipiscing. '
1543+
'Suspendisse pôtenti. Aliquam nibh. Suspendisse pôtenti.',
1544+
'=?utf-8?q?L=C3=B4rem_ipsum_d=C3=B4l=C3=B4r_sit_amet=2C_c'
1545+
'=C3=B4nsectetuer?=\n =?utf-8?q?adipiscing=2E_Suspendisse'
1546+
'_p=C3=B4tenti=2E_Aliquam_nibh=2E?=\n Suspendisse =?utf-8'
1547+
'?q?p=C3=B4tenti=2E?=',
1548+
),
1549+
):
1550+
h = self.make_header('To', Address(name, addr_spec='[email protected]'))
1551+
self.assertEqual(h.fold(policy=policy.default),
1552+
'To: %s <[email protected]>\n' % result)
1553+
15301554
def test_short_unstructured(self):
15311555
h = self.make_header('subject', 'this is a test')
15321556
self.assertEqual(h.fold(policy=policy.default),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix serialization of display name in originator or destination address fields with both encoded words and special chars.

0 commit comments

Comments
 (0)