<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>18.1.3. email: Generating MIME documents — Python v2.6.6 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.6.6',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v2.6.6 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="top" title="Python v2.6.6 documentation" href="../index.html" />
<link rel="up" title="18.1. email — An email and MIME handling package" href="email.html" />
<link rel="next" title="18.1.4. email: Creating email and MIME objects from scratch" href="email.mime.html" />
<link rel="prev" title="18.1.2. email: Parsing email messages" href="email.parser.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="email.mime.html" title="18.1.4. email: Creating email and MIME objects from scratch"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="email.parser.html" title="18.1.2. email: Parsing email messages"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="netdata.html" >18. Internet Data Handling</a> »</li>
<li><a href="email.html" accesskey="U">18.1. <tt class="docutils literal docutils literal docutils literal docutils literal docutils literal docutils literal"><span class="pre">email</span></tt> — An email and MIME handling package</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-email.generator">
<h1>18.1.3. <a title="Package supporting the parsing, manipulating, and generating email messages, including MIME documents." class="reference external" href="email.html#module-email"><tt class="xref docutils literal"><span class="pre">email</span></tt></a>: Generating MIME documents<a class="headerlink" href="#module-email.generator" title="Permalink to this headline">¶</a></h1>
<p>One of the most common tasks is to generate the flat text of the email message
represented by a message object structure. You will need to do this if you want
to send your message via the <a title="SMTP protocol client (requires sockets)." class="reference external" href="smtplib.html#module-smtplib"><tt class="xref docutils literal"><span class="pre">smtplib</span></tt></a> module or the <a title="NNTP protocol client (requires sockets)." class="reference external" href="nntplib.html#module-nntplib"><tt class="xref docutils literal"><span class="pre">nntplib</span></tt></a> module,
or print the message on the console. Taking a message object structure and
producing a flat text document is the job of the <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> class.</p>
<p>Again, as with the <a title="Parse flat text email messages to produce a message object structure." class="reference external" href="email.parser.html#module-email.parser"><tt class="xref docutils literal"><span class="pre">email.parser</span></tt></a> 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 a message
structure via the <a title="email.parser.Parser" class="reference external" href="email.parser.html#email.parser.Parser"><tt class="xref docutils literal"><span class="pre">Parser</span></tt></a> class, and back to flat text,
is idempotent (the input is identical to the output). On the other hand, using
the Generator on a <a title="email.message.Message" class="reference external" href="email.message.html#email.message.Message"><tt class="xref docutils literal"><span class="pre">Message</span></tt></a> constructed by program may
result in changes to the <a title="email.message.Message" class="reference external" href="email.message.html#email.message.Message"><tt class="xref docutils literal"><span class="pre">Message</span></tt></a> object as defaults are
filled in.</p>
<p>Here are the public methods of the <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> class, imported from the
<tt class="xref docutils literal"><span class="pre">email.generator</span></tt> module:</p>
<dl class="class">
<dt id="email.generator.Generator">
<em class="property">class </em><tt class="descclassname">email.generator.</tt><tt class="descname">Generator</tt><big>(</big><em>outfp</em><span class="optional">[</span>, <em>mangle_from_</em><span class="optional">[</span>, <em>maxheaderlen</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#email.generator.Generator" title="Permalink to this definition">¶</a></dt>
<dd><p>The constructor for the <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> class takes a file-like object called
<em>outfp</em> for an argument. <em>outfp</em> must support the <a title="email.generator.Generator.write" class="reference internal" href="#email.generator.Generator.write"><tt class="xref docutils literal"><span class="pre">write()</span></tt></a> method and be
usable as the output file in a Python extended print statement.</p>
<p>Optional <em>mangle_from_</em> is a flag that, when <tt class="xref docutils literal"><span class="pre">True</span></tt>, puts a <tt class="docutils literal"><span class="pre">></span></tt> character in
front of any line in the body that starts exactly as <tt class="docutils literal"><span class="pre">From</span></tt>, i.e. <tt class="docutils literal"><span class="pre">From</span></tt>
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 <a class="reference external" href="http://www.jwz.org/doc/content-length.html">WHY THE CONTENT-LENGTH FORMAT IS BAD</a> for details). <em>mangle_from_</em>
defaults to <tt class="xref docutils literal"><span class="pre">True</span></tt>, but you might want to set this to <tt class="xref docutils literal"><span class="pre">False</span></tt> if you are not
writing Unix mailbox format files.</p>
<p>Optional <em>maxheaderlen</em> specifies the longest length for a non-continued header.
When a header line is longer than <em>maxheaderlen</em> (in characters, with tabs
expanded to 8 spaces), the header will be split as defined in the
<a title="email.header.Header" class="reference external" href="email.header.html#email.header.Header"><tt class="xref docutils literal"><span class="pre">Header</span></tt></a> class. Set to zero to disable header wrapping.
The default is 78, as recommended (but not required) by <span class="target" id="index-293"></span><a class="reference external" href="http://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a>.</p>
<p>The other public <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> methods are:</p>
<dl class="method">
<dt id="email.generator.Generator.flatten">
<tt class="descname">flatten</tt><big>(</big><em>msg</em><span class="optional">[</span>, <em>unixfrom</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#email.generator.Generator.flatten" title="Permalink to this definition">¶</a></dt>
<dd><p>Print the textual representation of the message object structure rooted at
<em>msg</em> to the output file specified when the <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> instance
was created. Subparts are visited depth-first and the resulting text will
be properly MIME encoded.</p>
<p>Optional <em>unixfrom</em> is a flag that forces the printing of the envelope
header delimiter before the first <span class="target" id="index-294"></span><a class="reference external" href="http://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a> 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 <tt class="xref docutils literal"><span class="pre">False</span></tt> to inhibit the printing of
the envelope delimiter.</p>
<p>Note that for subparts, no envelope header is ever printed.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.2.2.</span></p>
</dd></dl>
<dl class="method">
<dt id="email.generator.Generator.clone">
<tt class="descname">clone</tt><big>(</big><em>fp</em><big>)</big><a class="headerlink" href="#email.generator.Generator.clone" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an independent clone of this <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> instance with the
exact same options.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.2.2.</span></p>
</dd></dl>
<dl class="method">
<dt id="email.generator.Generator.write">
<tt class="descname">write</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#email.generator.Generator.write" title="Permalink to this definition">¶</a></dt>
<dd>Write the string <em>s</em> to the underlying file object, i.e. <em>outfp</em> passed to
<a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a>‘s constructor. This provides just enough file-like API
for <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> instances to be used in extended print statements.</dd></dl>
</dd></dl>
<p>As a convenience, see the methods <tt class="xref docutils literal"><span class="pre">Message.as_string()</span></tt> and
<tt class="docutils literal"><span class="pre">str(aMessage)</span></tt>, a.k.a. <tt class="xref docutils literal"><span class="pre">Message.__str__()</span></tt>, which simplify the generation
of a formatted string representation of a message object. For more detail, see
<a title="The base class representing email messages." class="reference external" href="email.message.html#module-email.message"><tt class="xref docutils literal"><span class="pre">email.message</span></tt></a>.</p>
<p>The <tt class="xref docutils literal"><span class="pre">email.generator</span></tt> module also provides a derived class, called
<a title="email.generator.DecodedGenerator" class="reference internal" href="#email.generator.DecodedGenerator"><tt class="xref docutils literal"><span class="pre">DecodedGenerator</span></tt></a> which is like the <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> base class,
except that non-<em>text</em> parts are substituted with a format string
representing the part.</p>
<dl class="class">
<dt id="email.generator.DecodedGenerator">
<em class="property">class </em><tt class="descclassname">email.generator.</tt><tt class="descname">DecodedGenerator</tt><big>(</big><em>outfp</em><span class="optional">[</span>, <em>mangle_from_</em><span class="optional">[</span>, <em>maxheaderlen</em><span class="optional">[</span>, <em>fmt</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#email.generator.DecodedGenerator" title="Permalink to this definition">¶</a></dt>
<dd><p>This class, derived from <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> walks through all the subparts of a
message. If the subpart is of main type <em>text</em>, then it prints the
decoded payload of the subpart. Optional <em>_mangle_from_</em> and <em>maxheaderlen</em> are
as with the <a title="email.generator.Generator" class="reference internal" href="#email.generator.Generator"><tt class="xref docutils literal"><span class="pre">Generator</span></tt></a> base class.</p>
<p>If the subpart is not of main type <em>text</em>, optional <em>fmt</em> is a format
string that is used instead of the message payload. <em>fmt</em> is expanded with the
following keywords, <tt class="docutils literal"><span class="pre">%(keyword)s</span></tt> format:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">type</span></tt> – Full MIME type of the non-<em>text</em> part</li>
<li><tt class="docutils literal"><span class="pre">maintype</span></tt> – Main MIME type of the non-<em>text</em> part</li>
<li><tt class="docutils literal"><span class="pre">subtype</span></tt> – Sub-MIME type of the non-<em>text</em> part</li>
<li><tt class="docutils literal"><span class="pre">filename</span></tt> – Filename of the non-<em>text</em> part</li>
<li><tt class="docutils literal"><span class="pre">description</span></tt> – Description associated with the non-<em>text</em> part</li>
<li><tt class="docutils literal"><span class="pre">encoding</span></tt> – Content transfer encoding of the non-<em>text</em> part</li>
</ul>
<p>The default value for <em>fmt</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, meaning</p>
<div class="highlight-python"><pre>[Non-text (%(type)s) part of message omitted, filename %(filename)s]</pre>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 2.2.2.</span></p>
</dd></dl>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.5: </span>The previously deprecated method <a title="object.__call__" class="reference external" href="../reference/datamodel.html#object.__call__"><tt class="xref docutils literal"><span class="pre">__call__()</span></tt></a> was removed.</p>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="email.parser.html"
title="previous chapter">18.1.2. <tt class="docutils literal"><span class="pre">email</span></tt>: Parsing email messages</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="email.mime.html"
title="next chapter">18.1.4. <tt class="docutils literal"><span class="pre">email</span></tt>: Creating email and MIME objects from scratch</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li><a href="../_sources/library/email.generator.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../modindex.html" title="Global Module Index"
>modules</a> |</li>
<li class="right" >
<a href="email.mime.html" title="18.1.4. email: Creating email and MIME objects from scratch"
>next</a> |</li>
<li class="right" >
<a href="email.parser.html" title="18.1.2. email: Parsing email messages"
>previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="netdata.html" >18. Internet Data Handling</a> »</li>
<li><a href="email.html" >18.1. <tt class="docutils literal docutils literal docutils literal docutils literal docutils literal docutils literal"><span class="pre">email</span></tt> — An email and MIME handling package</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="http://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on Jul 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez