Skip to content

UnicodeEncodeError raise from smtplib.verify() method #80274

Open

Description

@Windsooon
mannequin
BPO 36093
Nosy @warsaw, @ezio-melotti, @Windsooon

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2019-02-23.07:59:18.247>
labels = ['3.8', 'type-bug', '3.7', 'expert-unicode']
title = 'UnicodeEncodeError raise from smtplib.verify() method'
updated_at = <Date 2020-03-27.11:16:23.501>
user = 'https://github.com/Windsooon'

bugs.python.org fields:

activity = <Date 2020-03-27.11:16:23.501>
actor = 'vstinner'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Unicode']
creation = <Date 2019-02-23.07:59:18.247>
creator = 'Windson Yang'
dependencies = []
files = []
hgrepos = []
issue_num = 36093
keywords = []
message_count = 4.0
messages = ['336374', '336375', '336509', '365140']
nosy_count = 3.0
nosy_names = ['barry', 'ezio.melotti', 'Windson Yang']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue36093'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

Linked PRs

Activity

  1. Windsooon commented on Feb 23, 2019

    Windsooonmannequin
    MannequinAuthor

    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)

    >>> 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)

    I found this issue when I updating https://github.com/python/cpython/pull/8938/files

  2. Windsooon commented on Feb 23, 2019

    Windsooonmannequin
    MannequinAuthor

    Btw, from the docs https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail

    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?

  3. vstinner commented on Feb 25, 2019

    @vstinner
    Member

    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).

  4. Windsooon commented on Mar 27, 2020

    Windsooonmannequin
    MannequinAuthor

    Any update?

  5. transferred this issue fromon Apr 10, 2022
  6. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 23, 2023
  7. vasiliyk commented on Nov 6, 2026

    @vasiliyk
    Contributor

    Still reproducible on 3.16.0a1+dev.

    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.

  8. vasiliyk commented on Nov 7, 2026

    @vasiliyk
    Contributor

    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.

  9. added a commit that references this issue on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.7 (EOL)end of life3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytopic-emailtopic-unicodetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions