| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-email.Generator.html" /> |
| 13 | <link rel="prev" href="module-email.Message.html" /> |
| 14 | <link rel="parent" href="module-email.html" /> |
| 15 | <link rel="next" href="node583.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>12.2.2 Parsing email messages</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="12.2.1.1 Deprecated methods" |
| 25 | href="node581.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="12.2 email " |
| 28 | href="module-email.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="12.2.2.1 FeedParser API" |
| 31 | href="node583.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="node581.html">12.2.1.1 Deprecated methods</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-email.html">12.2 email </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node583.html">12.2.2.1 FeedParser API</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0014220000000000000000"> |
| 56 | 12.2.2 Parsing email messages</A> |
| 57 | </H2> |
| 58 | <A NAME="module-email.Parser"></A> |
| 59 | |
| 60 | <P> |
| 61 | Message object structures can be created in one of two ways: they can be |
| 62 | created from whole cloth by instantiating <tt class="class">Message</tt> objects and |
| 63 | stringing them together via <tt class="method">attach()</tt> and |
| 64 | <tt class="method">set_payload()</tt> calls, or they can be created by parsing a flat text |
| 65 | representation of the email message. |
| 66 | |
| 67 | <P> |
| 68 | The <tt class="module">email</tt> package provides a standard parser that understands |
| 69 | most email document structures, including MIME documents. You can |
| 70 | pass the parser a string or a file object, and the parser will return |
| 71 | to you the root <tt class="class">Message</tt> instance of the object structure. For |
| 72 | simple, non-MIME messages the payload of this root object will likely |
| 73 | be a string containing the text of the message. For MIME |
| 74 | messages, the root object will return <code>True</code> from its |
| 75 | <tt class="method">is_multipart()</tt> method, and the subparts can be accessed via |
| 76 | the <tt class="method">get_payload()</tt> and <tt class="method">walk()</tt> methods. |
| 77 | |
| 78 | <P> |
| 79 | There are actually two parser interfaces available for use, the classic |
| 80 | <tt class="class">Parser</tt> API and the incremental <tt class="class">FeedParser</tt> API. The classic |
| 81 | <tt class="class">Parser</tt> API is fine if you have the entire text of the message in |
| 82 | memory as a string, or if the entire message lives in a file on the file |
| 83 | system. <tt class="class">FeedParser</tt> is more appropriate for when you're reading the |
| 84 | message from a stream which might block waiting for more input (e.g. reading |
| 85 | an email message from a socket). The <tt class="class">FeedParser</tt> can consume and parse |
| 86 | the message incrementally, and only returns the root object when you close the |
| 87 | parser<A NAME="tex2html101" |
| 88 | HREF="#foot56991"><SUP>12.1</SUP></A>. |
| 89 | |
| 90 | <P> |
| 91 | Note that the parser can be extended in limited ways, and of course |
| 92 | you can implement your own parser completely from scratch. There is |
| 93 | no magical connection between the <tt class="module">email</tt> package's bundled |
| 94 | parser and the <tt class="class">Message</tt> class, so your custom parser can create |
| 95 | message object trees any way it finds necessary. |
| 96 | |
| 97 | <P> |
| 98 | <BR><HR><H4>Footnotes</H4> |
| 99 | <DL> |
| 100 | <DT><A NAME="foot56991">... |
| 101 | parser</A><A |
| 102 | href="module-email.Parser.html#tex2html101"><SUP>12.1</SUP></A></DT> |
| 103 | <DD>As of email package version 3.0, introduced in |
| 104 | Python 2.4, the classic <tt class="class">Parser</tt> was re-implemented in terms of the |
| 105 | <tt class="class">FeedParser</tt>, so the semantics and results are identical between the two |
| 106 | parsers. |
| 107 | |
| 108 | </DD> |
| 109 | </DL> |
| 110 | <p><br /></p><hr class='online-navigation' /> |
| 111 | <div class='online-navigation'> |
| 112 | <!--Table of Child-Links--> |
| 113 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 114 | |
| 115 | <UL CLASS="ChildLinks"> |
| 116 | <LI><A href="node583.html">12.2.2.1 FeedParser API</a> |
| 117 | <LI><A href="node584.html">12.2.2.2 Parser class API</a> |
| 118 | <LI><A href="node585.html">12.2.2.3 Additional notes</a> |
| 119 | </ul> |
| 120 | <!--End of Table of Child-Links--> |
| 121 | </div> |
| 122 | |
| 123 | <DIV CLASS="navigation"> |
| 124 | <div class='online-navigation'> |
| 125 | <p></p><hr /> |
| 126 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 127 | <tr> |
| 128 | <td class='online-navigation'><a rel="prev" title="12.2.1.1 Deprecated methods" |
| 129 | href="node581.html"><img src='../icons/previous.png' |
| 130 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 131 | <td class='online-navigation'><a rel="parent" title="12.2 email " |
| 132 | href="module-email.html"><img src='../icons/up.png' |
| 133 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 134 | <td class='online-navigation'><a rel="next" title="12.2.2.1 FeedParser API" |
| 135 | href="node583.html"><img src='../icons/next.png' |
| 136 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 137 | <td align="center" width="100%">Python Library Reference</td> |
| 138 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 139 | href="contents.html"><img src='../icons/contents.png' |
| 140 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 141 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 142 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 143 | <td class='online-navigation'><a rel="index" title="Index" |
| 144 | href="genindex.html"><img src='../icons/index.png' |
| 145 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 146 | </tr></table> |
| 147 | <div class='online-navigation'> |
| 148 | <b class="navlabel">Previous:</b> |
| 149 | <a class="sectref" rel="prev" href="node581.html">12.2.1.1 Deprecated methods</A> |
| 150 | <b class="navlabel">Up:</b> |
| 151 | <a class="sectref" rel="parent" href="module-email.html">12.2 email </A> |
| 152 | <b class="navlabel">Next:</b> |
| 153 | <a class="sectref" rel="next" href="node583.html">12.2.2.1 FeedParser API</A> |
| 154 | </div> |
| 155 | </div> |
| 156 | <hr /> |
| 157 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 158 | </DIV> |
| 159 | <!--End of Navigation Panel--> |
| 160 | <ADDRESS> |
| 161 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 162 | </ADDRESS> |
| 163 | </BODY> |
| 164 | </HTML> |