[Python-checkins] r79124 - in python/branches/py3k/Doc: includes/email-headers.py library/email-examples.rst

sean.reifschneider python-checkins at python.org
Sat Mar 20 00:23:05 CET 2010


Author: sean.reifschneider
Date: Sat Mar 20 00:23:05 2010
New Revision: 79124

Log:
Adding an example of reproducing the rfc822.Message() parsing.


Added:
   python/branches/py3k/Doc/includes/email-headers.py
Modified:
   python/branches/py3k/Doc/library/email-examples.rst

Added: python/branches/py3k/Doc/includes/email-headers.py
==============================================================================
--- (empty file)
+++ python/branches/py3k/Doc/includes/email-headers.py	Sat Mar 20 00:23:05 2010
@@ -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 at example.com>\n'
+        'To: <someone_else at 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']

Modified: python/branches/py3k/Doc/library/email-examples.rst
==============================================================================
--- python/branches/py3k/Doc/library/email-examples.rst	(original)
+++ python/branches/py3k/Doc/library/email-examples.rst	Sat Mar 20 00:23:05 2010
@@ -11,6 +11,12 @@
 .. 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:
 


More information about the Python-checkins mailing list