blob: 5fac92f41f9fccb6383f426087cfd46fb98c673f [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 Warsawbb113862004-10-03 03:16:19 +000021There are actually two parser interfaces available for use, the classic
22\class{Parser} API and the incremental \class{FeedParser} API. The classic
23\class{Parser} API is fine if you have the entire text of the message in
24memory as a string, or if the entire message lives in a file on the file
25system. \class{FeedParser} is more appropriate for when you're reading the
26message from a stream which might block waiting for more input (e.g. reading
27an email message from a socket). The \class{FeedParser} can consume and parse
28the message incrementally, and only returns the root object when you close the
29parser\footnote{As of email package version 3.0, introduced in
30Python 2.4, the classic \class{Parser} was re-implemented in terms of the
31\class{FeedParser}, so the semantics and results are identical between the two
32parsers.}.
33
Barry Warsawc5f8fe32001-09-26 22:21:52 +000034Note that the parser can be extended in limited ways, and of course
35you can implement your own parser completely from scratch. There is
36no magical connection between the \module{email} package's bundled
37parser and the \class{Message} class, so your custom parser can create
Greg Wardf8b1f242002-02-22 21:24:32 +000038message object trees any way it finds necessary.
Barry Warsawc5f8fe32001-09-26 22:21:52 +000039
Barry Warsawbb113862004-10-03 03:16:19 +000040\subsubsection{FeedParser API}
Barry Warsaw5b9da892002-10-01 01:05:52 +000041
Barry Warsawbb113862004-10-03 03:16:19 +000042\versionadded{2.4}
43
44The \class{FeedParser} provides an API that is conducive to incremental
45parsing of email messages, such as would be necessary when reading the text of
46an email message from a source that can block (e.g. a socket). The
47\class{FeedParser} can of course be used to parse an email message fully
48contained in a string or a file, but the classic \class{Parser} API may be
49more convenient for such use cases. The semantics and results of the two
50parser APIs are identical.
51
52The \class{FeedParser}'s API is simple; you create an instance, feed it a
53bunch of text until there's no more to feed it, then close the parser to
54retrieve the root message object. The \class{FeedParser} is extremely
55accurate when parsing standards-compliant messages, and it does a very good
56job of parsing non-compliant messages, providing information about how a
57message was deemed broken. It will populate a message object's \var{defects}
58attribute with a list of any problems it found in a message. See the
59\refmodule{email.Errors} module for the list of defects that it can find.
60
61Here is the API for the \class{FeedParser}:
62
63\begin{classdesc}{FeedParser}{\optional{_factory}}
64Create a \class{FeedParser} instance. Optional \var{_factory} is a
65no-argument callable that will be called whenever a new message object is
66needed. It defaults to the \class{email.Message.Message} class.
67\end{classdesc}
68
69\begin{methoddesc}[FeedParser]{feed}{data}
70Feed the \class{FeedParser} some more data. \var{data} should be a
71string containing one or more lines. The lines can be partial and the
72\class{FeedParser} will stitch such partial lines together properly. The
73lines in the string can have any of the common three line endings, carriage
74return, newline, or carriage return and newline (they can even be mixed).
75\end{methoddesc}
76
77\begin{methoddesc}[FeedParser]{close}{}
78Closing a \class{FeedParser} completes the parsing of all previously fed data,
79and returns the root message object. It is undefined what happens if you feed
80more data to a closed \class{FeedParser}.
81\end{methoddesc}
82
83\subsubsection{Parser class API}
84
85The \class{Parser} provides an API that can be used to parse a message when
86the complete contents of the message are available in a string or file. The
87\module{email.Parser} module also provides a second class, called
Barry Warsaw5b9da892002-10-01 01:05:52 +000088\class{HeaderParser} which can be used if you're only interested in
89the headers of the message. \class{HeaderParser} can be much faster in
90these situations, since it does not attempt to parse the message body,
91instead setting the payload to the raw body as a string.
92\class{HeaderParser} has the same API as the \class{Parser} class.
Barry Warsawc7f8b862001-10-11 15:45:05 +000093
Barry Warsaw5b9da892002-10-01 01:05:52 +000094\begin{classdesc}{Parser}{\optional{_class\optional{, strict}}}
95The constructor for the \class{Parser} class takes an optional
Fred Drakeab9b2382001-10-16 19:22:51 +000096argument \var{_class}. This must be a callable factory (such as a
97function or a class), and it is used whenever a sub-message object
98needs to be created. It defaults to \class{Message} (see
99\refmodule{email.Message}). The factory will be called without
Barry Warsaw5e634632001-09-26 05:23:47 +0000100arguments.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000101
Barry Warsawbb113862004-10-03 03:16:19 +0000102The optional \var{strict} flag is ignored. \deprecated{2.4}{Because the
103\class{Parser} class is a backward compatible API wrapper around the
104new-in-Python 2.4 \class{FeedParser}, \emph{all} parsing is effectively
105non-strict. You should simply stop passing a \var{strict} flag to the
106\class{Parser} constructor.}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000107
108\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsawbb113862004-10-03 03:16:19 +0000109\versionchanged[The \var{strict} flag was deprecated]{2.4}
Barry Warsaw5e634632001-09-26 05:23:47 +0000110\end{classdesc}
111
112The other public \class{Parser} methods are:
113
Barry Warsaw5b9da892002-10-01 01:05:52 +0000114\begin{methoddesc}[Parser]{parse}{fp\optional{, headersonly}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000115Read all the data from the file-like object \var{fp}, parse the
116resulting text, and return the root message object. \var{fp} must
117support both the \method{readline()} and the \method{read()} methods
118on file-like objects.
119
120The text contained in \var{fp} must be formatted as a block of \rfc{2822}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000121style headers and header continuation lines, optionally preceded by a
Barry Warsaw5b9da892002-10-01 01:05:52 +0000122envelope header. The header block is terminated either by the
Barry Warsaw5e634632001-09-26 05:23:47 +0000123end of the data or by a blank line. Following the header block is the
124body of the message (which may contain MIME-encoded subparts).
Barry Warsaw5b9da892002-10-01 01:05:52 +0000125
Barry Warsaw5db478f2002-10-01 04:33:16 +0000126Optional \var{headersonly} is as with the \method{parse()} method.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000127
128\versionchanged[The \var{headersonly} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000129\end{methoddesc}
130
Barry Warsaw5b9da892002-10-01 01:05:52 +0000131\begin{methoddesc}[Parser]{parsestr}{text\optional{, headersonly}}
Barry Warsaw5e634632001-09-26 05:23:47 +0000132Similar to the \method{parse()} method, except it takes a string
133object instead of a file-like object. Calling this method on a string
134is exactly equivalent to wrapping \var{text} in a \class{StringIO}
135instance first and calling \method{parse()}.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000136
137Optional \var{headersonly} is a flag specifying whether to stop
138parsing after reading the headers or not. The default is \code{False},
139meaning it parses the entire contents of the file.
140
141\versionchanged[The \var{headersonly} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000142\end{methoddesc}
143
Barry Warsaw5b9da892002-10-01 01:05:52 +0000144Since creating a message object structure from a string or a file
145object is such a common task, two functions are provided as a
146convenience. They are available in the top-level \module{email}
147package namespace.
Barry Warsaw5e634632001-09-26 05:23:47 +0000148
Barry Warsaw5b9da892002-10-01 01:05:52 +0000149\begin{funcdesc}{message_from_string}{s\optional{, _class\optional{, strict}}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000150Return a message object structure from a string. This is exactly
Barry Warsaw5b9da892002-10-01 01:05:52 +0000151equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} and
152\var{strict} are interpreted as with the \class{Parser} class constructor.
153
154\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000155\end{funcdesc}
156
Barry Warsaw5b9da892002-10-01 01:05:52 +0000157\begin{funcdesc}{message_from_file}{fp\optional{, _class\optional{, strict}}}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000158Return a message object structure tree from an open file object. This
159is exactly equivalent to \code{Parser().parse(fp)}. Optional
160\var{_class} and \var{strict} are interpreted as with the
161\class{Parser} class constructor.
Barry Warsaw5b9da892002-10-01 01:05:52 +0000162
163\versionchanged[The \var{strict} flag was added]{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +0000164\end{funcdesc}
165
166Here's an example of how you might use this at an interactive Python
167prompt:
168
169\begin{verbatim}
170>>> import email
171>>> msg = email.message_from_string(myString)
172\end{verbatim}
173
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000174\subsubsection{Additional notes}
Barry Warsaw5e634632001-09-26 05:23:47 +0000175
176Here are some notes on the parsing semantics:
177
178\begin{itemize}
Barry Warsawc5f8fe32001-09-26 22:21:52 +0000179\item Most non-\mimetype{multipart} type messages are parsed as a single
Barry Warsaw5e634632001-09-26 05:23:47 +0000180 message object with a string payload. These objects will return
Barry Warsaw5b9da892002-10-01 01:05:52 +0000181 \code{False} for \method{is_multipart()}. Their
182 \method{get_payload()} method will return a string object.
Barry Warsawdd868d32002-10-01 15:29:09 +0000183
Barry Warsaw5b9da892002-10-01 01:05:52 +0000184\item All \mimetype{multipart} type messages will be parsed as a
185 container message object with a list of sub-message objects for
Barry Warsaw5db478f2002-10-01 04:33:16 +0000186 their payload. The outer container message will return
187 \code{True} for \method{is_multipart()} and their
188 \method{get_payload()} method will return the list of
189 \class{Message} subparts.
Barry Warsawdd868d32002-10-01 15:29:09 +0000190
Barry Warsaw5b9da892002-10-01 01:05:52 +0000191\item Most messages with a content type of \mimetype{message/*}
Fred Drake59e02c12004-02-24 20:58:10 +0000192 (e.g. \mimetype{message/delivery-status} and
Barry Warsaw5b9da892002-10-01 01:05:52 +0000193 \mimetype{message/rfc822}) will also be parsed as container
194 object containing a list payload of length 1. Their
195 \method{is_multipart()} method will return \code{True}. The
196 single element in the list payload will be a sub-message object.
Barry Warsawbb113862004-10-03 03:16:19 +0000197
198\item Some non-standards compliant messages may not be internally consistent
199 about their \mimetype{multipart}-edness. Such messages may have a
200 \mailheader{Content-Type} header of type \mimetype{multipart}, but their
201 \method{is_multipart()} method may return \code{False}. If such
202 messages were parsed with the \class{FeedParser}, they will have an
203 instance of the \class{MultipartInvariantViolationDefect} class in their
204 \var{defects} attribute list. See \refmodule{email.Errors} for
205 details.
Barry Warsaw5e634632001-09-26 05:23:47 +0000206\end{itemize}