Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: a5f49f5bb46b6b8c7bf1ffedbaf3009186b94b50 authored by Benjamin Peterson on 10 December 2014, 15:57:00 UTC
bump to 2.7.9 final
Tip revision: a5f49f5
email-headers.py
# Import the email modules we'll need
from email.parser import Parser

#  If the e-mail headers are in a file, uncomment this line:
#headers = Parser().parse(open(messagefile, 'r'))

#  Or for parsing headers in a string, use:
headers = Parser().parsestr('From: <user@example.com>\n'
        'To: <someone_else@example.com>\n'
        'Subject: Test message\n'
        '\n'
        'Body would go here\n')

#  Now the header items can be accessed as a dictionary:
print 'To: %s' % headers['to']
print 'From: %s' % headers['from']
print 'Subject: %s' % headers['subject']
back to top