class email.generator.Generator(outfp, mangle_from_=True, maxheaderlen=78, *, policy=None)
The constructor for the Generator
class takes a file-like object called outfp for an argument. outfp must support the write()
method and be usable as the output file for the print()
function.
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 beginning of the line. This is the only guaranteed portable way to avoid having such lines be mistaken for a Unix mailbox format envelope header separator (see WHY THE CONTENT-LENGTH FORMAT IS BAD for details). mangle_from_ defaults to True
, but you might want to set this to False
if you are not writing Unix mailbox format files.
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 split as defined in the Header
class. Set to zero to disable header wrapping. The default is 78, as recommended (but not required) by RFC 2822.
The policy keyword specifies a policy
object that controls a number of aspects of the generator’s operation. If no policy is specified, then the policy attached to the message object passed to flatten
is used.
Changed in version 3.3: Added the policy keyword.
The other public Generator
methods are:
-
flatten(msg, unixfrom=False, linesep=None)
-
Print the textual representation of the message object structure rooted at msg to the output file specified when the
Generator
instance was created. Subparts are visited depth-first and the resulting text will be properly MIME encoded.Optional unixfrom is a flag that forces the printing of the 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
False
to inhibit the printing of the envelope delimiter.Note that for subparts, no envelope header is ever printed.
Optional linesep specifies the line separator character used to terminate lines in the output. If specified it overrides the value specified by the msg‘s or
Generator
‘spolicy
.Because strings cannot represent non-ASCII bytes, if the policy that applies when
flatten
is run hascte_type
set to8bit
,Generator
will operate as if it were set to7bit
. This means that messages parsed with a Bytes parser that have a Content-Transfer-Encoding of8bit
will be converted to a use a7bit
Content-Transfer-Encoding. Non-ASCII bytes in the headers will be RFC 2047 encoded with a charset ofunknown-8bit
.Changed in version 3.2: Added support for re-encoding
8bit
message bodies, and the linesep argument.
-
clone(fp)
-
Return an independent clone of this
Generator
instance with the exact same options.
-
write(s)
-
Write the string s to the underlying file object, i.e. outfp passed to
Generator
‘s constructor. This provides just enough file-like API forGenerator
instances to be used in theprint()
function.
Please login to continue.