blob: b5d9900497c09078d3fc34023e52867c8af163a5 [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,
57the \class{Parser} will attempt to workaround such broken formatting
58to 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}
76style headers and header continuation lines, optionally preceeded 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
81Optional \var{headersonly} is a flag specifying whether to stop
82parsing after reading the headers or not. The default is \code{False},
83meaning it parses the entire contents of the file.
84
85\versionchanged[The \var{headersonly} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +000086\end{methoddesc}
87
Barry Warsaw5b9da892002-10-01 01:05:52 +000088\begin{methoddesc}[Parser]{parsestr}{text\optional{, headersonly}}
Barry Warsaw5e634632001-09-26 05:23:47 +000089Similar to the \method{parse()} method, except it takes a string
90object instead of a file-like object. Calling this method on a string
91is exactly equivalent to wrapping \var{text} in a \class{StringIO}
92instance first and calling \method{parse()}.
Barry Warsaw5b9da892002-10-01 01:05:52 +000093
94Optional \var{headersonly} is a flag specifying whether to stop
95parsing after reading the headers or not. The default is \code{False},
96meaning it parses the entire contents of the file.
97
98\versionchanged[The \var{headersonly} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +000099\end{methoddesc}
100
Barry Warsaw5b9da892002-10-01 01:05:52 +0000101Since creating a message object structure from a string or a file
102object is such a common task, two functions are provided as a
103convenience. They are available in the top-level \module{email}
104package namespace.
Barry Warsaw5e634632001-09-26 05:23:47 +0000105
Barry Warsaw5b9da892002-10-01 01:05:52 +0000106\begin{funcdesc}{message_from_string}{s\optional{, _class\optional{, strict}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000107Return a message object tree from a string. This is exactly
Barry Warsaw5b9da892002-10-01 01:05:52 +0000108equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} and
109\var{strict} are interpreted as with the \class{Parser} class constructor.
110
111\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000112\end{funcdesc}
113
Barry Warsaw5b9da892002-10-01 01:05:52 +0000114\begin{funcdesc}{message_from_file}{fp\optional{, _class\optional{, strict}}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000115Return a message object tree from an open file object. This is exactly
Barry Warsaw5b9da892002-10-01 01:05:52 +0000116equivalent to \code{Parser().parse(fp)}. Optional \var{_class} and
117\var{strict} are interpreted as with the \class{Parser} class constructor.
118
119\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000120\end{funcdesc}
121
122Here's an example of how you might use this at an interactive Python
123prompt:
124
125\begin{verbatim}
126>>> import email
127>>> msg = email.message_from_string(myString)
128\end{verbatim}
129
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000130\subsubsection{Additional notes}
Barry Warsaw5e634632001-09-26 05:23:47 +0000131
132Here are some notes on the parsing semantics:
133
134\begin{itemize}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000135\item Most non-\mimetype{multipart} type messages are parsed as a single
Barry Warsaw5e634632001-09-26 05:23:47 +0000136 message object with a string payload. These objects will return
Barry Warsaw5b9da892002-10-01 01:05:52 +0000137 \code{False} for \method{is_multipart()}. Their
138 \method{get_payload()} method will return a string object.
139\item All \mimetype{multipart} type messages will be parsed as a
140 container message object with a list of sub-message objects for
141 their payload. These messages will return \code{True} for
142 \method{is_multipart()} and their \method{get_payload()} method
143 will return a list of \class{Message} instances.
144\item Most messages with a content type of \mimetype{message/*}
145 (e.g. \mimetype{message/deliver-status} and
146 \mimetype{message/rfc822}) will also be parsed as container
147 object containing a list payload of length 1. Their
148 \method{is_multipart()} method will return \code{True}. The
149 single element in the list payload will be a sub-message object.
Barry Warsaw5e634632001-09-26 05:23:47 +0000150\end{itemize}