Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 1 | \declaremodule{standard}{email.Parser} |
| 2 | \modulesynopsis{Parse flat text email messages to produce a message |
| 3 | object tree.} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 5 | Message object trees can be created in one of two ways: they can be |
| 6 | created from whole cloth by instantiating \class{Message} objects and |
| 7 | stringing them together via \method{add_payload()} and |
| 8 | \method{set_payload()} calls, or they can be created by parsing a flat text |
| 9 | representation of the email message. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 10 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 11 | The \module{email} package provides a standard parser that understands |
| 12 | most email document structures, including MIME documents. You can |
| 13 | pass the parser a string or a file object, and the parser will return |
| 14 | to you the root \class{Message} instance of the object tree. For |
| 15 | simple, non-MIME messages the payload of this root object will likely |
Fred Drake | ab9b238 | 2001-10-16 19:22:51 +0000 | [diff] [blame] | 16 | be a string containing the text of the message. For MIME |
| 17 | messages, the root object will return true from its |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 18 | \method{is_multipart()} method, and the subparts can be accessed via |
| 19 | the \method{get_payload()} and \method{walk()} methods. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 20 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 21 | Note that the parser can be extended in limited ways, and of course |
| 22 | you can implement your own parser completely from scratch. There is |
| 23 | no magical connection between the \module{email} package's bundled |
| 24 | parser and the \class{Message} class, so your custom parser can create |
Greg Ward | f8b1f24 | 2002-02-22 21:24:32 +0000 | [diff] [blame] | 25 | message object trees any way it finds necessary. |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 26 | |
Barry Warsaw | c7f8b86 | 2001-10-11 15:45:05 +0000 | [diff] [blame] | 27 | The primary parser class is \class{Parser} which parses both the |
| 28 | headers and the payload of the message. In the case of |
| 29 | \mimetype{multipart} messages, it will recursively parse the body of |
| 30 | the container message. The \module{email.Parser} module also provides |
| 31 | a second class, called \class{HeaderParser} which can be used if |
| 32 | you're only interested in the headers of the message. |
| 33 | \class{HeaderParser} can be much faster in this situations, since it |
| 34 | does not attempt to parse the message body, instead setting the |
| 35 | payload to the raw body as a string. \class{HeaderParser} has the |
| 36 | same API as the \class{Parser} class. |
| 37 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 38 | \subsubsection{Parser class API} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 39 | |
| 40 | \begin{classdesc}{Parser}{\optional{_class}} |
| 41 | The constructor for the \class{Parser} class takes a single optional |
Fred Drake | ab9b238 | 2001-10-16 19:22:51 +0000 | [diff] [blame] | 42 | argument \var{_class}. This must be a callable factory (such as a |
| 43 | function or a class), and it is used whenever a sub-message object |
| 44 | needs to be created. It defaults to \class{Message} (see |
| 45 | \refmodule{email.Message}). The factory will be called without |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 46 | arguments. |
| 47 | \end{classdesc} |
| 48 | |
| 49 | The other public \class{Parser} methods are: |
| 50 | |
| 51 | \begin{methoddesc}[Parser]{parse}{fp} |
| 52 | Read all the data from the file-like object \var{fp}, parse the |
| 53 | resulting text, and return the root message object. \var{fp} must |
| 54 | support both the \method{readline()} and the \method{read()} methods |
| 55 | on file-like objects. |
| 56 | |
| 57 | The text contained in \var{fp} must be formatted as a block of \rfc{2822} |
| 58 | style headers and header continuation lines, optionally preceeded by a |
| 59 | \emph{Unix-From} header. The header block is terminated either by the |
| 60 | end of the data or by a blank line. Following the header block is the |
| 61 | body of the message (which may contain MIME-encoded subparts). |
| 62 | \end{methoddesc} |
| 63 | |
| 64 | \begin{methoddesc}[Parser]{parsestr}{text} |
| 65 | Similar to the \method{parse()} method, except it takes a string |
| 66 | object instead of a file-like object. Calling this method on a string |
| 67 | is exactly equivalent to wrapping \var{text} in a \class{StringIO} |
| 68 | instance first and calling \method{parse()}. |
| 69 | \end{methoddesc} |
| 70 | |
| 71 | Since creating a message object tree from a string or a file object is |
| 72 | such a common task, two functions are provided as a convenience. They |
| 73 | are available in the top-level \module{email} package namespace. |
| 74 | |
| 75 | \begin{funcdesc}{message_from_string}{s\optional{, _class}} |
| 76 | Return a message object tree from a string. This is exactly |
| 77 | equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} is |
| 78 | interpreted as with the \class{Parser} class constructor. |
| 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{funcdesc}{message_from_file}{fp\optional{, _class}} |
| 82 | Return a message object tree from an open file object. This is exactly |
| 83 | equivalent to \code{Parser().parse(fp)}. Optional \var{_class} is |
| 84 | interpreted as with the \class{Parser} class constructor. |
| 85 | \end{funcdesc} |
| 86 | |
| 87 | Here's an example of how you might use this at an interactive Python |
| 88 | prompt: |
| 89 | |
| 90 | \begin{verbatim} |
| 91 | >>> import email |
| 92 | >>> msg = email.message_from_string(myString) |
| 93 | \end{verbatim} |
| 94 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 95 | \subsubsection{Additional notes} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 96 | |
| 97 | Here are some notes on the parsing semantics: |
| 98 | |
| 99 | \begin{itemize} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 100 | \item Most non-\mimetype{multipart} type messages are parsed as a single |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 101 | message object with a string payload. These objects will return |
| 102 | 0 for \method{is_multipart()}. |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 103 | \item One exception is for \mimetype{message/delivery-status} type |
| 104 | messages. Because the body of such messages consist of |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 105 | blocks of headers, \class{Parser} will create a non-multipart |
| 106 | object containing non-multipart subobjects for each header |
| 107 | block. |
Fred Drake | ab9b238 | 2001-10-16 19:22:51 +0000 | [diff] [blame] | 108 | \item Another exception is for \mimetype{message/*} types (more |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 109 | general than \mimetype{message/delivery-status}). These are |
Fred Drake | ab9b238 | 2001-10-16 19:22:51 +0000 | [diff] [blame] | 110 | typically \mimetype{message/rfc822} messages, represented as a |
| 111 | non-multipart object containing a singleton payload which is |
| 112 | another non-multipart \class{Message} instance. |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 113 | \end{itemize} |