ยปCore Development>Code coverage>Doc/includes/email-headers.py

Python code coverage for Doc/includes/email-headers.py

#countcontent
1n/a# Import the email modules we'll need
2n/afrom email.parser import BytesParser, Parser
3n/afrom email.policy import default
4n/a
5n/a# If the e-mail headers are in a file, uncomment these two lines:
6n/a# with open(messagefile, 'rb') as fp:
7n/a# headers = BytesParser(policy=default).parse(fp)
8n/a
9n/a# Or for parsing headers in a string (this is an uncommon operation), use:
10n/aheaders = Parser(policy=default).parsestr(
11n/a 'From: Foo Bar <user@example.com>\n'
12n/a 'To: <someone_else@example.com>\n'
13n/a 'Subject: Test message\n'
14n/a '\n'
15n/a 'Body would go here\n')
16n/a
17n/a# Now the header items can be accessed as a dictionary:
18n/aprint('To: {}'.format(headers['to']))
19n/aprint('From: {}'.format(headers['from']))
20n/aprint('Subject: {}'.format(headers['subject']))
21n/a
22n/a# You can also access the parts of the addresses:
23n/aprint('Recipient username: {}'.format(headers['to'].addresses[0].username))
24n/aprint('Sender name: {}'.format(headers['from'].addresses[0].display_name))