Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 827
janeh Avatar asked Oct 15 '12 20:10

janeh


2 Answers

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.

like image 181
ataylor Avatar answered Oct 06 '22 15:10

ataylor


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

import email.header

def 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?='))
like image 29
phihag Avatar answered Oct 06 '22 14:10

phihag