Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:12.2.2.2 Additional notesUp:12.2 emailNext:12.2.3.1 Deprecated methods

12.2.3 Generating MIME documents

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:

classGenerator(outfp[, mangle_from_[, maxheaderlen]])
The constructor for theGenerator class takes a file-likeobject calledoutfp for an argument.outfp must supportthewrite() method and be usable as the output file in aPython extended print statement.

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:

flatten(msg[, unixfrom])
Print the textual representation of the message object structure rooted atmsg to the output file specified when theGeneratorinstance was created. Subparts are visited depth-first and theresulting text will be properly MIME encoded.

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.

clone(fp)
Return an independent clone of thisGenerator instance withthe exact same options.

New in version 2.2.2.

write(s)
Write the strings to the underlying file object,i.e.outfp passed toGenerator's constructor. Thisprovides just enough file-like API forGenerator instances tobe used in extended print statements.

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.

classDecodedGenerator(outfp[, mangle_from_[, maxheaderlen[, fmt]]])

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:

The default value forfmt isNone, meaning

[Non-text (%(type)s) part of message omitted, filename %(filename)s]

New in version 2.2.2.


Subsections


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:12.2.2.2 Additional notesUp:12.2 emailNext:12.2.3.1 Deprecated methods
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp