[issue20845] email.utils.formataddr encodes incorrectly

Michael JasonSmith report at bugs.python.org
Mon Mar 3 19:14:53 CET 2014


New submission from Michael JasonSmith:

The email.utils.formataddr function is used to encode a name-address 2-tuple for use as an email message. If the name contains a non-ASCII character it needs to be encoded. This happens correctly in Python 3.3.2, but incorrectly in Python 2.7.5. Ideally Python 2 would acquire the Python 3 behaviour, as this should make porting easier.

In the following Python 3.3.2 example the name is encoded because of the non-ASCII ☢ character:
>>> import email.utils
>>> name = 'Me ☢'
>>> addr = 'mpj17 at onlinegroups.net'
>>> email.utils.formataddr((name, addr))
'=?utf-8?b?TWUg4pii?= <mpj17 at onlinegroups.net>'

In Python 2.7.5 the same name is incorrectly left unaltered:
>>> import email.utils
>>> name = u'Me ☢'
>>> addr = 'mpj17 at onlinegroups.net'
>>> email.utils.formataddr((name, addr))
u'Me \u2622 <mpj17 at onlinegroups.net>'

However, calling the email.header.Header.encode method works around this issue in Python 2:
>>> import email.utils
>>> import email.header
>>> name = u'Me ☢'
>>> addr = 'mpj17 at onlinegroups.net'
>>> h = email.header.Header(name)
>>> email.utils.formataddr((h.encode(), addr))
'=?utf-8?b?TWUg4pii?= <mpj17 at onlinegroups.net>'

The example code immediately above also works in Python 3; it is the current work-around in GroupServer. However, ideally instances of Unicode objects passed to email.utils.formataddr will work the same in both Python 2 and Python 3.

----------
components: Library (Lib)
messages: 212648
nosy: Michael.JasonSmith
priority: normal
severity: normal
status: open
title: email.utils.formataddr encodes incorrectly
type: behavior
versions: Python 2.7, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20845>
_______________________________________


More information about the Python-bugs-list mailing list