~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.2-docs-html/library/email.generator.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>19.1.3. email.generator: Generating MIME documents &mdash; Python 3.5.2 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '3.5.2',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 3.5.2 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
 
33
    <link rel="up" title="19.1. email — An email and MIME handling package" href="email.html" />
 
34
    <link rel="next" title="19.1.4. email.policy: Policy Objects" href="email.policy.html" />
 
35
    <link rel="prev" title="19.1.2. email.parser: Parsing email messages" href="email.parser.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    
 
38
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
39
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
40
    
 
41
    
 
42
 
 
43
 
 
44
  </head>
 
45
  <body role="document">  
 
46
    <div class="related" role="navigation" aria-label="related navigation">
 
47
      <h3>Navigation</h3>
 
48
      <ul>
 
49
        <li class="right" style="margin-right: 10px">
 
50
          <a href="../genindex.html" title="General Index"
 
51
             accesskey="I">index</a></li>
 
52
        <li class="right" >
 
53
          <a href="../py-modindex.html" title="Python Module Index"
 
54
             >modules</a> |</li>
 
55
        <li class="right" >
 
56
          <a href="email.policy.html" title="19.1.4. email.policy: Policy Objects"
 
57
             accesskey="N">next</a> |</li>
 
58
        <li class="right" >
 
59
          <a href="email.parser.html" title="19.1.2. email.parser: Parsing email messages"
 
60
             accesskey="P">previous</a> |</li>
 
61
        <li><img src="../_static/py.png" alt=""
 
62
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
63
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
64
        <li>
 
65
          <span class="version_switcher_placeholder">3.5.2</span>
 
66
          <a href="../index.html">Documentation </a> &raquo;
 
67
        </li>
 
68
 
 
69
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
70
          <li class="nav-item nav-item-2"><a href="netdata.html" >19. Internet Data Handling</a> &raquo;</li>
 
71
          <li class="nav-item nav-item-3"><a href="email.html" accesskey="U">19.1. <code class="docutils literal"><span class="pre">email</span></code> &#8212; An email and MIME handling package</a> &raquo;</li>
 
72
    <li class="right">
 
73
        
 
74
 
 
75
    <div class="inline-search" style="display: none" role="search">
 
76
        <form class="inline-search" action="../search.html" method="get">
 
77
          <input placeholder="Quick search" type="text" name="q" />
 
78
          <input type="submit" value="Go" />
 
79
          <input type="hidden" name="check_keywords" value="yes" />
 
80
          <input type="hidden" name="area" value="default" />
 
81
        </form>
 
82
    </div>
 
83
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
84
         |
 
85
    </li>
 
86
 
 
87
      </ul>
 
88
    </div>    
 
89
 
 
90
    <div class="document">
 
91
      <div class="documentwrapper">
 
92
        <div class="bodywrapper">
 
93
          <div class="body" role="main">
 
94
            
 
95
  <div class="section" id="module-email.generator">
 
96
<span id="email-generator-generating-mime-documents"></span><h1>19.1.3. <a class="reference internal" href="#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal"><span class="pre">email.generator</span></code></a>: Generating MIME documents<a class="headerlink" href="#module-email.generator" title="Permalink to this headline">¶</a></h1>
 
97
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/email/generator.py">Lib/email/generator.py</a></p>
 
98
<hr class="docutils" />
 
99
<p>One of the most common tasks is to generate the flat text of the email message
 
100
represented by a message object structure.  You will need to do this if you want
 
101
to send your message via the <a class="reference internal" href="smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">smtplib</span></code></a> module or the <a class="reference internal" href="nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">nntplib</span></code></a> module,
 
102
or print the message on the console.  Taking a message object structure and
 
103
producing a flat text document is the job of the <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> class.</p>
 
104
<p>Again, as with the <a class="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal"><span class="pre">email.parser</span></code></a> module, you aren&#8217;t limited to the
 
105
functionality of the bundled generator; you could write one from scratch
 
106
yourself.  However the bundled generator knows how to generate most email in a
 
