From 78a44c5428a6ff016baceb6fc517b1efd39afd33 Mon Sep 17 00:00:00 2001 From: Sean Reifscheider Date: Fri, 19 Mar 2010 23:23:05 +0000 Subject: [PATCH] Adding an example of reproducing the rfc822.Message() parsing. --- Doc/includes/email-headers.py | 17 +++++++++++++++++ Doc/library/email-examples.rst | 6 ++++++ 2 files changed, 23 insertions(+) create mode 100644 Doc/includes/email-headers.py diff --git a/Doc/includes/email-headers.py b/Doc/includes/email-headers.py new file mode 100644 index 00000000000..edaac235f35 --- /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: \n' + 'To: \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 c1b16da3947..32cecf3486c 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: