diff options
author | Sean Reifscheider <jafo@tummy.com> | 2010-03-19 23:23:05 +0000 |
---|---|---|
committer | Sean Reifscheider <jafo@tummy.com> | 2010-03-19 23:23:05 +0000 |
commit | 5441fa2cb0443a42b5634d135d276c93c2c53dff (patch) | |
tree | 11f327165b4c6a03be3fa1f3fbede5a8128986bc | |
parent | c9c0331da4b896fedb5d8edaada48d7f63ac2c7e (diff) | |
download | cpython-5441fa2cb0443a42b5634d135d276c93c2c53dff.tar.gz |
Adding an example of reproducing the rfc822.Message() parsing.
-rw-r--r-- | Doc/includes/email-headers.py | 17 | ||||
-rw-r--r-- | Doc/library/email-examples.rst | 6 |
2 files changed, 23 insertions, 0 deletions
diff --git a/Doc/includes/email-headers.py b/Doc/includes/email-headers.py new file mode 100644 index 0000000000..edaac235f3 --- /dev/null +++ b/Doc/includes/email-headers.py @@ -0,0 +1,17 @@ +# Import the email modules we'll need +from email.parser import Parser + +# If the e-mail headers are in a file, uncomment this line: +#headers = Parser().parse(messagefile) + +# 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'] diff --git a/Doc/library/email-examples.rst b/Doc/library/email-examples.rst index c1b16da394..32cecf3486 100644 --- a/Doc/library/email-examples.rst +++ b/Doc/library/email-examples.rst @@ -11,6 +11,12 @@ First, let's see how to create and send a simple text message: .. literalinclude:: ../includes/email-simple.py +And parsing RFC822 headers can easily be done by the parse(filename) or +parsestr(message_as_string) methods of the Parser() class: + +.. literalinclude:: ../includes/email-headers.py + + Here's an example of how to send a MIME message containing a bunch of family pictures that may be residing in a directory: |