You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UnicodeEncodeError raise from smtplib.verify() method #80274
>>> import smtplib
>>> s = smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
>>> s.verify('你好@outlook.com')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/windson/learn/cpython/Lib/smtplib.py", line 577, in verify
self.putcmd("vrfy", _addr_only(address))
File "/Users/windson/learn/cpython/Lib/smtplib.py", line 367, in putcmd
self.send(str)
File "/Users/windson/learn/cpython/Lib/smtplib.py", line 352, in send
s = s.encode(self.command_encoding)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-6: ordinal not in range(128)
msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters. A byte string is not modified.
So we can't send non-ASCII msg using send_mail(), is this expected behavior?
Barry: would you mind to have a look at this issue? It seems like smtplib encodes Unicode commands to ASCII, whereas verify() argument (SMTP "VRFY" command) is an hostname and hostname can be non-ASCII ("punycode" encoding, RFC 3492).
Punycode only applies to the domain part (RFC 3492 #1.2).
putcmd() encodes every command with self.command_encoding (ascii), mail uses UTF-8 if 'smtputf8' extension is supported by SMTP server and caller passed 'SMTPUTF8' option.
I will mirror what send_message() does for non-ASCII addresses:
Non-ASCII argument and server advertises SMTPUTF8 - send the command as UTF-8 with SMTPUTF8.
Non-ASCII argument and no SMTPUTF8 support - raise SMTPNotSupportedError('SMTPUTF8 not supported by server') instead of UnicodeEncodeError.
Looking at the issue more closely, I used explicit flag rather than auto-detection, mirroring mail().
For a non-ASCII address the flag is clearly needed, but it also matters for an ASCII address:
the SMTPUTF8 parameter tells the server that UTF-8 is acceptable in the reply, so VRFY joe@example.com SMTPUTF8 can return 250 Jő Smith <joe@example.com>, where without the parameter the server must answer 252 or 550 instead.
The PR adds a smtputf8 parameter to verify and expn.
When true, the address is sent as UTF-8 with the SMTPUTF8 parameter (RFC 6531 #3.7.4.2), EHLO is negotiated first if needed. SMTPNotSupportedError is raised if the server does not advertise the extension.
Without the flag nothing changes
I also documented expn. It was missed from the doc file but as it is available we need to have it in the Doc.
Activity
Windsooon commentedon Feb 23, 2019
AFAIK, the email address should support non-ASCII character (from https://stackoverflow.com/questions/760150/can-an-email-address-contain-international-non-english-characters and SMTPUTF8
option from https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail)
I found this issue when I updating https://github.com/python/cpython/pull/8938/files
Windsooon commentedon Feb 23, 2019
Btw, from the docs https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail
So we can't send non-ASCII msg using send_mail(), is this expected behavior?
vstinner commentedon Feb 25, 2019
Barry: would you mind to have a look at this issue? It seems like smtplib encodes Unicode commands to ASCII, whereas verify() argument (SMTP "VRFY" command) is an hostname and hostname can be non-ASCII ("punycode" encoding, RFC 3492).
Windsooon commentedon Mar 27, 2020
Any update?
vasiliyk commentedon Nov 6, 2026
Still reproducible on 3.16.0a1+dev.
Punycode only applies to the domain part (RFC 3492 #1.2).
putcmd()encodes every command withself.command_encoding(ascii),mailuses UTF-8 if 'smtputf8' extension is supported by SMTP server and caller passed 'SMTPUTF8' option.I will mirror what
send_message()does for non-ASCII addresses:Non-ASCII argument and server advertises
SMTPUTF8- send the command as UTF-8 withSMTPUTF8.Non-ASCII argument and no
SMTPUTF8support - raiseSMTPNotSupportedError('SMTPUTF8 not supported by server')instead ofUnicodeEncodeError.vasiliyk commentedon Nov 7, 2026
Looking at the issue more closely, I used explicit flag rather than auto-detection, mirroring
mail().For a non-ASCII address the flag is clearly needed, but it also matters for an ASCII address:
the
SMTPUTF8parameter tells the server that UTF-8 is acceptable in the reply, soVRFY joe@example.com SMTPUTF8can return250 Jő Smith <joe@example.com>, where without the parameter the server must answer 252 or 550 instead.The PR adds a
smtputf8parameter toverifyandexpn.SMTPUTF8parameter (RFC 6531 #3.7.4.2), EHLO is negotiated first if needed.SMTPNotSupportedErroris raised if the server does not advertise the extension.I also documented
expn. It was missed from the doc file but as it is available we need to have it in the Doc.Merge branch 'main' into pythongh-80274-unicode