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