Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: 05c28b08f6e2fc8782472b026c98a3fdd61a2ba9 authored by Larry Hastings on 07 May 2019, 18:29:45 UTC
Post-release version bump for 3.4.10.
Tip revision: 05c28b0
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 these two lines:
# with open(messagefile) as fp:
#     headers = Parser().parse(fp)

#  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