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

email non-ASCII characters in TO or FROM field doesn't work #59967

Closed
HWJ mannequin opened this issue Aug 22, 2012 · 2 comments
Closed

email non-ASCII characters in TO or FROM field doesn't work #59967

HWJ mannequin opened this issue Aug 22, 2012 · 2 comments

Comments

@HWJ
Copy link
Mannequin

HWJ mannequin commented Aug 22, 2012

BPO 15763
Nosy @warsaw, @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 2012-08-22.14:46:39.826>
created_at = <Date 2012-08-22.11:28:07.627>
labels = ['expert-email']
title = "email  non-ASCII characters in TO or FROM field doesn't work"
updated_at = <Date 2012-08-22.14:46:39.825>
user = 'https://bugs.python.org/HWJ'

bugs.python.org fields:

activity = <Date 2012-08-22.14:46:39.825>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2012-08-22.14:46:39.826>
closer = 'r.david.murray'
components = ['email']
creation = <Date 2012-08-22.11:28:07.627>
creator = 'HWJ'
dependencies = []
files = []
hgrepos = []
issue_num = 15763
keywords = []
message_count = 2.0
messages = ['168869', '168888']
nosy_count = 3.0
nosy_names = ['barry', 'HWJ', 'r.david.murray']
pr_nums = []
priority = 'normal'
resolution = 'works for me'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue15763'
versions = ['Python 3.2']

@HWJ
Copy link
Mannequin Author

HWJ mannequin commented Aug 22, 2012

Trying to generate an email with Latin-1 characters in the TO or FROM
field either produces an exception or produces strange values in the generated email:

Using Python 3.2.3+ (3.2:481f5d9ef577+, Aug 8 2012, 10:00:28)

#!/usr/bin/python3
#-- coding: latin1 --

import smtplib
from email.message import Message
import datetime
import sys

msg= Message()
msg.set_charset('latin-1')
msg['Subject'] = "*** Email Test ***"

# the following gives a UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc ..
# in File "/usr/lib64/python3.2/email/header.py", line 281, in append
# msg['From'] = FromAddr = "Günter Groß <Email_Tester@numa-sv.igpm.rwth-aachen.de>".encode('iso-8859-1')

# The following doesn't crash Python but generates a strange "From" string :
# =?utf-8?b?R8O8bnRlciBHcm/DnyA8RW1haWxfVGVzdGVyQG51bWEtc3YuaWdwbS5yd3RoLWFhY2hlbi5kZT4=?=

msg['From'] = FromAddr = "Günter Groß <Email_Tester@numa-sv.igpm.rwth-aachen.de>"

# The same with this one
msg['To'] = ToAddr = "Günter Weiße <jarausch@igpm.rwth-aachen.de>"

DATE = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S %p')
msg['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S %p')

server= smtplib.SMTP("igpm.igpm.rwth-aachen.de")
# server= smtplib.SMTP('relay.skynet.be')
server.set_debuglevel(9)

msg.set_payload("Gedanken über einen Test","iso-8859-1")
# server.send_message(msg)
server.sendmail(FromAddr,ToAddr,msg.as_string().encode("iso-8859-1"))
server.quit()

@HWJ HWJ mannequin added the topic-email label Aug 22, 2012
@bitdancer
Copy link
Member

Doing non-ASCII email in python before 3.3 is a bit of a pain and not as well documented as it should be. 3.3 will work more like you expect, if you use the new provisional policies (which are intended to become standard in 3.4, after a the bake-in period in 3.3).

For 3.2, you need to handle encoding addresses using utils.formataddr and header.Header:

>>> h = Header(header_name='Sender')
>>> h.append("Éric", 'latin-1')
>>> h.append('<eric@example.com>')
>>> h.encode()
'=?iso-8859-1?q?=C9ric?= <eric@example.com>'
>>> m = Message()
>>> m['Sender'] = h
>>> print(m)
Sender: =?iso-8859-1?q?=C9ric?= <eric@example.com>

In 3.3 this will work:

>>> m = Message()
>>> m['Sender'] = formataddr(('Éric', 'eric@example.com'))
>>> print(m)
Sender: =?iso-8859-1?q?=C9ric?= <eric@example.com>

But even better, so will this:

>>> m = Message(policy=policy.SMTP)
>>> m['From'] = "Günter Weiße <jarausch@igpm.rwth-aachen.de>"
>>> print(m)
From: =?utf-8?q?G=C3=BCnter_Wei=C3=9Fe?= <jarausch@igpm.rwth-aachen.de>

@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
Projects
None yet
Development

No branches or pull requests

1 participant