Staging
v0.5.1
Revision 4ea802868460fad54e40cb99eb0ca283b3b293f0 authored by Miss Islington (bot) on 26 May 2020, 09:16:36 UTC, committed by GitHub on 26 May 2020, 09:16:36 UTC

struct.error is now raised if there is a null character in a struct
format string.
(cherry picked from commit 3f59b55316f4c6ab451997902579aa69020b537c)
(cherry picked from commit 5ff5edfef63b3dbc1abb004b3fa4b3db87e79ff9)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
1 parent b068d89
Raw File
email-headers.py
# Import the email modules we'll need
from email.parser import BytesParser, Parser
from email.policy import default

# If the e-mail headers are in a file, uncomment these two lines:
# with open(messagefile, 'rb') as fp:
#     headers = BytesParser(policy=default).parse(fp)

#  Or for parsing headers in a string (this is an uncommon operation), use:
headers = Parser(policy=default).parsestr(
        'From: Foo Bar <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: {}'.format(headers['to']))
print('From: {}'.format(headers['from']))
print('Subject: {}'.format(headers['subject']))

# You can also access the parts of the addresses:
print('Recipient username: {}'.format(headers['to'].addresses[0].username))
print('Sender name: {}'.format(headers['from'].addresses[0].display_name))
back to top