One of the most common tasks is to generate the flat text of the email message represented by a message object tree. You will need to do this if you want to send your message via the smtplib module or the nntplib module, or print the message on the console. Taking a message object tree and producing a flat text document is the job of the Generator class.
Again, as with the 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 transformation from flat text, to an object tree via the Parser class, and back to flat text, is idempotent (the input is identical to the output).
Here are the public methods of the Generator class:
Optional mangle_from_ is a flag that, when true, puts a ">"character in front of any line in the body that starts exactly as
"From " (i.e. From
followed by a space at the front of the
line). This is the only guaranteed portable way to avoid having such
lines be mistaken for Unix-From headers (see
WHY THE CONTENT-LENGTH FORMAT IS BAD
for details).
Optional maxheaderlen specifies the longest length for a non-continued header. When a header line is longer than 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.
The other public Generator methods are:
Optional unixfrom is a flag that forces the printing of the
Unix-From (a.k.a. envelope header or From_
header)
delimiter before the first RFC 2822 header of the root message
object. If the root object has no Unix-From header, a standard
one is crafted. By default, this is set to 0 to inhibit the printing
of the Unix-From delimiter.
Note that for sub-objects, no Unix-From header is ever printed.
As a convenience, see the methods Message.as_string() and
str(aMessage)
, a.k.a. Message.__str__(), which
simplify the generation of a formatted string representation of a
message object. For more detail, see email.Message.
See About this document... for information on suggesting changes.