Sean Reifscheider | 95ce1fc | 2010-03-19 23:19:55 +0000 | [diff] [blame] | 1 | # Import the email modules we'll need |
| 2 | from email.parser import Parser |
| 3 | |
| 4 | # If the e-mail headers are in a file, uncomment this line: |
Sean Reifscheider | b2efeff | 2010-03-20 00:05:42 +0000 | [diff] [blame^] | 5 | #headers = Parser().parse(open(messagefile, 'r')) |
Sean Reifscheider | 95ce1fc | 2010-03-19 23:19:55 +0000 | [diff] [blame] | 6 | |
| 7 | # Or for parsing headers in a string, use: |
| 8 | headers = 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: |
| 15 | print 'To: %s' % headers['to'] |
| 16 | print 'From: %s' % headers['from'] |
| 17 | print 'Subject: %s' % headers['subject'] |