blob: c96c3b32e28c3adb44468c6fe2bd6f36e22ad1dc [file] [log] [blame]
Barry Warsaw5e634632001-09-26 05:23:47 +00001\section{\module{email.Parser} ---
2 Parsing flat text email messages}
3
4\declaremodule{standard}{email.Parser}
5\modulesynopsis{Parse flat text email messages to produce a message
6 object tree.}
7\sectionauthor{Barry A. Warsaw}{barry@zope.com}
8
9\versionadded{2.2}
10
11The \module{Parser} module provides a single class, the \class{Parser}
12class, which is used to take a message in flat text form and create
13the associated object model. The resulting object tree can then be
14manipulated using the \class{Message} class interface as described in
15\refmodule{email.Message}, and turned over
16to a generator (as described in \refmodule{emamil.Generator}) to
17return the textual representation of the message. It is intended that
18the \class{Parser} to \class{Generator} path be idempotent if the
19object model isn't modified in between.
20
21\subsection{Parser class API}
22
23\begin{classdesc}{Parser}{\optional{_class}}
24The constructor for the \class{Parser} class takes a single optional
25argument \var{_class}. This must be callable factory (i.e. a function
26or a class), and it is used whenever a sub-message object needs to be
27created. It defaults to \class{Message} (see
28\refmodule{email.Message}). \var{_class} will be called with zero
29arguments.
30\end{classdesc}
31
32The other public \class{Parser} methods are:
33
34\begin{methoddesc}[Parser]{parse}{fp}
35Read all the data from the file-like object \var{fp}, parse the
36resulting text, and return the root message object. \var{fp} must
37support both the \method{readline()} and the \method{read()} methods
38on file-like objects.
39
40The text contained in \var{fp} must be formatted as a block of \rfc{2822}
41style headers and header continuation lines, optionally preceeded by a
42\emph{Unix-From} header. The header block is terminated either by the
43end of the data or by a blank line. Following the header block is the
44body of the message (which may contain MIME-encoded subparts).
45\end{methoddesc}
46
47\begin{methoddesc}[Parser]{parsestr}{text}
48Similar to the \method{parse()} method, except it takes a string
49object instead of a file-like object. Calling this method on a string
50is exactly equivalent to wrapping \var{text} in a \class{StringIO}
51instance first and calling \method{parse()}.
52\end{methoddesc}
53
54Since creating a message object tree from a string or a file object is
55such a common task, two functions are provided as a convenience. They
56are available in the top-level \module{email} package namespace.
57
58\begin{funcdesc}{message_from_string}{s\optional{, _class}}
59Return a message object tree from a string. This is exactly
60equivalent to \code{Parser().parsestr(s)}. Optional \var{_class} is
61interpreted as with the \class{Parser} class constructor.
62\end{funcdesc}
63
64\begin{funcdesc}{message_from_file}{fp\optional{, _class}}
65Return a message object tree from an open file object. This is exactly
66equivalent to \code{Parser().parse(fp)}. Optional \var{_class} is
67interpreted as with the \class{Parser} class constructor.
68\end{funcdesc}
69
70Here's an example of how you might use this at an interactive Python
71prompt:
72
73\begin{verbatim}
74>>> import email
75>>> msg = email.message_from_string(myString)
76\end{verbatim}
77
78\subsection{Additional notes}
79
80Here are some notes on the parsing semantics:
81
82\begin{itemize}
83\item Most non-\code{multipart} type messages are parsed as a single
84 message object with a string payload. These objects will return
85 0 for \method{is_multipart()}.
86\item One exception is for \code{message/delivery-status} type
87 messages. Because such the body of such messages consist of
88 blocks of headers, \class{Parser} will create a non-multipart
89 object containing non-multipart subobjects for each header
90 block.
91\item Another exception is for \code{message/*} types (i.e. more
92 general than \code{message/delivery-status}. These are
93 typically \code{message/rfc822} type messages, represented as a
94 non-multipart object containing a singleton payload, another
95 non-multipart \class{Message} instance.
96\end{itemize}