Skip to content

Instantly share code, notes, and snippets.

@co3k
Created April 12, 2010 07:35
Show Gist options
  • Save co3k/363355 to your computer and use it in GitHub Desktop.
Save co3k/363355 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import email, sys
p = email.FeedParser.FeedParser()
p.feed(sys.stdin.read())
mail = p.close()
content_charset = mail.get_content_charset(None)
text = ""
# decode headers
for key in mail.keys():
decoded = email.Header.decode_header(mail[key])[0]
if decoded[1] != None:
str = unicode(decoded[0], decoded[1])
else:
str = mail[key]
text = text + key + ": " + str + "\n"
# decode part
for part in mail.walk():
payload = part.get_payload()
charset = part.get_charset()
if charset is None:
charset = content_charset
if charset is None :
charset = "latin_1"
payload = payload.decode(charset, "replace")
text = text + " " + payload
text = text + "---------------------------------------\n"
f = open("/tmp/ebilog", "a+")
f.writelines(text.encode('utf-8'))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment