blob: a53317dbd84375e279aee45062a0361f954c0502 [file] [log] [blame]
Sean Reifscheider78a44c52010-03-19 23:23:05 +00001# Import the email modules we'll need
2from email.parser import Parser
3
4# If the e-mail headers are in a file, uncomment this line:
Sean Reifscheiderbb30da92010-03-20 00:06:05 +00005#headers = Parser().parse(open(messagefile, 'r'))
Sean Reifscheider78a44c52010-03-19 23:23:05 +00006
7# Or for parsing headers in a string, use:
8headers = Parser().parsestr('From: <user@example.com>\n'
9 'To: <someone_else@example.com>\n'
10 'Subject: Test message\n'
11 '\n'
12 'Body would go here\n')
13
14# Now the header items can be accessed as a dictionary:
Senthil Kumaranc9613222010-10-17 11:42:21 +000015print('To: %s' % headers['to'])
16print('From: %s' % headers['from'])
17print('Subject: %s' % headers['subject'])