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