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

janeh picture janeh · Oct 15, 2012 · Viewed 15.7k times · Source

I am displaying new email with IMAP, and everything looks fine, except for one message subject shows as:

=?utf-8?Q?Subject?=

How can I fix it?

Answer

ataylor picture ataylor · Oct 15, 2012

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

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

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