blob: 1e8597c66f4a6a8101f78df268e592b43bc8233b [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
Barry Warsaw5b9da892002-10-01 01:05:52 +00003 object structure.}
Barry Warsaw5e634632001-09-26 05:23:47 +00004
Barry Warsaw5b9da892002-10-01 01:05:52 +00005Message object structures can be created in one of two ways: they can be
Barry Warsawc5f8fe32001-09-26 22:21:52 +00006created from whole cloth by instantiating \class{Message} objects and
Barry Warsaw5b9da892002-10-01 01:05:52 +00007stringing them together via \method{attach()} and
Barry Warsawc5f8fe32001-09-26 22:21:52 +00008\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
Barry Warsaw5b9da892002-10-01 01:05:52 +000014to you the root \class{Message} instance of the object structure. For
Barry Warsawc5f8fe32001-09-26 22:21:52 +000015simple, 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
Barry Warsaw5b9da892002-10-01 01:05:52 +000017messages, the root object will return \code{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
Barry Warsaw5b9da892002-10-01 01:05:52 +000030the container message. Two modes of parsing are supported,
31\emph{strict} parsing, which will usually reject any non-RFC compliant
32message, and \emph{lax} parsing, which attempts to adjust for common
33MIME formatting problems.
34
35The \module{email.Parser} module also provides a second class, called
36\class{HeaderParser} which can be used if you're only interested in
37the headers of the message. \class{HeaderParser} can be much faster in
38these situations, since it does not attempt to parse the message body,
39instead setting the payload to the raw body as a string.
40\class{HeaderParser} has the same API as the \class{Parser} class.
Barry Warsawc7f8b862001-10-11 15:45:05 +000041
Barry Warsawc5f8fe32001-09-26 22:21:52 +000042\subsubsection{Parser class API}
Barry Warsaw5e634632001-09-26 05:23:47 +000043
Barry Warsaw5b9da892002-10-01 01:05:52 +000044\begin{classdesc}{Parser}{\optional{_class\optional{, strict}}}
45The constructor for the \class{Parser} class takes an optional
Fred Drakeab9b2382001-10-16 19:22:51 +000046argument \var{_class}. This must be a callable factory (such as a
47function or a class), and it is used whenever a sub-message object
48needs to be created. It defaults to \class{Message} (see
49\refmodule{email.Message}). The factory will be called without
Barry Warsaw5e634632001-09-26 05:23:47 +000050arguments.
Barry Warsaw5b9da892002-10-01 01:05:52 +000051
52The optional \var{strict} flag specifies whether strict or lax parsing
53should be performed. Normally, when things like MIME terminating
54boundaries are missing, or when messages contain other formatting
55problems, the \class{Parser} will raise a
56\exception{MessageParseError}. However, when lax parsing is enabled,
Barry Warsaw5db478f2002-10-01 04:33:16 +000057the \class{Parser} will attempt to work around such broken formatting
Barry Warsaw5b9da892002-10-01 01:05:52 +000058to produce a usable message structure (this doesn't mean
59\exception{MessageParseError}s are never raised; some ill-formatted
60messages just can't be parsed). The \var{strict} flag defaults to
61\code{False} since lax parsing usually provides the most convenient
62behavior.
63
64\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +000065\end{classdesc}
66
67The other public \class{Parser} methods are:
68
Barry Warsaw5b9da892002-10-01 01:05:52 +000069\begin{methoddesc}[Parser]{parse}{fp\optional{, headersonly}}
Barry Warsaw5e634632001-09-26 05:23:47 +000070Read all the data from the file-like object \var{fp}, parse the
71resulting text, and return the root message object. \var{fp} must
72support both the \method{readline()} and the \method{read()} methods
73on file-like objects.
74
75The text contained in \var{fp} must be formatted as a block of \rfc{2822}
Barry Warsaw5db478f2002-10-01 04:33:16 +000076style headers and header continuation lines, optionally preceded by a
Barry Warsaw5b9da892002-10-01 01:05:52 +000077envelope header. The header block is terminated either by the
Barry Warsaw5e634632001-09-26 05:23:47 +000078end of the data or by a blank line. Following the header block is the
79body of the message (which may contain MIME-encoded subparts).
Barry Warsaw5b9da892002-10-01 01:05:52 +000080
Barry Warsaw5db478f2002-10-01 04:33:16 +000081Optional \var{headersonly} is as with the \method{parse()} method.
Barry Warsaw5b9da892002-10-01 01:05:52 +000082
83\versionchanged[The \var{headersonly} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +000084\end{methoddesc}
85
Barry Warsaw5b9da892002-10-01 01:05:52 +000086\begin{methoddesc}[Parser]{parsestr}{text\optional{, headersonly}}
Barry Warsaw5e634632001-09-26 05:23:47 +000087Similar to the \method{parse()} method, except it takes a string
88object instead of a file-like object. Calling this method on a string
89is exactly equivalent to wrapping \var{text} in a \class{StringIO}
90instance first and calling \method{parse()}.
Barry Warsaw5b9da892002-10-01 01:05:52 +000091
92Optional \var{headersonly} is a flag specifying whether to stop
93parsing after reading the headers or not. The default is \code{False},
94meaning it parses the entire contents of the file.
95
96\versionchanged[The \var{headersonly} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +000097\end{methoddesc}
98
Barry Warsaw5b9da892002-10-01 01:05:52 +000099Since creating a message object structure from a string or a file
100object is such a common task, two functions are provided as a
101convenience. They are available in the top-level \module{email}
102package namespace.
Barry Warsaw5e634632001-09-26 05:23:47 +0000103
Barry Warsaw5b9da892002-10-01 01:05:52 +0000104\begin{funcdesc}{message_from_string}{s\optional{, _class\optional{, strict}}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000105Return a message object structure from a string. This is exactly
Barry Warsaw5b9da892002-10-01 01:05:52 +0000106equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} and
107\var{strict} are interpreted as with the \class{Parser} class constructor.
108
109\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000110\end{funcdesc}
111
Barry Warsaw5b9da892002-10-01 01:05:52 +0000112\begin{funcdesc}{message_from_file}{fp\optional{, _class\optional{, strict}}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000113Return a message object structure tree from an open file object. This
114is exactly equivalent to \code{Parser().parse(fp)}. Optional
115\var{_class} and \var{strict} are interpreted as with the
116\class{Parser} class constructor.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000117
118\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000119\end{funcdesc}
120
121Here's an example of how you might use this at an interactive Python
122prompt:
123
124\begin{verbatim}
125>>> import email
126>>> msg = email.message_from_string(myString)
127\end{verbatim}
128
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000129\subsubsection{Additional notes}
Barry Warsaw5e634632001-09-26 05:23:47 +0000130
131Here are some notes on the parsing semantics:
132
133\begin{itemize}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000134\item Most non-\mimetype{multipart} type messages are parsed as a single
Barry Warsaw5e634632001-09-26 05:23:47 +0000135 message object with a string payload. These objects will return
Barry Warsaw5b9da892002-10-01 01:05:52 +0000136 \code{False} for \method{is_multipart()}. Their
137 \method{get_payload()} method will return a string object.
Barry Warsawdd868d32002-10-01 15:29:09 +0000138
Barry Warsaw5b9da892002-10-01 01:05:52 +0000139\item All \mimetype{multipart} type messages will be parsed as a
140 container message object with a list of sub-message objects for
Barry Warsaw5db478f2002-10-01 04:33:16 +0000141 their payload. The outer container message will return
142 \code{True} for \method{is_multipart()} and their
143 \method{get_payload()} method will return the list of
144 \class{Message} subparts.
Barry Warsawdd868d32002-10-01 15:29:09 +0000145
Barry Warsaw5b9da892002-10-01 01:05:52 +0000146\item Most messages with a content type of \mimetype{message/*}
Fred Drake59e02c12004-02-24 20:58:10 +0000147 (e.g. \mimetype{message/delivery-status} and
Barry Warsaw5b9da892002-10-01 01:05:52 +0000148 \mimetype{message/rfc822}) will also be parsed as container
149 object containing a list payload of length 1. Their
150 \method{is_multipart()} method will return \code{True}. The
151 single element in the list payload will be a sub-message object.
Barry Warsaw5e634632001-09-26 05:23:47 +0000152\end{itemize}