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