| Python Library Reference |
One of the most common tasks is to generate the flat text of the emailmessage represented by a message object structure. You will need to dothis if you want to send your message via thesmtplibmodule or thenntplib module, or print the message on theconsole. Taking a message object structure and producing a flat textdocument is the job of theGenerator class.
Again, as with theemail.Parser module, you aren't limitedto the functionality of the bundled generator; you could write onefrom scratch yourself. However the bundled generator knows how togenerate most email in a standards-compliant way, should handle MIMEand non-MIME email messages just fine, and is designed so that thetransformation from flat text, to a message structure via theParser class, and back to flat text, is idempotent (the inputis identical to the output).
Here are the public methods of theGenerator class:
Optionalmangle_from_ is a flag that, whenTrue, 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 beginning of theline. This is the only guaranteed portable way to avoid having suchlines be mistaken for a Unix mailbox format envelope header separator (seeWHY THE CONTENT-LENGTH FORMAT IS BADfor details).mangle_from_ defaults toTrue, but youmight want to set this toFalse if you are not writing Unixmailbox format files.
Optionalmaxheaderlen specifies the longest length for anon-continued header. When a header line is longer thanmaxheaderlen (in characters, with tabs expanded to 8 spaces),the header will be broken on semicolons and continued as perRFC 2822. If no semicolon is found, then the header is left alone.Set to zero to disable wrapping headers. Default is 78, asrecommended (but not required) byRFC 2822.
The other publicGenerator methods are:
Optionalunixfrom is a flag that forces the printing of theenvelope header delimiter before the firstRFC 2822 header of theroot message object. If the root object has no envelope header, astandard one is crafted. By default, this is set toFalse toinhibit the printing of the envelope delimiter.
Note that for subparts, no envelope header is ever printed.
New in version 2.2.2.
New in version 2.2.2.
As a convenience, see the methodsMessage.as_string() andstr(aMessage), a.k.a.Message.__str__(), whichsimplify the generation of a formatted string representation of amessage object. For more detail, seeemail.Message.
Theemail.Generator module also provides a derived class,calledDecodedGenerator which is like theGeneratorbase class, except that non-text parts are substituted witha format string representing the part.
This class, derived fromGenerator walks through all thesubparts of a message. If the subpart is of main typetext, then it prints the decoded payload of the subpart.Optional_mangle_from_ andmaxheaderlen are as with theGenerator base class.
If the subpart is not of main typetext, optionalfmtis a format string that is used instead of the message payload.fmt is expanded with the following keywords, "%(keyword)s"format:
type - Full MIME type of the non-text partmaintype - Main MIME type of the non-text partsubtype - Sub-MIME type of the non-text partfilename - Filename of the non-text partdescription - Description associated with the non-text partencoding - Content transfer encoding of the non-text partThe default value forfmt isNone, meaning
[Non-text (%(type)s) part of message omitted, filename %(filename)s]
New in version 2.2.2.
| Python Library Reference |