Created
April 12, 2010 07:35
-
-
Save co3k/363355 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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