107
standards-compliant way, should handle MIME and non-MIME email messages just
 
108
fine, and is designed so that the transformation from flat text, to a message
 
109
structure via the <a class="reference internal" href="email.parser.html#email.parser.Parser" title="email.parser.Parser"><code class="xref py py-class docutils literal"><span class="pre">Parser</span></code></a> class, and back to flat text,
 
110
is idempotent (the input is identical to the output) <a class="footnote-reference" href="#id3" id="id1">[1]</a>.  On the other hand,
 
111
using the Generator on a <a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal"><span class="pre">Message</span></code></a> constructed by program
 
112
may result in changes to the <a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal"><span class="pre">Message</span></code></a> object as defaults
 
113
are filled in.</p>
 
114
<p><a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> output can be generated using the <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a> class.
 
115
If the message object structure contains non-ASCII bytes, this generator&#8217;s
 
116
<a class="reference internal" href="#email.generator.BytesGenerator.flatten" title="email.generator.BytesGenerator.flatten"><code class="xref py py-meth docutils literal"><span class="pre">flatten()</span></code></a> method will emit the original bytes.  Parsing a
 
117
binary message and then flattening it with <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a> should be
 
118
idempotent for standards compliant messages.</p>
 
119
<p>Here are the public methods of the <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> class, imported from the
 
120
<a class="reference internal" href="#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal"><span class="pre">email.generator</span></code></a> module:</p>
 
121
<dl class="class">
 
122
<dt id="email.generator.Generator">
 
