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