Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active December 23, 2015 01:39
Show Gist options
  • Save noqqe/6561895 to your computer and use it in GitHub Desktop.
Save noqqe/6561895 to your computer and use it in GitHub Desktop.
Simply convert internationalized mailheaders (http://tools.ietf.org/html/rfc2047.html) into utf-8
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## About
# Simply convert internationalized mailheaders into utf-8
# See: http://tools.ietf.org/html/rfc2047.html
## Usage
# $ echo 'Check german umlauts =?iso-8859-1?B?5CD8?= =?iso-8859-1?Q?_=F6?=' | ./convert_mailheader.py
# > Check german umlauts ä ü ö
# import libraries
import sys
from email.header import decode_header
# read from stdin
x = sys.stdin.read()
# decode internationalized subject and transform ascii into utf8
x = decode_header(x)
x = ' '.join([ unicode(t[0], t[1] or 'ASCII' ) for t in x ])
print x.encode('utf8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment