Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
Last active September 4, 2015 01:05
Show Gist options
  • Save pgtwitter/af5c6fb1b34220a638d6 to your computer and use it in GitHub Desktop.
Save pgtwitter/af5c6fb1b34220a638d6 to your computer and use it in GitHub Desktop.
MailのSubjectをdecodeする
from email.parser import Parser
from email.header import decode_header
fp = open("/path/to/mail.eml", "rb")
parser = Parser()
message = parser.parse(fp)
fp.close()
strs = []
for pair in decode_header(message['Subject']):
subject = pair[0]
encoding = pair[1]
strs.append(subject if (encoding == None) else subject.decode(encoding))
print ' '.join(strs)
@pgtwitter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment