Better decode

This commit is contained in:
Thomas Forgione 2018-11-29 11:54:54 +01:00
parent 3d7eeaefe7
commit 09cf022b6a
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
1 changed files with 6 additions and 1 deletions

View File

@ -4,6 +4,11 @@ import imaplib
import email.header
import datetime
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))
def process_mailbox(mailbox, to = None):
rv, data = mailbox.search(None, "(UNSEEN)")
if rv != 'OK':
@ -23,7 +28,7 @@ def process_mailbox(mailbox, to = None):
print('From:', msg['From'])
decode = email.header.decode_header(msg['Subject'])
subject = ''.join(map(lambda x: x[0].decode(), decode))
subject = decode_mime_words(decode)
print('Subject: %s' % subject)
date_tuple = email.utils.parsedate_tz(msg['Date'])