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 |
| 16 | be a string (e.g. containing the text of the message). For MIME |
| 17 | messages, the root object will return 1 from its |
| 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 |
| 25 | message object trees in any way it find necessary. |
| 26 | |
| 27 | \subsubsection{Parser class API} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 28 | |
| 29 | \begin{classdesc}{Parser}{\optional{_class}} |
| 30 | The constructor for the \class{Parser} class takes a single optional |
| 31 | argument \var{_class}. This must be callable factory (i.e. a function |
| 32 | or a class), and it is used whenever a sub-message object needs to be |
| 33 | created. It defaults to \class{Message} (see |
| 34 | \refmodule{email.Message}). \var{_class} will be called with zero |
| 35 | arguments. |
| 36 | \end{classdesc} |
| 37 | |
| 38 | The other public \class{Parser} methods are: |
| 39 | |
| 40 | \begin{methoddesc}[Parser]{parse}{fp} |
| 41 | Read all the data from the file-like object \var{fp}, parse the |
| 42 | resulting text, and return the root message object. \var{fp} must |
| 43 | support both the \method{readline()} and the \method{read()} methods |
| 44 | on file-like objects. |
| 45 | |
| 46 | The text contained in \var{fp} must be formatted as a block of \rfc{2822} |
| 47 | style headers and header continuation lines, optionally preceeded by a |
| 48 | \emph{Unix-From} header. The header block is terminated either by the |
| 49 | end of the data or by a blank line. Following the header block is the |
| 50 | body of the message (which may contain MIME-encoded subparts). |
| 51 | \end{methoddesc} |
| 52 | |
| 53 | \begin{methoddesc}[Parser]{parsestr}{text} |
| 54 | Similar to the \method{parse()} method, except it takes a string |
| 55 | object instead of a file-like object. Calling this method on a string |
| 56 | is exactly equivalent to wrapping \var{text} in a \class{StringIO} |
| 57 | instance first and calling \method{parse()}. |
| 58 | \end{methoddesc} |
| 59 | |
| 60 | Since creating a message object tree from a string or a file object is |
| 61 | such a common task, two functions are provided as a convenience. They |
| 62 | are available in the top-level \module{email} package namespace. |
| 63 | |
| 64 | \begin{funcdesc}{message_from_string}{s\optional{, _class}} |
| 65 | Return a message object tree from a string. This is exactly |
| 66 | equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} is |
| 67 | interpreted as with the \class{Parser} class constructor. |
| 68 | \end{funcdesc} |
| 69 | |
| 70 | \begin{funcdesc}{message_from_file}{fp\optional{, _class}} |
| 71 | Return a message object tree from an open file object. This is exactly |
| 72 | equivalent to \code{Parser().parse(fp)}. Optional \var{_class} is |
| 73 | interpreted as with the \class{Parser} class constructor. |
| 74 | \end{funcdesc} |
| 75 | |
| 76 | Here's an example of how you might use this at an interactive Python |
| 77 | prompt: |
| 78 | |
| 79 | \begin{verbatim} |
| 80 | >>> import email |
| 81 | >>> msg = email.message_from_string(myString) |
| 82 | \end{verbatim} |
| 83 | |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 84 | \subsubsection{Additional notes} |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 85 | |
| 86 | Here are some notes on the parsing semantics: |
| 87 | |
| 88 | \begin{itemize} |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 89 | \item Most non-\mimetype{multipart} type messages are parsed as a single |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 90 | message object with a string payload. These objects will return |
| 91 | 0 for \method{is_multipart()}. |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 92 | \item One exception is for \mimetype{message/delivery-status} type |
| 93 | messages. Because the body of such messages consist of |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 94 | blocks of headers, \class{Parser} will create a non-multipart |
| 95 | object containing non-multipart subobjects for each header |
| 96 | block. |
Barry Warsaw | c5f8fe3 | 2001-09-26 22:21:52 +0000 | [diff] [blame] | 97 | \item Another exception is for \mimetype{message/*} types (i.e. more |
| 98 | general than \mimetype{message/delivery-status}). These are |
| 99 | typically \mimetype{message/rfc822} type messages, represented as a |
Barry Warsaw | 5e63463 | 2001-09-26 05:23:47 +0000 | [diff] [blame] | 100 | non-multipart object containing a singleton payload, another |
| 101 | non-multipart \class{Message} instance. |
| 102 | \end{itemize} |