blob: 89c8f3af32f3017f67af55e7bad8074918215f8e [file] [log] [blame]
Sean Reifscheider78a44c52010-03-19 23:23:05 +00001# Import the email modules we'll need
2from email.parser import Parser
3
Berker Peksagf9e3cf12015-02-25 18:14:09 +02004# If the e-mail headers are in a file, uncomment these two lines:
5# with open(messagefile) as fp:
6# headers = Parser().parse(fp)
Sean Reifscheider78a44c52010-03-19 23:23:05 +00007
8# Or for parsing headers in a string, use:
9headers = Parser().parsestr('From: <user@example.com>\n'
10 'To: <someone_else@example.com>\n'
11 'Subject: Test message\n'
12 '\n'
13 'Body would go here\n')
14
15# Now the header items can be accessed as a dictionary:
Senthil Kumaranc9613222010-10-17 11:42:21 +000016print('To: %s' % headers['to'])
17print('From: %s' % headers['from'])
18print('Subject: %s' % headers['subject'])