Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smtplib can't send 8bit encoded utf-8 message #69922

Closed
airween mannequin opened this issue Nov 26, 2015 · 5 comments
Closed

smtplib can't send 8bit encoded utf-8 message #69922

airween mannequin opened this issue Nov 26, 2015 · 5 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@airween
Copy link
Mannequin

airween mannequin commented Nov 26, 2015

BPO 25736
Nosy @bitdancer

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 = <Date 2015-11-26.17:05:13.078>
created_at = <Date 2015-11-26.07:42:20.595>
labels = ['type-bug', 'invalid']
title = "smtplib can't send 8bit encoded utf-8 message"
updated_at = <Date 2015-11-26.17:54:14.568>
user = 'https://bugs.python.org/airween'

bugs.python.org fields:

activity = <Date 2015-11-26.17:54:14.568>
actor = 'airween'
assignee = 'none'
closed = True
closed_date = <Date 2015-11-26.17:05:13.078>
closer = 'r.david.murray'
components = []
creation = <Date 2015-11-26.07:42:20.595>
creator = 'airween'
dependencies = []
files = []
hgrepos = []
issue_num = 25736
keywords = []
message_count = 5.0
messages = ['255401', '255403', '255425', '255426', '255433']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'airween']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue25736'
versions = ['Python 3.4']

@airween airween mannequin added the type-bug An unexpected behavior, bug, or error label Nov 26, 2015
@airween
Copy link
Mannequin Author

airween mannequin commented Nov 26, 2015

Looks like smtplib can send only messages, which contains only 7bit (ascii) characters. Here is the example:

# -- coding: utf8 --

import time
import smtplib

mailfrom = "my@mydomain.com"
rcptto = "me@otherdomain.com"

msg = """%s
From: Me <%s>
To: %s
Subject: Plain text e-mail
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit

happy New Year

Ευτυχισμένο το Νέο Έτος

明けましておめでとうございます

с Новым годом
""" % (time.strftime('%a, %d %b %Y %H:%M:%S +0100', time.localtime()), mailfrom, rcptto)

server = smtplib.SMTP('localhost')
server.sendmail(mailfrom, rcptto, msg)
server.quit()

With Python2 (Python 2.7), this script finished succesfully. With Python3 (Python 3.4), I've got this execption:

Traceback (most recent call last):
  File "8bittest.py", line 28, in <module>
    server.sendmail(mailfrom, rcptto, msg)
  File "/usr/lib/python3.4/smtplib.py", line 765, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 261-271: ordinal not in range(128)

Basicly, I don't understand, why smtplib allows only ascii encoded messages in Python 3. That worked (and works) in Python 2, and I think, that's the correct behavior.

@airween
Copy link
Mannequin Author

airween mannequin commented Nov 26, 2015

Here is a workaround:

server.sendmail(mailfrom, rcptto, msg.encode("utf8"))

May be this would be better inside of smtplib?

@bitdancer
Copy link
Member

Although that will work for text-only messages if you know what RFC format looks like, you really don't want to do that in the general case, since you can't express messages that have binary non-text content using unicode. What you want to do is prepare your message in correct RFC form, which is what the email library is for. With the new API (provisional now, but any changes will be minor when it becomes final in 3.6), this is even easy (see the 'contentmanager' docs). Then you call smtplib.send_message, and the encoding to RFC format is taken care of for you. In 3.5 it even supports SMTPUTF8, if you know any servers that do :)

@bitdancer
Copy link
Member

Oh, and as for why this worked in python2: in python2 strings were binary, not unicode, so the non-ascii stuff was already in bytes form.

@airween
Copy link
Mannequin Author

airween mannequin commented Nov 26, 2015

David,

many thanks for your information.

I think my e-mail format was correct - I've copied it from a maildir, as an "email file".

As I wrote, there is a solution: before the code passes the 'msg' argument to sendmail() function, it needs to encode() it as "utf-8", then it will be a bytestream, instead of unicode (which is the default type of any string in Py3). Meanwhile I realized it :).

Thanks again, and sorry for my mistake.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant