Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78a44c5

Browse files
author
Sean Reifscheider
committed
Adding an example of reproducing the rfc822.Message() parsing.
1 parent d9b7d48 commit 78a44c5

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

Doc/includes/email-headers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Import the email modules we'll need
2+
from email.parser import Parser
3+
4+
# If the e-mail headers are in a file, uncomment this line:
5+
#headers = Parser().parse(messagefile)
6+
7+
# Or for parsing headers in a string, use:
8+
headers = Parser().parsestr('From: <[email protected]>\n'
9+
10+
'Subject: Test message\n'
11+
'\n'
12+
'Body would go here\n')
13+
14+
# Now the header items can be accessed as a dictionary:
15+
print 'To: %s' % headers['to']
16+
print 'From: %s' % headers['from']
17+
print 'Subject: %s' % headers['subject']

Doc/library/email-examples.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ First, let's see how to create and send a simple text message:
1111
.. literalinclude:: ../includes/email-simple.py
1212

1313

14+
And parsing RFC822 headers can easily be done by the parse(filename) or
15+
parsestr(message_as_string) methods of the Parser() class:
16+
17+
.. literalinclude:: ../includes/email-headers.py
18+
19+
1420
Here's an example of how to send a MIME message containing a bunch of family
1521
pictures that may be residing in a directory:
1622

0 commit comments

Comments
 (0)