Skip to content

Commit 1c700e4

Browse files
Issue 146: Fix comma in encoded address header (#147)
* Fix comma in encoded address header (issue 146), add test for it and fix issue136 test * pre-commit: improve formatting and readability in MailParser and tests --------- Co-authored-by: Fedele Mantuano <mantuano.fedele@gmail.com>
1 parent 025f937 commit 1c700e4

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

src/mailparser/core.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,13 +556,22 @@ def __getattr__(self, name):
556556

557557
# object headers
558558
elif name_header in ADDRESSES_HEADERS:
559-
h = decode_header_part(self.message.get(name_header, str()))
560-
h_parsed = email.utils.getaddresses([h], strict=True)
561-
return (
562-
h_parsed
563-
if h_parsed != [("", "")]
564-
else email.utils.getaddresses([h], strict=False)
565-
)
559+
raw_header = self.message.get(name_header, "")
560+
# parse before decoding
561+
parsed_addresses = email.utils.getaddresses([raw_header], strict=True)
562+
563+
# decoded addresses
564+
return [
565+
(
566+
(
567+
""
568+
if (decoded_name := decode_header_part(name)) == email_addr
569+
else decoded_name
570+
),
571+
email_addr,
572+
)
573+
for name, email_addr in parsed_addresses
574+
]
566575

567576
# others headers
568577
else:

tests/mails/mail_test_18

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
MIME-Version: 1.0
2+
Date: Mon, 3 Nov 2025 18:23:00 +0100
3+
Content-Type: text/plain; charset=utf-8
4+
Subject: Test for Comma and Name Bugs
5+
From: =?iso-8859-1?Q?Last=DFlName=2C_FirstName?= <comma.name@example.com>
6+
To: =?UTF-8?B?dG9ueS5zdGFya0BleGFtcGxlLmNvbQ==?= <tony.stark@example.com>
7+
Cc: simple@example.net, =?UTF-8?Q?John_=22Johnny=22_Doe?= <john.doe@example.com>
8+
9+
This is a test email body.
10+
11+
It validates two specific fixes:
12+
1. The 'From' header contains a comma in the encoded display name.
13+
2. The 'To' header has an encoded display name that matches the email address.
14+
3. The 'Cc' header has a mix of simple and encoded addresses.

tests/test_mail_parser.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
mail_test_15 = os.path.join(base_path, "mails", "mail_test_15")
6262
mail_test_16 = os.path.join(base_path, "mails", "mail_test_16")
6363
mail_test_17 = os.path.join(base_path, "mails", "mail_test_17")
64+
mail_test_18 = os.path.join(base_path, "mails", "mail_test_18")
6465
mail_malformed_1 = os.path.join(base_path, "mails", "mail_malformed_1")
6566
mail_malformed_2 = os.path.join(base_path, "mails", "mail_malformed_2")
6667
mail_malformed_3 = os.path.join(base_path, "mails", "mail_malformed_3")
@@ -700,7 +701,6 @@ def test_issue_136(self):
700701
mail = mailparser.parse_from_file(mail_test_17)
701702
assert mail.from_ == [
702703
("", "notificaccion-clientes@bbva.mx"),
703-
("", "notificaccion-clientes@bbva.mx"),
704704
]
705705

706706
def test_str_method_with_message(self):
@@ -938,3 +938,18 @@ def test_text_plain_8bit_encoding(self):
938938

939939
mail = mailparser.parse_from_string(raw_mail)
940940
self.assertIn("This is plain text", mail.body)
941+
942+
def test_comma_in_name(self):
943+
"""
944+
Tests the fixes for both the 'comma-in-encoded-name' issue and the
945+
'encoded-name-equals-email' issue (from test_issue_136).
946+
"""
947+
948+
mail = mailparser.parse_from_file(mail_test_18)
949+
950+
assert mail.from_ == [("LastßlName, FirstName", "comma.name@example.com")]
951+
assert mail.to == [("", "tony.stark@example.com")]
952+
assert mail.cc == [
953+
("", "simple@example.net"),
954+
('John "Johnny" Doe', "john.doe@example.com"),
955+
]

0 commit comments

Comments
 (0)