EmailMessage and RFC 2047

Thomas Jollans tjol at tjol.eu
Tue Apr 30 11:45:52 EDT 2019


On 30/04/2019 13.11, Peter J. Holzer wrote:
> Hi,
> 
> https://docs.python.org/3.7/library/email.header.html states:
> 
> | This module is part of the legacy (Compat32) email API. In the current
> | API encoding and decoding of headers is handled transparently by the
> | dictionary-like API of the EmailMessage class.
> 
> I understood this to mean that an EmailMessage does decode RFC 2047
> encoded header fields automatically.

I have no idea why it's doing what it's doing. The lesson appears to be:
don't use the compat32 mode unless you have to support Python 3.2 (which
you don't, because that's ridiculous)

>>> msg
'Subject: =?utf-8?q?=C3=89lys=C3=A9e?=\nContent-Type: text/plain;
charset="utf-8"\nContent-Transfer-Encoding: 7bit\nMIME-Version:
1.0\n\nTEST MESSAGE\n'
>>> p = email.parser.Parser(email.message.EmailMessage)
>>> p.parsestr(msg)['Subject']
'=?utf-8?q?=C3=89lys=C3=A9e?='
>>> p = email.parser.Parser(email.message.EmailMessage,
policy=email.policy.default)
>>> p.parsestr(msg)['Subject']
'Élysée'
>>>

Simple. Silly. Simply silly.

I've been confused by the unicode handling of this module before... [1]

— Thomas

[1]. https://mail.python.org/pipermail/python-list/2018-October/737726.html
[I'm still confused]

> 
> However this does not seem to be the case:
> 
> portia:~/tmp 13:06 :-) 16% python3
> Python 3.7.2+ (default, Feb 27 2019, 15:41:59) 
> [GCC 8.2.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import email.parser
>>>> import email.message
>>>> p = email.parser.Parser(email.message.EmailMessage)
>>>> f = open("test.msg", "r")
>>>> m = p.parse(f)
>>>> m
> <email.message.EmailMessage object at 0x7f3b5cac4278>
>>>> m["Subject"]
> '[luga] =?utf-8?Q?=C3=84ndern_des_Passwortes_mittels_We?=\n\t=?utf-8?Q?bformular?='
>>>> m["From"]
> 'Martin =?iso-8859-15?Q?W=FCrtele?= <martin at xxxxxxxx.xxx>'
>>>>
> 
> Am I using it wrong or did I misunderstand what is meant by "handled
> transparently by the dictionary-like API of the EmailMessage class"? If
> the latter, what does it mean?
> 
>         hp
> 
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20190430/1b3f94af/attachment.sig>


More information about the Python-list mailing list