Skip to content

Instantly share code, notes, and snippets.

@sebastianw
Created June 2, 2017 10:44
Show Gist options
  • Save sebastianw/ffe09b466a13db98755386dd10926815 to your computer and use it in GitHub Desktop.
Save sebastianw/ffe09b466a13db98755386dd10926815 to your computer and use it in GitHub Desktop.
Decode RFC2047 (formerly RFC1342) encoded mail headers - requires Python 3.3 or later
#! /usr/bin/env python3
# Decode RFC2047 (formerly RFC1342) encoded mail headers
# Requires >= python 3.3
import sys
from email.header import decode_header
def recode_utf8(subj):
output = ''
for s, enc in decode_header(subj):
if enc:
output += s.decode(enc)
else:
try:
output += s.decode()
except AttributeError:
output += s
return output
for l in sys.stdin.readlines():
print(recode_utf8(l).strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment