Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
'<user@example.com>')
# Already angle-bracketed and valid.
self.assertEqual(smtplib.quoteaddr('<user@example.com>'),
'<user@example.com>')
# 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('<user@example.com')
self.assertTrue(result.startswith('<') and result.endswith('>'), result)

def testBasic1(self):
mock_socket.reply_with(b"220 Hola mundo")
# connects
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading