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
5 changes: 3 additions & 2 deletions Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ 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.
if addrstring.strip().startswith('<'):
return addrstring
if addrstring.strip().endswith('>'):
return addrstring
return "<%s>" % addrstring
return "<%s>" % addr

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def testQuoteData(self):
expected = "abc\r\n..jkl\r\nfoo\r\n...blue"
self.assertEqual(expected, smtplib.quotedata(teststr))

def testQuoteAddr(self):
self.assertEqual(smtplib.quoteaddr('user@example.com'),
'<user@example.com>')
self.assertEqual(smtplib.quoteaddr('<user@example.com>'),
'<user@example.com>')
self.assertEqual(smtplib.quoteaddr(''), '<>')

def testQuoteAddrMalformedAngleBracket(self):
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