0
0
mirror of https://github.com/python/cpython.git synced 2024-12-01 03:01:36 +01:00
cpython/Doc/includes/email-headers.py
Berker Peksag f9e3cf1f9f Issue #23511: Port email-simple.py to Python 3.
Also, update email examples to use the context manager version of open().

Patch by Baptiste Mispelon.
2015-02-25 18:14:09 +02:00

19 lines
596 B
Python

# 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'])