Python IMAP: =?utf-8?Q? in subject string Python IMAP: =?utf-8?Q? in subject string python python

Python IMAP: =?utf-8?Q? in subject string


In MIME terminology, those encoded chunks are called encoded-words. You can decode them like this:

import email.Headertext, encoding = email.Header.decode_header('=?utf-8?Q?Subject?=')[0]

Check out the docs for email.Header for more details.


This is a MIME encoded-word. You can parse it with email.header:

import email.headerdef decode_mime_words(s):    return u''.join(        word.decode(encoding or 'utf8') if isinstance(word, bytes) else word        for word, encoding in email.header.decode_header(s))print(decode_mime_words(u'=?utf-8?Q?Subject=c3=a4?=X=?utf-8?Q?=c3=bc?='))


In Python 3.3+, the parsing classes and functions in email.parser automatically decode "encoded words" in headers if their policy argument is set to policy.default

>>> import email>>> from email import policy>>> msg = email.message_from_file(open('message.txt'), policy=policy.default)>>> msg['from']'Pepé Le Pew <pepe@example.com>'

The parsing classes and functions are:

Confusingly, up to at least Python 3.8, the default policy for these parsing functions is not policy.default, but policy.compat32, which does not decode "encoded words".

>>> msg = email.message_from_file(open('message.txt'))>>> msg['from']'=?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>'