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.Message.get_params decodes only first one header value
Type: enhancement Stage: resolved
Components: email Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: andrewnester, barry, pi314159, r.david.murray
Priority: normal Keywords:

Created on 2017-02-28 18:32 by pi314159, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 394 closed andrewnester, 2017-03-02 15:52
Messages (3)
msg288720 - (view) Author: Sergey (pi314159) Date: 2017-02-28 18:32
email.Message class has method get_params() that can decode(unquote) header values in compliance with RFC2231 and RFC2047. But if in email message exists multiple headers with the same key it can't be used to decode other headers than first.
In my application I could use: 
   headers = message.items() 
   for key, value in headers:
       cleanValue = message.get_params(value=value)
       print(key, cleanValue)
Also have posted question on stackoverflow:
http://stackoverflow.com/questions/42502312/python-3-email-package-how-decode-given-header-value
msg288810 - (view) Author: Andrew Nester (andrewnester) * Date: 2017-03-02 15:53
Thanks for reporting! Just added PR fixing this.
msg288814 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-03-02 16:39
Thanks for the response, but I do not believe that this is a bug.

The python3 email package will decode the headers automatically if you use the new policies, so if you iterate through the headers, you'll get the decoded versions, with access to the parms dict for each.  (For custom headers you will have to register the appropriate header parser, but it should automatically handle all the standard mime headers.)

For the compat32 policy (which is the default), there is indeed no easy way to do the same, but that isn't a bug, because any header that contains parameters (MIME headers) is supposed to be unique.

As for the stackoverflow question, see above for the RFC 2231 issue (the headers are unique).  For doing RFC 2047, decode_header does that.  With the compat32 policy it is awkward, but documented, how to apply the functions in the header submodule to decode an arbitrary string.  For the new policies you don't have to think about it; as I said above the decoding is done automatically (all unknown headers are treated as unstructured and rfc2047 decoding is done automatically).
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73864
2017-03-02 16:39:04r.david.murraysetstatus: open -> closed
resolution: not a bug
messages: + msg288814

stage: resolved
2017-03-02 15:53:54andrewnestersetnosy: + andrewnester
messages: + msg288810
2017-03-02 15:52:33andrewnestersetpull_requests: + pull_request326
2017-02-28 20:26:38pi314159settype: behavior -> enhancement
2017-02-28 18:32:28pi314159create