FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Doc/includes/email-headers.py at master · python-emscripten/cpython · GitHub
python-emscripten
cpython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Doc
/
includes
/
email-headers.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
24 lines (20 loc) · 950 Bytes
Breadcrumbs
cpython
/
Doc
/
includes
/
email-headers.py
Copy path
File metadata and controls
24 lines (20 loc) · 950 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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
|
FazBrowse Home
|
New Git URL