From fc8b6c8e1885224038093fd8e76e633756ceb4e7 Mon Sep 17 00:00:00 2001 From: Stefan Zetzsche Date: Sun, 22 Feb 2026 14:55:34 +0000 Subject: [PATCH 1/7] gh-XXXXX: Fix smtplib.quoteaddr() returning malformed address for '<' Input starting with '<' but missing closing '>' was returned verbatim. Ensure the result always ends with '>'. --- Lib/smtplib.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 72093f7f8b0f2d..e33f72efa98754 100644 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -150,7 +150,9 @@ def quoteaddr(addrstring): if (displayname, addr) == ('', ''): # parseaddr couldn't parse it, use it as is and hope for the best. if addrstring.strip().startswith('<'): - return addrstring + if addrstring.strip().endswith('>'): + return addrstring + return addrstring.strip() + '>' return "<%s>" % addrstring return "<%s>" % addr From 660ee1217b44d401d57d4df6a6b1e3a46baf0154 Mon Sep 17 00:00:00 2001 From: Stefan Zetzsche Date: Thu, 5 Mar 2026 15:03:48 +0000 Subject: [PATCH 2/7] test(smtplib): Add tests for quoteaddr angle-bracket handling Add testQuoteAddr for basic quoteaddr behavior and testQuoteAddrMalformedAngleBracket for inputs starting with '<' but missing closing '>'. The latter fails without the fix. --- Lib/test/test_smtplib.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index b8aac8c20202a2..fac8323d75953c 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -77,6 +77,26 @@ def testQuoteData(self): expected = "abc\r\n..jkl\r\nfoo\r\n...blue" self.assertEqual(expected, smtplib.quotedata(teststr)) + def testQuoteAddr(self): + # Standard address is wrapped in angle brackets. + self.assertEqual(smtplib.quoteaddr('user@example.com'), + '') + # Already angle-bracketed and valid. + self.assertEqual(smtplib.quoteaddr(''), + '') + # Empty string produces empty angle brackets. + self.assertEqual(smtplib.quoteaddr(''), '<>') + + def testQuoteAddrMalformedAngleBracket(self): + # Inputs starting with '<' but missing closing '>' must still + # produce output that ends with '>'. + result = smtplib.quoteaddr('<') + self.assertTrue(result.startswith('<') and result.endswith('>'), result) + result = smtplib.quoteaddr('< ') + self.assertTrue(result.startswith('<') and result.endswith('>'), result) + result = smtplib.quoteaddr(''), result) + def testBasic1(self): mock_socket.reply_with(b"220 Hola mundo") # connects From a95680f113d42332f9a53ab294793b08095ab3bd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 5 Mar 2026 15:20:19 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-03-05-15-20-18.gh-issue-145552.80p_Cr.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-03-05-15-20-18.gh-issue-145552.80p_Cr.rst diff --git a/Misc/NEWS.d/next/Library/2026-03-05-15-20-18.gh-issue-145552.80p_Cr.rst b/Misc/NEWS.d/next/Library/2026-03-05-15-20-18.gh-issue-145552.80p_Cr.rst new file mode 100644 index 00000000000000..8c074d08e79380 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-05-15-20-18.gh-issue-145552.80p_Cr.rst @@ -0,0 +1 @@ +Fix ``smtplib.quoteaddr()`` to ensure the returned address always has a closing ``>`` when the input starts with ``<`` but ``email.utils.parseaddr()`` fails to parse it. From 2378a955a3552a32864d9c5db9ad593c01185cec Mon Sep 17 00:00:00 2001 From: Stefan Zetzsche Date: Tue, 10 Mar 2026 17:25:21 +0000 Subject: [PATCH 4/7] Simplify fix: remove startswith check, restore unconditional wrapping --- Lib/smtplib.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/smtplib.py b/Lib/smtplib.py index e33f72efa98754..a99c80742b8ea2 100644 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -149,10 +149,6 @@ def quoteaddr(addrstring): displayname, addr = email.utils.parseaddr(addrstring) if (displayname, addr) == ('', ''): # parseaddr couldn't parse it, use it as is and hope for the best. - if addrstring.strip().startswith('<'): - if addrstring.strip().endswith('>'): - return addrstring - return addrstring.strip() + '>' return "<%s>" % addrstring return "<%s>" % addr From 68a384f846e345d4cf6ddf3a684960da2fd23aab Mon Sep 17 00:00:00 2001 From: Stefan Zetzsche Date: Tue, 10 Mar 2026 17:54:56 +0000 Subject: [PATCH 5/7] Update comment to reflect unconditional wrapping behavior --- Lib/smtplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/smtplib.py b/Lib/smtplib.py index a99c80742b8ea2..4873477ba9ea34 100644 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -148,7 +148,7 @@ def quoteaddr(addrstring): """ displayname, addr = email.utils.parseaddr(addrstring) if (displayname, addr) == ('', ''): - # parseaddr couldn't parse it, use it as is and hope for the best. + # parseaddr couldn't parse it, wrap it in angle brackets. return "<%s>" % addrstring return "<%s>" % addr From 55fa7036f0523c0b593cde1550e40fe05ecc21f4 Mon Sep 17 00:00:00 2001 From: Stefan Zetzsche Date: Tue, 10 Mar 2026 18:22:18 +0000 Subject: [PATCH 6/7] Preserve already-bracketed input (e.g. null sender <>), wrap everything else --- Lib/smtplib.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 4873477ba9ea34..fd8e562a69e40f 100644 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -149,6 +149,9 @@ def quoteaddr(addrstring): displayname, addr = email.utils.parseaddr(addrstring) if (displayname, addr) == ('', ''): # parseaddr couldn't parse it, wrap it in angle brackets. + if addrstring.strip().startswith('<'): + if addrstring.strip().endswith('>'): + return addrstring return "<%s>" % addrstring return "<%s>" % addr From 29d8166928c0aa1307a9ab6968360e06685b2ac7 Mon Sep 17 00:00:00 2001 From: Stefan Zetzsche Date: Thu, 12 Mar 2026 14:54:47 +0000 Subject: [PATCH 7/7] Remove unnecessary comments from test cases --- Lib/test/test_smtplib.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index fac8323d75953c..78239403814ae1 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -78,18 +78,13 @@ def testQuoteData(self): self.assertEqual(expected, smtplib.quotedata(teststr)) def testQuoteAddr(self): - # Standard address is wrapped in angle brackets. self.assertEqual(smtplib.quoteaddr('user@example.com'), '') - # Already angle-bracketed and valid. self.assertEqual(smtplib.quoteaddr(''), '') - # Empty string produces empty angle brackets. self.assertEqual(smtplib.quoteaddr(''), '<>') def testQuoteAddrMalformedAngleBracket(self): - # Inputs starting with '<' but missing closing '>' must still - # produce output that ends with '>'. result = smtplib.quoteaddr('<') self.assertTrue(result.startswith('<') and result.endswith('>'), result) result = smtplib.quoteaddr('< ')