This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: email.header.decode_header can not decode string with quotation
Type: behavior Stage: resolved
Components: email Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: barry, era, r.david.murray, sijian liang
Priority: normal Keywords:

Created on 2016-09-13 08:49 by sijian liang, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
demo.py sijian liang, 2016-09-13 08:49
Messages (3)
msg276214 - (view) Author: sijian liang (sijian liang) Date: 2016-09-13 08:51
#!/usr/bin/python
#-*- coding:utf-8 -*-
import email
# from email.parser import Parser
def decode_email_header(header, sep=''):
	l = []
	for s, c in header:
		if c:
			l.append(s.decode(c))
		else:
			l.append(s)
	return sep.join(l)
s = email.Header.decode_header('"=?gb18030?B?bWFpbGZvcnRlc3R0?=" <2070776562@qq.com>')
print decode_email_header(s)
s = email.Header.decode_header('=?gb18030?B?bWFpbGZvcnRlc3R0?= <2070776562@qq.com>')
print decode_email_header(s)


# see output:
#    "=?gb18030?B?bWFpbGZvcnRlc3R0?=" <2070776562@qq.com>
#    mailfortestt<2070776562@qq.com>
msg276236 - (view) Author: (era) Date: 2016-09-13 10:05
The double quotes around the "human readable" part of the email address are not allowed.  Python is handling this correctly.
msg276320 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-09-13 18:42
Note, however, that python3 will compensate for this bug in the input, and register a defect for it:

>>> import email
>>> import email.policy
>>> msg = email.message_from_string('To: "=?gb18030?B?bWFpbGZvcnRlc3R0?=" <2070776562@qq.com>\n\ntest body', policy=email.policy.default)
>>> print(msg)
To: "=?gb18030?B?bWFpbGZvcnRlc3R0?=" <2070776562@qq.com>

test body
>>> msg['To']
'mailfortestt <2070776562@qq.com>'
>>> msg['To'].defects
(InvalidHeaderDefect('encoded word inside quoted string',),)
History
Date User Action Args
2022-04-11 14:58:36adminsetgithub: 72309
2016-09-13 18:42:23r.david.murraysetstatus: open -> closed
resolution: out of date
messages: + msg276320

stage: resolved
2016-09-13 10:05:20erasetnosy: + era
messages: + msg276236
2016-09-13 08:51:52sijian liangsetmessages: + msg276214
2016-09-13 08:49:33sijian liangcreate