[New-bugs-announce] [issue41023] smtplib does not handle Unicode characters

Jay Patel report at bugs.python.org
Thu Jun 18 05:45:34 EDT 2020


New submission from Jay Patel <jay.patel at crestdatasys.com>:

According to the user requirements, I need to send an email, which is provided as a raw email, i.e., the contents of email are provided in form of headers. To accomplish this I am using the methods provided in the "send_rawemail_demo.py" file (attached below).
The smtplib library works fine when providing only 'ascii' characters in the 'raw_email' variable. But, when I provide any Unicode characters either in the Subject or Body of the email, then the sendmail method of the smtplib library fails with the following message:
UnicodeEncodeError 'ascii' codec can't encode characters in position 123-124: ordinal not in range(128)
I tried providing the mail_options=["SMTPUTF-8"] in the sendmail method (On line no. 72 in the send_rawemail_demo.py file), but then it fails (even for the 'ascii' characters) with the exception as SMTPSenderRefused.
I have faced the same issue on Python 3.6. 
The sendmail method of the SMTP class encodes the message using 'ascii' as:
    if isinstance(msg, str):
        msg = _fix_eols(msg).encode('ascii')
The code works properly for Python 2 as the smtplib library for Python 2 does not have the above line and hence it allows Unicode characters in the Body and the Subject.

----------
components: email
files: send_rawemail_demo.py
messages: 371801
nosy: barry, jpatel, r.david.murray
priority: normal
severity: normal
status: open
title: smtplib does not handle Unicode characters
type: enhancement
versions: Python 3.8
Added file: https://bugs.python.org/file49249/send_rawemail_demo.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41023>
_______________________________________


More information about the New-bugs-announce mailing list