blob: 2cb58ecd2c384541022053a1377a5ac93183d275 [file] [log] [blame]
Barry Warsaw5e634632001-09-26 05:23:47 +00001\section{\module{email.Generator} ---
2 Generating flat text from an email message object tree}
3
4\declaremodule{standard}{email.Generator}
5\modulesynopsis{Generate flat text email messages to from a message
6 object tree.}
7\sectionauthor{Barry A. Warsaw}{barry@zope.com}
8
9\versionadded{2.2}
10
11The \class{Generator} class is used to render a message object model
12into its flat text representation, including MIME encoding any
13sub-messages, generating the correct \rfc{2822} headers, etc. Here
14are the public methods of the \class{Generator} class.
15
16\begin{classdesc}{Generator}{outfp\optional{, mangle_from_\optional{,
17 maxheaderlen}}}
18The constructor for the \class{Generator} class takes a file-like
19object called \var{outfp} for an argument. \var{outfp} must support
20the \method{write()} method and be usable as the output file in a
21Python 2.0 extended print statement.
22
23Optional \var{mangle_from_} is a flag that, when true, puts a ``>''
24character in front of any line in the body that starts exactly as
25\samp{From } (i.e. \code{From} followed by a space at the front of the
26line). This is the only guaranteed portable way to avoid having such
27lines be mistaken for \emph{Unix-From} headers (see
28\url{http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html}
29 for details).
30
31Optional \var{maxheaderlen} specifies the longest length for a
32non-continued header. When a header line is longer than
33\var{maxheaderlen} (in characters, with tabs expanded to 8 spaces),
34the header will be broken on semicolons and continued as per
35\rfc{2822}. If no semicolon is found, then the header is left alone.
36Set to zero to disable wrapping headers. Default is 78, as
37recommended (but not required) by \rfc{2822}.
38\end{classdesc}
39
40The other public \class{Generator} methods are:
41
42\begin{methoddesc}[Generator]{__call__}{msg\optional{, unixfrom}}
43Print the textual representation of the message object tree rooted at
44\var{msg} to the output file specified when the \class{Generator}
45instance was created. Sub-objects are visited depth-first and the
46resulting text will be properly MIME encoded.
47
48Optional \var{unixfrom} is a flag that forces the printing of the
49\emph{Unix-From} (a.k.a. envelope header or \code{From_} header)
50delimiter before the first \rfc{2822} header of the root message
51object. If the root object has no \emph{Unix-From} header, a standard
52one is crafted. By default, this is set to 0 to inhibit the printing
53of the \emph{Unix-From} delimiter.
54
55Note that for sub-objects, no \emph{Unix-From} header is ever printed.
56\end{methoddesc}
57
58\begin{methoddesc}[Generator]{write}{s}
59Write the string \var{s} to the underlying file object,
60i.e. \var{outfp} passed to \class{Generator}'s constructor. This
61provides just enough file-like API for \class{Generator} instances to
62be used in extended print statements.
63\end{methoddesc}
64
65As a convenience, see the methods \method{Message.as_string()} and
66\code{str(aMessage)}, a.k.a. \method{Message.__str__()}, which
67simplify the generation of a formatted string representation of a
68message object. For more detail, see \refmodule{email.Message}.