| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | \declaremodule{standard}{email.Generator} | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | \modulesynopsis{Generate flat text email messages from a message structure.} | 
					
						
							| 
									
										
										
										
											2001-09-26 22:21:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | One of the most common tasks is to generate the flat text of the email | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | message represented by a message object structure.  You will need to do | 
					
						
							| 
									
										
										
										
											2001-09-26 22:21:52 +00:00
										 |  |  | this if you want to send your message via the \refmodule{smtplib} | 
					
						
							|  |  |  | module or the \refmodule{nntplib} module, or print the message on the | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | console.  Taking a message object structure and producing a flat text | 
					
						
							| 
									
										
										
										
											2001-09-26 22:21:52 +00:00
										 |  |  | document is the job of the \class{Generator} class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Again, as with the \refmodule{email.Parser} module, you aren't limited | 
					
						
							|  |  |  | to the functionality of the bundled generator; you could write one | 
					
						
							|  |  |  | from scratch yourself.  However the bundled generator knows how to | 
					
						
							|  |  |  | generate most email in a standards-compliant way, should handle MIME | 
					
						
							|  |  |  | and non-MIME email messages just fine, and is designed so that the | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | transformation from flat text, to a message structure via the | 
					
						
							|  |  |  | \class{Parser} class, and back to flat text, is idempotent (the input | 
					
						
							|  |  |  | is identical to the output). | 
					
						
							| 
									
										
										
										
											2001-09-26 22:21:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Here are the public methods of the \class{Generator} class: | 
					
						
							| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{classdesc}{Generator}{outfp\optional{, mangle_from_\optional{, | 
					
						
							|  |  |  |     maxheaderlen}}} | 
					
						
							|  |  |  | The constructor for the \class{Generator} class takes a file-like | 
					
						
							|  |  |  | object called \var{outfp} for an argument.  \var{outfp} must support | 
					
						
							|  |  |  | the \method{write()} method and be usable as the output file in a | 
					
						
							| 
									
										
										
										
											2002-10-01 04:33:16 +00:00
										 |  |  | Python extended print statement. | 
					
						
							| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | Optional \var{mangle_from_} is a flag that, when \code{True}, puts a | 
					
						
							|  |  |  | \samp{>} character in front of any line in the body that starts exactly as | 
					
						
							| 
									
										
										
										
											2002-10-01 04:33:16 +00:00
										 |  |  | \samp{From }, i.e. \code{From} followed by a space at the beginning of the | 
					
						
							|  |  |  | line.  This is the only guaranteed portable way to avoid having such | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | lines be mistaken for a Unix mailbox format envelope header separator (see | 
					
						
							| 
									
										
										
										
											2001-09-26 22:21:52 +00:00
										 |  |  | \ulink{WHY THE CONTENT-LENGTH FORMAT IS BAD} | 
					
						
							|  |  |  | {http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html} | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | for details).  \var{mangle_from_} defaults to \code{True}, but you | 
					
						
							|  |  |  | might want to set this to \code{False} if you are not writing Unix | 
					
						
							|  |  |  | mailbox format files. | 
					
						
							| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Optional \var{maxheaderlen} specifies the longest length for a | 
					
						
							|  |  |  | non-continued header.  When a header line is longer than | 
					
						
							|  |  |  | \var{maxheaderlen} (in characters, with tabs expanded to 8 spaces), | 
					
						
							|  |  |  | the header will be broken on semicolons and continued as per | 
					
						
							|  |  |  | \rfc{2822}.  If no semicolon is found, then the header is left alone. | 
					
						
							|  |  |  | Set to zero to disable wrapping headers.  Default is 78, as | 
					
						
							|  |  |  | recommended (but not required) by \rfc{2822}. | 
					
						
							|  |  |  | \end{classdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The other public \class{Generator} methods are: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-01 04:33:16 +00:00
										 |  |  | \begin{methoddesc}[Generator]{flatten}{msg\optional{, unixfrom}} | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | Print the textual representation of the message object structure rooted at | 
					
						
							| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | \var{msg} to the output file specified when the \class{Generator} | 
					
						
							| 
									
										
										
										
											2002-10-01 04:33:16 +00:00
										 |  |  | instance was created.  Subparts are visited depth-first and the | 
					
						
							| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | resulting text will be properly MIME encoded. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Optional \var{unixfrom} is a flag that forces the printing of the | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | envelope header delimiter before the first \rfc{2822} header of the | 
					
						
							|  |  |  | root message object.  If the root object has no envelope header, a | 
					
						
							|  |  |  | standard one is crafted.  By default, this is set to \code{False} to | 
					
						
							|  |  |  | inhibit the printing of the envelope delimiter. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-01 04:33:16 +00:00
										 |  |  | Note that for subparts, no envelope header is ever printed. | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \versionadded{2.2.2} | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}[Generator]{clone}{fp} | 
					
						
							|  |  |  | Return an independent clone of this \class{Generator} instance with | 
					
						
							|  |  |  | the exact same options. | 
					
						
							| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | \versionadded{2.2.2} | 
					
						
							| 
									
										
										
										
											2001-09-26 05:23:47 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}[Generator]{write}{s} | 
					
						
							|  |  |  | Write the string \var{s} to the underlying file object, | 
					
						
							|  |  |  | i.e. \var{outfp} passed to \class{Generator}'s constructor.  This | 
					
						
							|  |  |  | provides just enough file-like API for \class{Generator} instances to | 
					
						
							|  |  |  | be used in extended print statements. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | As a convenience, see the methods \method{Message.as_string()} and | 
					
						
							|  |  |  | \code{str(aMessage)}, a.k.a. \method{Message.__str__()}, which | 
					
						
							|  |  |  | simplify the generation of a formatted string representation of a | 
					
						
							|  |  |  | message object.  For more detail, see \refmodule{email.Message}. | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The \module{email.Generator} module also provides a derived class, | 
					
						
							|  |  |  | called \class{DecodedGenerator} which is like the \class{Generator} | 
					
						
							|  |  |  | base class, except that non-\mimetype{text} parts are substituted with | 
					
						
							|  |  |  | a format string representing the part. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{classdesc}{DecodedGenerator}{outfp\optional{, mangle_from_\optional{, | 
					
						
							|  |  |  |     maxheaderlen\optional{, fmt}}}} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This class, derived from \class{Generator} walks through all the | 
					
						
							|  |  |  | subparts of a message.  If the subpart is of main type | 
					
						
							|  |  |  | \mimetype{text}, then it prints the decoded payload of the subpart. | 
					
						
							|  |  |  | Optional \var{_mangle_from_} and \var{maxheaderlen} are as with the | 
					
						
							|  |  |  | \class{Generator} base class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If the subpart is not of main type \mimetype{text}, optional \var{fmt} | 
					
						
							| 
									
										
										
										
											2002-10-01 04:33:16 +00:00
										 |  |  | is a format string that is used instead of the message payload. | 
					
						
							|  |  |  | \var{fmt} is expanded with the following keywords, \samp{\%(keyword)s} | 
					
						
							|  |  |  | format: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{itemize} | 
					
						
							|  |  |  | \item \code{type} -- Full MIME type of the non-\mimetype{text} part | 
					
						
							|  |  |  | \item \code{maintype} -- Main MIME type of the non-\mimetype{text} part | 
					
						
							|  |  |  | \item \code{subtype} -- Sub-MIME type of the non-\mimetype{text} part | 
					
						
							|  |  |  | \item \code{filename} -- Filename of the non-\mimetype{text} part | 
					
						
							|  |  |  | \item \code{description} -- Description associated with the | 
					
						
							|  |  |  |       non-\mimetype{text} part | 
					
						
							|  |  |  | \item \code{encoding} -- Content transfer encoding of the | 
					
						
							|  |  |  |       non-\mimetype{text} part | 
					
						
							|  |  |  | \end{itemize} | 
					
						
							| 
									
										
										
										
											2002-10-01 01:05:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The default value for \var{fmt} is \code{None}, meaning | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | [Non-text (%(type)s) part of message omitted, filename %(filename)s]
 | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \versionadded{2.2.2} | 
					
						
							|  |  |  | \end{classdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \subsubsection{Deprecated methods} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The following methods are deprecated in \module{email} version 2. | 
					
						
							|  |  |  | They are documented here for completeness. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}[Generator]{__call__}{msg\optional{, unixfrom}} | 
					
						
							|  |  |  | This method is identical to the \method{flatten()} method. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \deprecated{2.2.2}{Use the \method{flatten()} method instead.} | 
					
						
							|  |  |  | \end{methoddesc} |