123
<em class="property">class </em><code class="descclassname">email.generator.</code><code class="descname">Generator</code><span class="sig-paren">(</span><em>outfp</em>, <em>mangle_from_=True</em>, <em>maxheaderlen=78</em>, <em>*</em>, <em>policy=None</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.Generator" title="Permalink to this definition">¶</a></dt>
 
124
<dd><p>The constructor for the <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> class takes a <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a>
 
125
called <em>outfp</em> for an argument.  <em>outfp</em> must support the <a class="reference internal" href="#email.generator.Generator.write" title="email.generator.Generator.write"><code class="xref py py-meth docutils literal"><span class="pre">write()</span></code></a> method
 
126
and be usable as the output file for the <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal"><span class="pre">print()</span></code></a> function.</p>
 
127
<p>Optional <em>mangle_from_</em> is a flag that, when <code class="docutils literal"><span class="pre">True</span></code>, puts a <code class="docutils literal"><span class="pre">&gt;</span></code> character in
 
128
front of any line in the body that starts exactly as <code class="docutils literal"><span class="pre">From</span></code>, i.e. <code class="docutils literal"><span class="pre">From</span></code>
 
129
followed by a space at the beginning of the line.  This is the only guaranteed
 
130
portable way to avoid having such lines be mistaken for a Unix mailbox format
 
131
envelope header separator (see <a class="reference external" href="https://www.jwz.org/doc/content-length.html">WHY THE CONTENT-LENGTH FORMAT IS BAD</a> for details).  <em>mangle_from_</em>
 
132
defaults to <code class="docutils literal"><span class="pre">True</span></code>, but you might want to set this to <code class="docutils literal"><span class="pre">False</span></code> if you are not
 
133
writing Unix mailbox format files.</p>
 
134
<p>Optional <em>maxheaderlen</em> specifies the longest length for a non-continued header.
 
135
When a header line is longer than <em>maxheaderlen</em> (in characters, with tabs
 
136
expanded to 8 spaces), the header will be split as defined in the
 
137
<a class="reference internal" href="email.header.html#email.header.Header" title="email.header.Header"><code class="xref py py-class docutils literal"><span class="pre">Header</span></code></a> class.  Set to zero to disable header wrapping.
 
138
The default is 78, as recommended (but not required) by <span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a>.</p>
 
139
<p>The <em>policy</em> keyword specifies a <a class="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal"><span class="pre">policy</span></code></a> object that controls a
 
140
number of aspects of the generator&#8217;s operation.  If no <em>policy</em> is specified,
 
141
then the <em>policy</em> attached to the message object passed to <a class="reference internal" href="#email.generator.Generator.flatten" title="email.generator.Generator.flatten"><code class="xref py py-attr docutils literal"><span class="pre">flatten</span></code></a>
 
142
is used.</p>
 
143
<div class="versionchanged">
 
144
<p><span class="versionmodified">Changed in version 3.3: </span>Added the <em>policy</em> keyword.</p>
 
145
</div>
 
146
<p>The other public <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> methods are:</p>
 
147
<dl class="method">
 
148
<dt id="email.generator.Generator.flatten">
 
149
<code class="descname">flatten</code><span class="sig-paren">(</span><em>msg</em>, <em>unixfrom=False</em>, <em>linesep=None</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.Generator.flatten" title="Permalink to this definition">¶</a></dt>
 
150
<dd><p>Print the textual representation of the message object structure rooted at
 
151
<em>msg</em> to the output file specified when the <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> instance
 
152
was created.  Subparts are visited depth-first and the resulting text will
 
153
be properly MIME encoded.</p>
 
154
<p>Optional <em>unixfrom</em> is a flag that forces the printing of the envelope
 
155
header delimiter before the first <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a> header of the root message
 
156
object.  If the root object has no envelope header, a standard one is
 
157
crafted.  By default, this is set to <code class="docutils literal"><span class="pre">False</span></code> to inhibit the printing of
 
158
the envelope delimiter.</p>
 
159
<p>Note that for subparts, no envelope header is ever printed.</p>
 
160
<p>Optional <em>linesep</em> specifies the line separator character used to
 
161
terminate lines in the output.  If specified it overrides the value
 
162
specified by the <em>msg</em>&#8216;s or <code class="docutils literal"><span class="pre">Generator</span></code>&#8216;s <code class="docutils literal"><span class="pre">policy</span></code>.</p>
 
163
<p>Because strings cannot represent non-ASCII bytes, if the policy that
 
164
applies when <code class="docutils literal"><span class="pre">flatten</span></code> is run has <a class="reference internal" href="email.policy.html#email.policy.Policy.cte_type" title="email.policy.Policy.cte_type"><code class="xref py py-attr docutils literal"><span class="pre">cte_type</span></code></a>
 
165
set to <code class="docutils literal"><span class="pre">8bit</span></code>, <code class="docutils literal"><span class="pre">Generator</span></code> will operate as if it were set to
 
166
<code class="docutils literal"><span class="pre">7bit</span></code>.  This means that messages parsed with a Bytes parser that have
 
167
a <em class="mailheader">Content-Transfer-Encoding</em> of <code class="docutils literal"><span class="pre">8bit</span></code> will be converted
 
168
to a use a <code class="docutils literal"><span class="pre">7bit</span></code> Content-Transfer-Encoding.  Non-ASCII bytes in the
 
169
headers will be <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2047.html"><strong>RFC 2047</strong></a> encoded with a charset of <code class="docutils literal"><span class="pre">unknown-8bit</span></code>.</p>
 
170
<div class="versionchanged">
 
171
<p><span class="versionmodified">Changed in version 3.2: </span>Added support for re-encoding <code class="docutils literal"><span class="pre">8bit</span></code> message bodies, and the
 
172
<em>linesep</em> argument.</p>
 
173
</div>
 
174
</dd></dl>
 
175
 
 
176
<dl class="method">
 
177
<dt id="email.generator.Generator.clone">
 
178
<code class="descname">clone</code><span class="sig-paren">(</span><em>fp</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.Generator.clone" title="Permalink to this definition">¶</a></dt>
 
179
<dd><p>Return an independent clone of this <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> instance with the
 
180
exact same options.</p>
 
181
</dd></dl>
 
182
 
 
183
<dl class="method">
 
184
<dt id="email.generator.Generator.write">
 
185
<code class="descname">write</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.Generator.write" title="Permalink to this definition">¶</a></dt>
 
186
<dd><p>Write the string <em>s</em> to the underlying file object, i.e. <em>outfp</em> passed to
 
187
<a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a>&#8216;s constructor.  This provides just enough file-like API
 
188
for <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> instances to be used in the <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal"><span class="pre">print()</span></code></a> function.</p>
 
189
</dd></dl>
 
190
 
 
191
</dd></dl>
 
192
 
 
193
<p>As a convenience, see the <a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal"><span class="pre">Message</span></code></a> methods
 
194
<a class="reference internal" href="email.message.html#email.message.Message.as_string" title="email.message.Message.as_string"><code class="xref py py-meth docutils literal"><span class="pre">as_string()</span></code></a> and <code class="docutils literal"><span class="pre">str(aMessage)</span></code>, a.k.a.
 
195
<a class="reference internal" href="email.message.html#email.message.Message.__str__" title="email.message.Message.__str__"><code class="xref py py-meth docutils literal"><span class="pre">__str__()</span></code></a>, which simplify the generation of a
 
196
formatted string representation of a message object.  For more detail, see
 
197
<a class="reference internal" href="email.message.html#module-email.message" title="email.message: The base class representing email messages."><code class="xref py py-mod docutils literal"><span class="pre">email.message</span></code></a>.</p>
 
198
<dl class="class">
 
199
<dt id="email.generator.BytesGenerator">
 
200
<em class="property">class </em><code class="descclassname">email.generator.</code><code class="descname">BytesGenerator</code><span class="sig-paren">(</span><em>outfp</em>, <em>mangle_from_=True</em>, <em>maxheaderlen=78</em>, <em>*</em>, <em>policy=None</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.BytesGenerator" title="Permalink to this definition">¶</a></dt>
 
201
<dd><p>The constructor for the <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a> class takes a binary
 
202
<a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a> called <em>outfp</em> for an argument.  <em>outfp</em> must
 
203
support a <a class="reference internal" href="#email.generator.BytesGenerator.write" title="email.generator.BytesGenerator.write"><code class="xref py py-meth docutils literal"><span class="pre">write()</span></code></a> method that accepts binary data.</p>
 
204
<p>Optional <em>mangle_from_</em> is a flag that, when <code class="docutils literal"><span class="pre">True</span></code>, puts a <code class="docutils literal"><span class="pre">&gt;</span></code>
 
205
character in front of any line in the body that starts exactly as <code class="docutils literal"><span class="pre">From</span></code>,
 
206
i.e. <code class="docutils literal"><span class="pre">From</span></code> followed by a space at the beginning of the line.  This is the
 
207
only guaranteed portable way to avoid having such lines be mistaken for a
 
208
Unix mailbox format envelope header separator (see <a class="reference external" href="https://www.jwz.org/doc/content-length.html">WHY THE CONTENT-LENGTH
 
209
FORMAT IS BAD</a> for details).
 
210
<em>mangle_from_</em> defaults to <code class="docutils literal"><span class="pre">True</span></code>, but you might want to set this to
 
211
<code class="docutils literal"><span class="pre">False</span></code> if you are not writing Unix mailbox format files.</p>
 
212
<p>Optional <em>maxheaderlen</em> specifies the longest length for a non-continued
 
213
header.  When a header line is longer than <em>maxheaderlen</em> (in characters,
 
214
with tabs expanded to 8 spaces), the header will be split as defined in the
 
215
<a class="reference internal" href="email.header.html#email.header.Header" title="email.header.Header"><code class="xref py py-class docutils literal"><span class="pre">Header</span></code></a> class.  Set to zero to disable header
 
216
wrapping.  The default is 78, as recommended (but not required) by
 
217
<span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a>.</p>
 
218
<p>The <em>policy</em> keyword specifies a <a class="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal"><span class="pre">policy</span></code></a> object that controls a
 
219
number of aspects of the generator&#8217;s operation.  If no <em>policy</em> is specified,
 
220
then the <em>policy</em> attached to the message object passed to <a class="reference internal" href="#email.generator.BytesGenerator.flatten" title="email.generator.BytesGenerator.flatten"><code class="xref py py-attr docutils literal"><span class="pre">flatten</span></code></a>
 
221
is used.</p>
 
222
<div class="versionchanged">
 
223
<p><span class="versionmodified">Changed in version 3.3: </span>Added the <em>policy</em> keyword.</p>
 
224
</div>
 
225
<p>The other public <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a> methods are:</p>
 
226
<dl class="method">
 
227
<dt id="email.generator.BytesGenerator.flatten">
 
228
<code class="descname">flatten</code><span class="sig-paren">(</span><em>msg</em>, <em>unixfrom=False</em>, <em>linesep=None</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.BytesGenerator.flatten" title="Permalink to this definition">¶</a></dt>
 
229
<dd><p>Print the textual representation of the message object structure rooted
 
230
at <em>msg</em> to the output file specified when the <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a>
 
231
instance was created.  Subparts are visited depth-first and the resulting
 
232
text will be properly MIME encoded.  If the <a class="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal"><span class="pre">policy</span></code></a> option
 
233
<a class="reference internal" href="email.policy.html#email.policy.Policy.cte_type" title="email.policy.Policy.cte_type"><code class="xref py py-attr docutils literal"><span class="pre">cte_type</span></code></a> is <code class="docutils literal"><span class="pre">8bit</span></code> (the default),
 
234
then any bytes with the high bit set in the original parsed message that
 
235
have not been modified will be copied faithfully to the output.  If
 
236
<code class="docutils literal"><span class="pre">cte_type</span></code> is <code class="docutils literal"><span class="pre">7bit</span></code>, the bytes will be converted as needed
 
237
using an ASCII-compatible Content-Transfer-Encoding.  In particular,
 
238
RFC-invalid non-ASCII bytes in headers will be encoded using the MIME
 
239
<code class="docutils literal"><span class="pre">unknown-8bit</span></code> character set, thus rendering them RFC-compliant.</p>
 
240
<p>Messages parsed with a Bytes parser that have a
 
241
<em class="mailheader">Content-Transfer-Encoding</em> of 8bit will be reconstructed
 
242
as 8bit if they have not been modified.</p>
 
243
<p>Optional <em>unixfrom</em> is a flag that forces the printing of the envelope
 
244
header delimiter before the first <span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a> header of the root message
 
245
object.  If the root object has no envelope header, a standard one is
 
246
crafted.  By default, this is set to <code class="docutils literal"><span class="pre">False</span></code> to inhibit the printing of
 
247
the envelope delimiter.</p>
 
248
<p>Note that for subparts, no envelope header is ever printed.</p>
 
249
<p>Optional <em>linesep</em> specifies the line separator character used to
 
250
terminate lines in the output.  If specified it overrides the value
 
251
specified by the <code class="docutils literal"><span class="pre">Generator</span></code>or <em>msg</em>&#8216;s <code class="docutils literal"><span class="pre">policy</span></code>.</p>
 
252
</dd></dl>
 
253
 
 
254
<dl class="method">
 
255
<dt id="email.generator.BytesGenerator.clone">
 
256
<code class="descname">clone</code><span class="sig-paren">(</span><em>fp</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.BytesGenerator.clone" title="Permalink to this definition">¶</a></dt>
 
257
<dd><p>Return an independent clone of this <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a> instance with
 
258
the exact same options.</p>
 
259
</dd></dl>
 
260
 
 
261
<dl class="method">
 
262
<dt id="email.generator.BytesGenerator.write">
 
263
<code class="descname">write</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.BytesGenerator.write" title="Permalink to this definition">¶</a></dt>
 
264
<dd><p>Write the string <em>s</em> to the underlying file object.  <em>s</em> is encoded using
 
265
the <code class="docutils literal"><span class="pre">ASCII</span></code> codec and written to the <em>write</em> method of the  <em>outfp</em>
 
266
<em>outfp</em> passed to the <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a>&#8216;s constructor.  This
 
267
provides just enough file-like API for <a class="reference internal" href="#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></code></a> instances
 
268
to be used in the <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal"><span class="pre">print()</span></code></a> function.</p>
 
269
</dd></dl>
 
270
 
 
271
<div class="versionadded">
 
272
<p><span class="versionmodified">New in version 3.2.</span></p>
 
273
</div>
 
274
</dd></dl>
 
275
 
 
276
<p>The <a class="reference internal" href="#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal"><span class="pre">email.generator</span></code></a> module also provides a derived class, called
 
277
<a class="reference internal" href="#email.generator.DecodedGenerator" title="email.generator.DecodedGenerator"><code class="xref py py-class docutils literal"><span class="pre">DecodedGenerator</span></code></a> which is like the <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> base class,
 
278
except that non-<em class="mimetype">text</em> parts are substituted with a format string
 
279
representing the part.</p>
 
280
<dl class="class">
 
281
<dt id="email.generator.DecodedGenerator">
 
282
<em class="property">class </em><code class="descclassname">email.generator.</code><code class="descname">DecodedGenerator</code><span class="sig-paren">(</span><em>outfp</em>, <em>mangle_from_=True</em>, <em>maxheaderlen=78</em>, <em>fmt=None</em><span class="sig-paren">)</span><a class="headerlink" href="#email.generator.DecodedGenerator" title="Permalink to this definition">¶</a></dt>
 
283
<dd><p>This class, derived from <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> walks through all the subparts of a
 
284
message.  If the subpart is of main type <em class="mimetype">text</em>, then it prints the
 
285
decoded payload of the subpart. Optional <em>_mangle_from_</em> and <em>maxheaderlen</em> are
 
286
as with the <a class="reference internal" href="#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a> base class.</p>
 
287
<p>If the subpart is not of main type <em class="mimetype">text</em>, optional <em>fmt</em> is a format
 
288
string that is used instead of the message payload. <em>fmt</em> is expanded with the
 
289
following keywords, <code class="docutils literal"><span class="pre">%(keyword)s</span></code> format:</p>
 
290
<ul class="simple">
 
291
<li><code class="docutils literal"><span class="pre">type</span></code> &#8211; Full MIME type of the non-<em class="mimetype">text</em> part</li>
 
292
<li><code class="docutils literal"><span class="pre">maintype</span></code> &#8211; Main MIME type of the non-<em class="mimetype">text</em> part</li>
 
293
<li><code class="docutils literal"><span class="pre">subtype</span></code> &#8211; Sub-MIME type of the non-<em class="mimetype">text</em> part</li>
 
294
<li><code class="docutils literal"><span class="pre">filename</span></code> &#8211; Filename of the non-<em class="mimetype">text</em> part</li>
 
295
<li><code class="docutils literal"><span class="pre">description</span></code> &#8211; Description associated with the non-<em class="mimetype">text</em> part</li>
 
296
<li><code class="docutils literal"><span class="pre">encoding</span></code> &#8211; Content transfer encoding of the non-<em class="mimetype">text</em> part</li>
 
297
</ul>
 
298
<p>The default value for <em>fmt</em> is <code class="docutils literal"><span class="pre">None</span></code>, meaning</p>
 
299
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">Non</span><span class="o">-</span><span class="n">text</span> <span class="p">(</span><span class="o">%</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span><span class="n">s</span><span class="p">)</span> <span class="n">part</span> <span class="n">of</span> <span class="n">message</span> <span class="n">omitted</span><span class="p">,</span> <span class="n">filename</span> <span class="o">%</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="n">s</span><span class="p">]</span>
 
300
</pre></div>
 
301
</div>
 
302
</dd></dl>
 
303
 
 
304
<p class="rubric">Footnotes</p>
 
305
<table class="docutils footnote" frame="void" id="id3" rules="none">
 
306
<colgroup><col class="label" /><col /></colgroup>
 
307
<tbody valign="top">
 
308
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>This statement assumes that you use the appropriate setting for the
 
309
<code class="docutils literal"><span class="pre">unixfrom</span></code> argument, and that you set maxheaderlen=0 (which will
 
310
preserve whatever the input line lengths were).  It is also not strictly
 
311
true, since in many cases runs of whitespace in headers are collapsed
 
312
into single blanks.  The latter is a bug that will eventually be fixed.</td></tr>
 
313
</tbody>
 
314
</table>
 
315
</div>
 
316
 
 
317
 
 
318
          </div>
 
319
        </div>
 
320
      </div>
 
321
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
322
        <div class="sphinxsidebarwrapper">
 
323
  <h4>Previous topic</h4>
 
324
  <p class="topless"><a href="email.parser.html"
 
325
                        title="previous chapter">19.1.2. <code class="docutils literal"><span class="pre">email.parser</span></code>: Parsing email messages</a></p>
 
326
  <h4>Next topic</h4>
 
327
  <p class="topless"><a href="email.policy.html"
 
328
                        title="next chapter">19.1.4. <code class="docutils literal"><span class="pre">email.policy</span></code>: Policy Objects</a></p>
 
329
  <div role="note" aria-label="source link">
 
330
    <h3>This Page</h3>
 
331
    <ul class="this-page-menu">
 
332
      <li><a href="../bugs.html">Report a Bug</a></li>
 
333
      <li><a href="../_sources/library/email.generator.txt"
 
334
            rel="nofollow">Show Source</a></li>
 
335
    </ul>
 
336
  </div>
 
337
        </div>
 
338
      </div>
 
339
      <div class="clearer"></div>
 
340
    </div>  
 
341
    <div class="related" role="navigation" aria-label="related navigation">
 
342
      <h3>Navigation</h3>
 
343
      <ul>
 
344
        <li class="right" style="margin-right: 10px">
 
345
          <a href="../genindex.html" title="General Index"
 
346
             >index</a></li>
 
347
        <li class="right" >
 
348
          <a href="../py-modindex.html" title="Python Module Index"
 
349
             >modules</a> |</li>
 
350
        <li class="right" >
 
351
          <a href="email.policy.html" title="19.1.4. email.policy: Policy Objects"
 
352
             >next</a> |</li>
 
353
        <li class="right" >
 
354
          <a href="email.parser.html" title="19.1.2. email.parser: Parsing email messages"
 
355
             >previous</a> |</li>
 
356
        <li><img src="../_static/py.png" alt=""
 
357
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
358
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
359
        <li>
 
360
          <span class="version_switcher_placeholder">3.5.2</span>
 
361
          <a href="../index.html">Documentation </a> &raquo;
 
362
        </li>
 
363
 
 
364
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
365
          <li class="nav-item nav-item-2"><a href="netdata.html" >19. Internet Data Handling</a> &raquo;</li>
 
366
          <li class="nav-item nav-item-3"><a href="email.html" >19.1. <code class="docutils literal"><span class="pre">email</span></code> &#8212; An email and MIME handling package</a> &raquo;</li>
 
367
    <li class="right">
 
368
        
 
369
 
 
370
    <div class="inline-search" style="display: none" role="search">
 
371
        <form class="inline-search" action="../search.html" method="get">
 
372
          <input placeholder="Quick search" type="text" name="q" />
 
373
          <input type="submit" value="Go" />
 
374
          <input type="hidden" name="check_keywords" value="yes" />
 
375
          <input type="hidden" name="area" value="default" />
 
376
        </form>
 
377
    </div>
 
378
    <script type="text/javascript">$('.inline-search').show(0);</script>
 
379
         |
 
380
    </li>
 
381
 
 
382
      </ul>
 
383
    </div>  
 
384
    <div class="footer">
 
385
    &copy; <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
 
386
    <br />
 
387
    The Python Software Foundation is a non-profit corporation.
 
388
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
389
    <br />
 
390
    Last updated on Sep 23, 2016.
 
391
    <a href="../bugs.html">Found a bug</a>?
 
392
    <br />
 
393
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
394
    </div>
 
395
 
 
396
  </body>
 
397
</html>
 
 
b'\\ No newline at end of file'