blob: 96eb2687a9e45ae6f7c33d7168c99045b4f9e20e [file] [log] [blame]
Barry Warsaw5e634632001-09-26 05:23:47 +00001\declaremodule{standard}{email.Generator}
Barry Warsaw5b9da892002-10-01 01:05:52 +00002\modulesynopsis{Generate flat text email messages from a message structure.}
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
Barry Warsaw5b9da892002-10-01 01:05:52 +00005message represented by a message object structure. You will need to do
Barry Warsawc5f8fe32001-09-26 22:21:52 +00006this if you want to send your message via the \refmodule{smtplib}
7module or the \refmodule{nntplib} module, or print the message on the
Barry Warsaw5b9da892002-10-01 01:05:52 +00008console. Taking a message object structure and producing a flat text
Barry Warsawc5f8fe32001-09-26 22:21:52 +00009document 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
Barry Warsaw5b9da892002-10-01 01:05:52 +000016transformation from flat text, to a message structure via the
17\class{Parser} class, and back to flat text, is idempotent (the input
18is identical to the output).
Barry Warsawc5f8fe32001-09-26 22:21:52 +000019
20Here are the public methods of the \class{Generator} class:
Barry Warsaw5e634632001-09-26 05:23:47 +000021
22\begin{classdesc}{Generator}{outfp\optional{, mangle_from_\optional{,
23 maxheaderlen}}}
24The constructor for the \class{Generator} class takes a file-like
25object called \var{outfp} for an argument. \var{outfp} must support
26the \method{write()} method and be usable as the output file in a
Barry Warsaw5db478f2002-10-01 04:33:16 +000027Python extended print statement.
Barry Warsaw5e634632001-09-26 05:23:47 +000028
Barry Warsaw5b9da892002-10-01 01:05:52 +000029Optional \var{mangle_from_} is a flag that, when \code{True}, puts a
30\samp{>} character in front of any line in the body that starts exactly as
Barry Warsaw5db478f2002-10-01 04:33:16 +000031\samp{From }, i.e. \code{From} followed by a space at the beginning of the
32line. This is the only guaranteed portable way to avoid having such
Barry Warsaw5b9da892002-10-01 01:05:52 +000033lines be mistaken for a Unix mailbox format envelope header separator (see
Barry Warsawc5f8fe32001-09-26 22:21:52 +000034\ulink{WHY THE CONTENT-LENGTH FORMAT IS BAD}
35{http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html}
Barry Warsaw5b9da892002-10-01 01:05:52 +000036for details). \var{mangle_from_} defaults to \code{True}, but you
37might want to set this to \code{False} if you are not writing Unix
38mailbox format files.
Barry Warsaw5e634632001-09-26 05:23:47 +000039
40Optional \var{maxheaderlen} specifies the longest length for a
41non-continued header. When a header line is longer than
42\var{maxheaderlen} (in characters, with tabs expanded to 8 spaces),
43the header will be broken on semicolons and continued as per
44\rfc{2822}. If no semicolon is found, then the header is left alone.
45Set to zero to disable wrapping headers. Default is 78, as
46recommended (but not required) by \rfc{2822}.
47\end{classdesc}
48
49The other public \class{Generator} methods are:
50
Barry Warsaw5db478f2002-10-01 04:33:16 +000051\begin{methoddesc}[Generator]{flatten}{msg\optional{, unixfrom}}
Barry Warsaw5b9da892002-10-01 01:05:52 +000052Print the textual representation of the message object structure rooted at
Barry Warsaw5e634632001-09-26 05:23:47 +000053\var{msg} to the output file specified when the \class{Generator}
Barry Warsaw5db478f2002-10-01 04:33:16 +000054instance was created. Subparts are visited depth-first and the
Barry Warsaw5e634632001-09-26 05:23:47 +000055resulting text will be properly MIME encoded.
56
57Optional \var{unixfrom} is a flag that forces the printing of the
Barry Warsaw5b9da892002-10-01 01:05:52 +000058envelope header delimiter before the first \rfc{2822} header of the
59root message object. If the root object has no envelope header, a
60standard one is crafted. By default, this is set to \code{False} to
61inhibit the printing of the envelope delimiter.
Barry Warsaw5e634632001-09-26 05:23:47 +000062
Barry Warsaw5db478f2002-10-01 04:33:16 +000063Note that for subparts, no envelope header is ever printed.
Barry Warsaw5b9da892002-10-01 01:05:52 +000064
65\versionadded{2.2.2}
66\end{methoddesc}
67
68\begin{methoddesc}[Generator]{clone}{fp}
69Return an independent clone of this \class{Generator} instance with
70the exact same options.
71
72\versionadded{2.2.2}
Barry Warsaw5e634632001-09-26 05:23:47 +000073\end{methoddesc}
74
75\begin{methoddesc}[Generator]{write}{s}
76Write the string \var{s} to the underlying file object,
77i.e. \var{outfp} passed to \class{Generator}'s constructor. This
78provides just enough file-like API for \class{Generator} instances to
79be used in extended print statements.
80\end{methoddesc}
81
82As a convenience, see the methods \method{Message.as_string()} and
83\code{str(aMessage)}, a.k.a. \method{Message.__str__()}, which
84simplify the generation of a formatted string representation of a
85message object. For more detail, see \refmodule{email.Message}.
Barry Warsaw5b9da892002-10-01 01:05:52 +000086
87The \module{email.Generator} module also provides a derived class,
88called \class{DecodedGenerator} which is like the \class{Generator}
89base class, except that non-\mimetype{text} parts are substituted with
90a format string representing the part.
91
92\begin{classdesc}{DecodedGenerator}{outfp\optional{, mangle_from_\optional{,
93 maxheaderlen\optional{, fmt}}}}
94
95This class, derived from \class{Generator} walks through all the
96subparts of a message. If the subpart is of main type
97\mimetype{text}, then it prints the decoded payload of the subpart.
98Optional \var{_mangle_from_} and \var{maxheaderlen} are as with the
99\class{Generator} base class.
100
101If the subpart is not of main type \mimetype{text}, optional \var{fmt}
Barry Warsaw5db478f2002-10-01 04:33:16 +0000102is a format string that is used instead of the message payload.
103\var{fmt} is expanded with the following keywords, \samp{\%(keyword)s}
104format:
Barry Warsaw5b9da892002-10-01 01:05:52 +0000105
Barry Warsaw5db478f2002-10-01 04:33:16 +0000106\begin{itemize}
107\item \code{type} -- Full MIME type of the non-\mimetype{text} part
Barry Warsawdd868d32002-10-01 15:29:09 +0000108
Barry Warsaw5db478f2002-10-01 04:33:16 +0000109\item \code{maintype} -- Main MIME type of the non-\mimetype{text} part
Barry Warsawdd868d32002-10-01 15:29:09 +0000110
Barry Warsaw5db478f2002-10-01 04:33:16 +0000111\item \code{subtype} -- Sub-MIME type of the non-\mimetype{text} part
Barry Warsawdd868d32002-10-01 15:29:09 +0000112
Barry Warsaw5db478f2002-10-01 04:33:16 +0000113\item \code{filename} -- Filename of the non-\mimetype{text} part
Barry Warsawdd868d32002-10-01 15:29:09 +0000114
Barry Warsaw5db478f2002-10-01 04:33:16 +0000115\item \code{description} -- Description associated with the
116 non-\mimetype{text} part
Barry Warsawdd868d32002-10-01 15:29:09 +0000117
Barry Warsaw5db478f2002-10-01 04:33:16 +0000118\item \code{encoding} -- Content transfer encoding of the
119 non-\mimetype{text} part
Barry Warsawdd868d32002-10-01 15:29:09 +0000120
Barry Warsaw5db478f2002-10-01 04:33:16 +0000121\end{itemize}
Barry Warsaw5b9da892002-10-01 01:05:52 +0000122
123The default value for \var{fmt} is \code{None}, meaning
124
125\begin{verbatim}
126[Non-text (%(type)s) part of message omitted, filename %(filename)s]
127\end{verbatim}
128
129\versionadded{2.2.2}
130\end{classdesc}
131
132\subsubsection{Deprecated methods}
133
134The following methods are deprecated in \module{email} version 2.
135They are documented here for completeness.
136
137\begin{methoddesc}[Generator]{__call__}{msg\optional{, unixfrom}}
138This method is identical to the \method{flatten()} method.
139
140\deprecated{2.2.2}{Use the \method{flatten()} method instead.}
141\end{methoddesc}