blob: 6ded8d12d4bc1c8c06708f8ec694e10d0c1be854 [file] [log] [blame]
Barry Warsaw5e634632001-09-26 05:23:47 +00001\declaremodule{standard}{email.Generator}
Barry Warsawc5f8fe32001-09-26 22:21:52 +00002\modulesynopsis{Generate flat text email messages from a message object tree.}
Barry Warsaw5e634632001-09-26 05:23:47 +00003
Barry Warsawc5f8fe32001-09-26 22:21:52 +00004One of the most common tasks is to generate the flat text of the email
5message represented by a message object tree. You will need to do
6this if you want to send your message via the \refmodule{smtplib}
7module or the \refmodule{nntplib} module, or print the message on the
8console. Taking a message object tree and producing a flat text
9document is the job of the \class{Generator} class.
Barry Warsaw5e634632001-09-26 05:23:47 +000010
Barry Warsawc5f8fe32001-09-26 22:21:52 +000011Again, as with the \refmodule{email.Parser} module, you aren't limited
12to the functionality of the bundled generator; you could write one
13from scratch yourself. However the bundled generator knows how to
14generate most email in a standards-compliant way, should handle MIME
15and non-MIME email messages just fine, and is designed so that the
16transformation from flat text, to an object tree via the
17\class{Parser} class,
18and back to flat text, be idempotent (the input is identical to the
19output).
20
21Here are the public methods of the \class{Generator} class:
Barry Warsaw5e634632001-09-26 05:23:47 +000022
23\begin{classdesc}{Generator}{outfp\optional{, mangle_from_\optional{,
24 maxheaderlen}}}
25The constructor for the \class{Generator} class takes a file-like
26object called \var{outfp} for an argument. \var{outfp} must support
27the \method{write()} method and be usable as the output file in a
28Python 2.0 extended print statement.
29
30Optional \var{mangle_from_} is a flag that, when true, puts a ``>''
31character in front of any line in the body that starts exactly as
32\samp{From } (i.e. \code{From} followed by a space at the front of the
33line). This is the only guaranteed portable way to avoid having such
34lines be mistaken for \emph{Unix-From} headers (see
Barry Warsawc5f8fe32001-09-26 22:21:52 +000035\ulink{WHY THE CONTENT-LENGTH FORMAT IS BAD}
36{http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html}
37for details).
Barry Warsaw5e634632001-09-26 05:23:47 +000038
39Optional \var{maxheaderlen} specifies the longest length for a
40non-continued header. When a header line is longer than
41\var{maxheaderlen} (in characters, with tabs expanded to 8 spaces),
42the header will be broken on semicolons and continued as per
43\rfc{2822}. If no semicolon is found, then the header is left alone.
44Set to zero to disable wrapping headers. Default is 78, as
45recommended (but not required) by \rfc{2822}.
46\end{classdesc}
47
48The other public \class{Generator} methods are:
49
50\begin{methoddesc}[Generator]{__call__}{msg\optional{, unixfrom}}
51Print the textual representation of the message object tree rooted at
52\var{msg} to the output file specified when the \class{Generator}
53instance was created. Sub-objects are visited depth-first and the
54resulting text will be properly MIME encoded.
55
56Optional \var{unixfrom} is a flag that forces the printing of the
57\emph{Unix-From} (a.k.a. envelope header or \code{From_} header)
58delimiter before the first \rfc{2822} header of the root message
59object. If the root object has no \emph{Unix-From} header, a standard
60one is crafted. By default, this is set to 0 to inhibit the printing
61of the \emph{Unix-From} delimiter.
62
63Note that for sub-objects, no \emph{Unix-From} header is ever printed.
64\end{methoddesc}
65
66\begin{methoddesc}[Generator]{write}{s}
67Write the string \var{s} to the underlying file object,
68i.e. \var{outfp} passed to \class{Generator}'s constructor. This
69provides just enough file-like API for \class{Generator} instances to
70be used in extended print statements.
71\end{methoddesc}
72
73As a convenience, see the methods \method{Message.as_string()} and
74\code{str(aMessage)}, a.k.a. \method{Message.__str__()}, which
75simplify the generation of a formatted string representation of a
76message object. For more detail, see \refmodule{email.Message}.