Skip to content

Instantly share code, notes, and snippets.

@danielfaust
Created June 8, 2011 09:57
Show Gist options
  • Save danielfaust/1014131 to your computer and use it in GitHub Desktop.
Save danielfaust/1014131 to your computer and use it in GitHub Desktop.
Decode Email Headers to Unicode
# header = email.message_from_string(mail['header'])
# decoded_subject = myDecodeHeader(header["subject"])
def myDecodeHeader(subject):
if subject is None:
subject = ''
else:
subject = subject.replace("\r\n", "")
try:
subject = unicode(subject, 'utf-8')
except:
pass
do_check = True
while do_check:
match = re.search(r"=\?.+?\?(Q|q|B|b)\?.+?\?=", subject)
if match:
s1 = subject[:match.start()]
s3 = subject[match.end():]
s2dh = email.Header.decode_header(match.group())[0]
s2 = s2dh[0].replace("\t", "")
if s2dh[1] != None:
s2 = unicode(s2, s2dh[1])
subject = s1 + s2 + s3
else:
do_check = False
return subject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment