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('>'):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in responding.

I'd prefer to do addrstring.strip() == '<>', which theoretically matches the old behavior. (I say theoretically because the code the old version of this code was calling has moved on as well and I don't feel like going down that rabbit hole.) In any case, it should be correct: only <> is a special case per RFC 5321; we otherwise don't want to do more to fix user errors than to ensure that the provided originator address is sorrounded by the angle quotes.

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