Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
email.contentmanager.html560 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="email.contentmanager: Managing MIME Content" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/email.contentmanager.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/email/contentmanager.py Content Manager Instances: Currently the email package provides only one concrete content manager, raw_data_manager, although more may be added in the futur..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_email.contentmanager_7c5d4114.png" />15<meta property="og:image:alt" content="Source code: Lib/email/contentmanager.py Content Manager Instances: Currently the email package provides only one concrete content manager, raw_data_manager, although more may be added in the futur..." />16<meta name="description" content="Source code: Lib/email/contentmanager.py Content Manager Instances: Currently the email package provides only one concrete content manager, raw_data_manager, although more may be added in the futur..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>email.contentmanager: Managing MIME Content &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="email: Examples" href="email.examples.html" />43    <link rel="prev" title="email.headerregistry: Custom Header Objects" href="email.headerregistry.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/email.contentmanager.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a><ul>108<li><a class="reference internal" href="#content-manager-instances">Content Manager Instances</a></li>109</ul>110</li>111</ul>112 113  </div>114  <div>115    <h4>Previous topic</h4>116    <p class="topless"><a href="email.headerregistry.html"117                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.headerregistry</span></code>: Custom Header Objects</a></p>118  </div>119  <div>120    <h4>Next topic</h4>121    <p class="topless"><a href="email.examples.html"122                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code>: Examples</a></p>123  </div>124  <script>125    document.addEventListener('DOMContentLoaded', () => {126        const title = document.querySelector('meta[property="og:title"]').content;127        const elements = document.querySelectorAll('.improvepage');128        const pageurl = window.location.href.split('?')[0];129        elements.forEach(element => {130            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));131            url.searchParams.set('pagetitle', title);132            url.searchParams.set('pageurl', pageurl);133            url.searchParams.set('pagesource', "library/email.contentmanager.rst");134            element.href = url.toString();135        });136    });137  </script>138  <div role="note" aria-label="source link">139    <h3>This page</h3>140    <ul class="this-page-menu">141      <li><a href="../bugs.html">Report a bug</a></li>142      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>143      <li>144        <a href="https://github.com/python/cpython/blob/main/Doc/library/email.contentmanager.rst?plain=1"145            rel="nofollow">Show source146        </a>147      </li>148      149    </ul>150  </div>151        </nav>152    </div>153</div>154 155  156    <div class="related" role="navigation" aria-label="Related">157      <h3>Navigation</h3>158      <ul>159        <li class="right" style="margin-right: 10px">160          <a href="../genindex.html" title="General Index"161             accesskey="I">index</a></li>162        <li class="right" >163          <a href="../py-modindex.html" title="Python Module Index"164             >modules</a> |</li>165        <li class="right" >166          <a href="email.examples.html" title="email: Examples"167             accesskey="N">next</a> |</li>168        <li class="right" >169          <a href="email.headerregistry.html" title="email.headerregistry: Custom Header Objects"170             accesskey="P">previous</a> |</li>171 172          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>173          <li><a href="https://www.python.org/">Python</a> &#187;</li>174          <li class="switchers">175            <div class="language_switcher_placeholder"></div>176            <div class="version_switcher_placeholder"></div>177          </li>178          <li>179              180          </li>181    <li id="cpython-language-and-version">182      <a href="../index.html">3.15.0a6 Documentation</a> &#187;183    </li>184 185          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>186          <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>187          <li class="nav-item nav-item-3"><a href="email.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>188        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a></li>189                <li class="right">190                    191 192    <div class="inline-search" role="search">193        <form class="inline-search" action="../search.html" method="get">194          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">195          <input type="submit" value="Go">196        </form>197    </div>198                     |199                </li>200            <li class="right">201<label class="theme-selector-label">202    Theme203    <select class="theme-selector" oninput="activateTheme(this.value)">204        <option value="auto" selected>Auto</option>205        <option value="light">Light</option>206        <option value="dark">Dark</option>207    </select>208</label> |</li>209            210      </ul>211    </div>    212 213    <div class="document">214      <div class="documentwrapper">215        <div class="bodywrapper">216          <div class="body" role="main">217            218  <section id="module-email.contentmanager">219<span id="email-contentmanager-managing-mime-content"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content<a class="headerlink" href="#module-email.contentmanager" title="Link to this heading">¶</a></h1>220<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/email/contentmanager.py">Lib/email/contentmanager.py</a></p>221<hr class="docutils" />222<div class="versionadded">223<p><span class="versionmodified added">Added in version 3.6: </span><a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>224</div>225<dl class="py class">226<dt class="sig sig-object py" id="email.contentmanager.ContentManager">227<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">ContentManager</span></span><a class="headerlink" href="#email.contentmanager.ContentManager" title="Link to this definition">¶</a></dt>228<dd><p>Base class for content managers.  Provides the standard registry mechanisms229to register converters between MIME content and other representations, as230well as the <code class="docutils literal notranslate"><span class="pre">get_content</span></code> and <code class="docutils literal notranslate"><span class="pre">set_content</span></code> dispatch methods.</p>231<dl class="py method">232<dt class="sig sig-object py" id="email.contentmanager.ContentManager.get_content">233<span class="sig-name descname"><span class="pre">get_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.get_content" title="Link to this definition">¶</a></dt>234<dd><p>Look up a handler function based on the <code class="docutils literal notranslate"><span class="pre">mimetype</span></code> of <em>msg</em> (see next235paragraph), call it, passing through all arguments, and return the result236of the call.  The expectation is that the handler will extract the237payload from <em>msg</em> and return an object that encodes information about238the extracted data.</p>239<p>To find the handler, look for the following keys in the registry,240stopping with the first one found:</p>241<ul class="simple">242<li><p>the string representing the full MIME type (<code class="docutils literal notranslate"><span class="pre">maintype/subtype</span></code>)</p></li>243<li><p>the string representing the <code class="docutils literal notranslate"><span class="pre">maintype</span></code></p></li>244<li><p>the empty string</p></li>245</ul>246<p>If none of these keys produce a handler, raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> for the247full MIME type.</p>248</dd></dl>249 250<dl class="py method">251<dt class="sig sig-object py" id="email.contentmanager.ContentManager.set_content">252<span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.set_content" title="Link to this definition">¶</a></dt>253<dd><p>If the <code class="docutils literal notranslate"><span class="pre">maintype</span></code> is <code class="docutils literal notranslate"><span class="pre">multipart</span></code>, raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>; otherwise254look up a handler function based on the type of <em>obj</em> (see next255paragraph), call <a class="reference internal" href="email.message.html#email.message.EmailMessage.clear_content" title="email.message.EmailMessage.clear_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clear_content()</span></code></a> on the256<em>msg</em>, and call the handler function, passing through all arguments.  The257expectation is that the handler will transform and store <em>obj</em> into258<em>msg</em>, possibly making other changes to <em>msg</em> as well, such as adding259various MIME headers to encode information needed to interpret the stored260data.</p>261<p>To find the handler, obtain the type of <em>obj</em> (<code class="docutils literal notranslate"><span class="pre">typ</span> <span class="pre">=</span> <span class="pre">type(obj)</span></code>), and262look for the following keys in the registry, stopping with the first one263found:</p>264<ul class="simple">265<li><p>the type itself (<code class="docutils literal notranslate"><span class="pre">typ</span></code>)</p></li>266<li><p>the type’s fully qualified name (<code class="docutils literal notranslate"><span class="pre">typ.__module__</span> <span class="pre">+</span> <span class="pre">'.'</span> <span class="pre">+</span>267<span class="pre">typ.__qualname__</span></code>).</p></li>268<li><p>the type’s <a class="reference internal" href="../reference/datamodel.html#type.__qualname__" title="type.__qualname__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">qualname</span></code></a> (<code class="docutils literal notranslate"><span class="pre">typ.__qualname__</span></code>)</p></li>269<li><p>the type’s <a class="reference internal" href="../reference/datamodel.html#type.__name__" title="type.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> (<code class="docutils literal notranslate"><span class="pre">typ.__name__</span></code>).</p></li>270</ul>271<p>If none of the above match, repeat all of the checks above for each of272the types in the <a class="reference internal" href="../glossary.html#term-MRO"><span class="xref std std-term">MRO</span></a> (<a class="reference internal" href="../reference/datamodel.html#type.__mro__" title="type.__mro__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">typ.__mro__</span></code></a>).273Finally, if no other key274yields a handler, check for a handler for the key <code class="docutils literal notranslate"><span class="pre">None</span></code>.  If there is275no handler for <code class="docutils literal notranslate"><span class="pre">None</span></code>, raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> for the fully276qualified name of the type.</p>277<p>Also add a <em class="mailheader">MIME-Version</em> header if one is not present (see278also <a class="reference internal" href="email.message.html#email.message.MIMEPart" title="email.message.MIMEPart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEPart</span></code></a>).</p>279</dd></dl>280 281<dl class="py method">282<dt class="sig sig-object py" id="email.contentmanager.ContentManager.add_get_handler">283<span class="sig-name descname"><span class="pre">add_get_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.add_get_handler" title="Link to this definition">¶</a></dt>284<dd><p>Record the function <em>handler</em> as the handler for <em>key</em>.  For the possible285values of <em>key</em>, see <a class="reference internal" href="#email.contentmanager.get_content" title="email.contentmanager.get_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_content()</span></code></a>.</p>286</dd></dl>287 288<dl class="py method">289<dt class="sig sig-object py" id="email.contentmanager.ContentManager.add_set_handler">290<span class="sig-name descname"><span class="pre">add_set_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typekey</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.add_set_handler" title="Link to this definition">¶</a></dt>291<dd><p>Record <em>handler</em> as the function to call when an object of a type292matching <em>typekey</em> is passed to <a class="reference internal" href="#email.contentmanager.set_content" title="email.contentmanager.set_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code></a>.  For the possible293values of <em>typekey</em>, see <code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code>.</p>294</dd></dl>295 296</dd></dl>297 298<section id="content-manager-instances">299<h2>Content Manager Instances<a class="headerlink" href="#content-manager-instances" title="Link to this heading">¶</a></h2>300<p>Currently the email package provides only one concrete content manager,301<a class="reference internal" href="#email.contentmanager.raw_data_manager" title="email.contentmanager.raw_data_manager"><code class="xref py py-data docutils literal notranslate"><span class="pre">raw_data_manager</span></code></a>, although more may be added in the future.302<code class="xref py py-data docutils literal notranslate"><span class="pre">raw_data_manager</span></code> is the303<a class="reference internal" href="email.policy.html#email.policy.EmailPolicy.content_manager" title="email.policy.EmailPolicy.content_manager"><code class="xref py py-attr docutils literal notranslate"><span class="pre">content_manager</span></code></a> provided by304<a class="reference internal" href="email.policy.html#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a> and its derivatives.</p>305<dl class="py data">306<dt class="sig sig-object py" id="email.contentmanager.raw_data_manager">307<span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">raw_data_manager</span></span><a class="headerlink" href="#email.contentmanager.raw_data_manager" title="Link to this definition">¶</a></dt>308<dd><p>This content manager provides only a minimum interface beyond that provided309by <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> itself:  it deals only with text, raw310byte strings, and <code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> objects.  Nevertheless, it311provides significant advantages compared to the base API: <code class="docutils literal notranslate"><span class="pre">get_content</span></code> on312a text part will return a unicode string without the application needing to313manually decode it, <code class="docutils literal notranslate"><span class="pre">set_content</span></code> provides a rich set of options for314controlling the headers added to a part and controlling the content transfer315encoding, and it enables the use of the various <code class="docutils literal notranslate"><span class="pre">add_</span></code> methods, thereby316simplifying the creation of multipart messages.</p>317<dl class="py method">318<dt class="sig sig-object py" id="email.contentmanager.get_content">319<span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">get_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'replace'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.get_content" title="Link to this definition">¶</a></dt>320<dd><p>Return the payload of the part as either a string (for <code class="docutils literal notranslate"><span class="pre">text</span></code> parts), an321<a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> object (for <code class="docutils literal notranslate"><span class="pre">message/rfc822</span></code>322parts), or a <code class="docutils literal notranslate"><span class="pre">bytes</span></code> object (for all other non-multipart types).  Raise323a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> if called on a <code class="docutils literal notranslate"><span class="pre">multipart</span></code>.  If the part is a324<code class="docutils literal notranslate"><span class="pre">text</span></code> part and <em>errors</em> is specified, use it as the error handler when325decoding the payload to unicode.  The default error handler is326<code class="docutils literal notranslate"><span class="pre">replace</span></code>.</p>327</dd></dl>328 329<dl class="py method">330<dt class="sig sig-object py" id="email.contentmanager.set_content">331<span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">&lt;'str'&gt;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subtype=&quot;plain&quot;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset='utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disposition=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cid=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">params=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers=None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.set_content" title="Link to this definition">¶</a></dt>332<dt class="sig sig-object py">333<span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">&lt;'bytes'&gt;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maintype</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subtype</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte=&quot;base64&quot;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disposition=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cid=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">params=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers=None</span></span></em><span class="sig-paren">)</span></dt>334<dt class="sig sig-object py">335<span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">&lt;'EmailMessage'&gt;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disposition=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cid=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">params=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers=None</span></span></em><span class="sig-paren">)</span></dt>336<dd><p>Add headers and payload to <em>msg</em>:</p>337<p>Add a <em class="mailheader">Content-Type</em> header with a <code class="docutils literal notranslate"><span class="pre">maintype/subtype</span></code>338value.</p>339<ul class="simple">340<li><p>For <code class="docutils literal notranslate"><span class="pre">str</span></code>, set the MIME <code class="docutils literal notranslate"><span class="pre">maintype</span></code> to <code class="docutils literal notranslate"><span class="pre">text</span></code>, and set the341subtype to <em>subtype</em> if it is specified, or <code class="docutils literal notranslate"><span class="pre">plain</span></code> if it is not.</p></li>342<li><p>For <code class="docutils literal notranslate"><span class="pre">bytes</span></code>, use the specified <em>maintype</em> and <em>subtype</em>, or343raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if they are not specified.</p></li>344<li><p>For <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> objects, set the maintype345to <code class="docutils literal notranslate"><span class="pre">message</span></code>, and set the subtype to <em>subtype</em> if it is346specified or <code class="docutils literal notranslate"><span class="pre">rfc822</span></code> if it is not.  If <em>subtype</em> is347<code class="docutils literal notranslate"><span class="pre">partial</span></code>, raise an error (<code class="docutils literal notranslate"><span class="pre">bytes</span></code> objects must be used to348construct <code class="docutils literal notranslate"><span class="pre">message/partial</span></code> parts).</p></li>349</ul>350<p>If <em>charset</em> is provided (which is valid only for <code class="docutils literal notranslate"><span class="pre">str</span></code>), encode the351string to bytes using the specified character set.  The default is352<code class="docutils literal notranslate"><span class="pre">utf-8</span></code>.  If the specified <em>charset</em> is a known alias for a standard353MIME charset name, use the standard charset instead.</p>354<p>If <em>cte</em> is set, encode the payload using the specified content transfer355encoding, and set the <em class="mailheader">Content-Transfer-Encoding</em> header to356that value.  Possible values for <em>cte</em> are <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code>,357<code class="docutils literal notranslate"><span class="pre">base64</span></code>, <code class="docutils literal notranslate"><span class="pre">7bit</span></code>, <code class="docutils literal notranslate"><span class="pre">8bit</span></code>, and <code class="docutils literal notranslate"><span class="pre">binary</span></code>.  If the input cannot be358encoded in the specified encoding (for example, specifying a <em>cte</em> of359<code class="docutils literal notranslate"><span class="pre">7bit</span></code> for an input that contains non-ASCII values), raise a360<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>361<ul class="simple">362<li><p>For <code class="docutils literal notranslate"><span class="pre">str</span></code> objects, if <em>cte</em> is not set use heuristics to363determine the most compact encoding.  Prior to encoding,364<a class="reference internal" href="stdtypes.html#str.splitlines" title="str.splitlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.splitlines()</span></code></a> is used to normalize all line boundaries,365ensuring that each line of the payload is terminated by the366current policy’s <a class="reference internal" href="email.policy.html#email.policy.Policy.linesep" title="email.policy.Policy.linesep"><code class="xref py py-data docutils literal notranslate"><span class="pre">linesep</span></code></a> property367(even if the original string did not end with one).</p></li>368<li><p>For <code class="docutils literal notranslate"><span class="pre">bytes</span></code> objects, <em>cte</em> is taken to be base64 if not set,369and the aforementioned newline translation is not performed.</p></li>370<li><p>For <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a>, per <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2046.html"><strong>RFC 2046</strong></a>, raise371an error if a <em>cte</em> of <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code> or <code class="docutils literal notranslate"><span class="pre">base64</span></code> is372requested for <em>subtype</em> <code class="docutils literal notranslate"><span class="pre">rfc822</span></code>, and for any <em>cte</em> other than373<code class="docutils literal notranslate"><span class="pre">7bit</span></code> for <em>subtype</em> <code class="docutils literal notranslate"><span class="pre">external-body</span></code>.  For374<code class="docutils literal notranslate"><span class="pre">message/rfc822</span></code>, use <code class="docutils literal notranslate"><span class="pre">8bit</span></code> if <em>cte</em> is not specified.  For375all other values of <em>subtype</em>, use <code class="docutils literal notranslate"><span class="pre">7bit</span></code>.</p></li>376</ul>377<div class="admonition note">378<p class="admonition-title">Note</p>379<p>A <em>cte</em> of <code class="docutils literal notranslate"><span class="pre">binary</span></code> does not actually work correctly yet.380The <code class="docutils literal notranslate"><span class="pre">EmailMessage</span></code> object as modified by <code class="docutils literal notranslate"><span class="pre">set_content</span></code> is381correct, but <a class="reference internal" href="email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesGenerator</span></code></a> does not382serialize it correctly.</p>383</div>384<p>If <em>disposition</em> is set, use it as the value of the385<em class="mailheader">Content-Disposition</em> header.  If not specified, and386<em>filename</em> is specified, add the header with the value <code class="docutils literal notranslate"><span class="pre">attachment</span></code>.387If <em>disposition</em> is not specified and <em>filename</em> is also not specified,388do not add the header.  The only valid values for <em>disposition</em> are389<code class="docutils literal notranslate"><span class="pre">attachment</span></code> and <code class="docutils literal notranslate"><span class="pre">inline</span></code>.</p>390<p>If <em>filename</em> is specified, use it as the value of the <code class="docutils literal notranslate"><span class="pre">filename</span></code>391parameter of the <em class="mailheader">Content-Disposition</em> header.</p>392<p>If <em>cid</em> is specified, add a <em class="mailheader">Content-ID</em> header with393<em>cid</em> as its value.</p>394<p>If <em>params</em> is specified, iterate its <code class="docutils literal notranslate"><span class="pre">items</span></code> method and use the395resulting <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code> pairs to set additional parameters on the396<em class="mailheader">Content-Type</em> header.</p>397<p>If <em>headers</em> is specified and is a list of strings of the form398<code class="docutils literal notranslate"><span class="pre">headername:</span> <span class="pre">headervalue</span></code> or a list of <code class="docutils literal notranslate"><span class="pre">header</span></code> objects399(distinguished from strings by having a <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute), add the400headers to <em>msg</em>.</p>401</dd></dl>402 403</dd></dl>404 405<p class="rubric">Footnotes</p>406<aside class="footnote-list brackets">407<aside class="footnote brackets" id="id2" role="doc-footnote">408<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>409<p>Originally added in 3.4 as a <a class="reference internal" href="../glossary.html#term-provisional-package"><span class="xref std std-term">provisional module</span></a></p>410</aside>411</aside>412</section>413</section>414 415 416            <div class="clearer"></div>417          </div>418        </div>419      </div>420      <div class="sphinxsidebar" role="navigation" aria-label="Main">421        <div class="sphinxsidebarwrapper">422  <div>423    <h3><a href="../contents.html">Table of Contents</a></h3>424    <ul>425<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a><ul>426<li><a class="reference internal" href="#content-manager-instances">Content Manager Instances</a></li>427</ul>428</li>429</ul>430 431  </div>432  <div>433    <h4>Previous topic</h4>434    <p class="topless"><a href="email.headerregistry.html"435                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.headerregistry</span></code>: Custom Header Objects</a></p>436  </div>437  <div>438    <h4>Next topic</h4>439    <p class="topless"><a href="email.examples.html"440                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code>: Examples</a></p>441  </div>442  <script>443    document.addEventListener('DOMContentLoaded', () => {444        const title = document.querySelector('meta[property="og:title"]').content;445        const elements = document.querySelectorAll('.improvepage');446        const pageurl = window.location.href.split('?')[0];447        elements.forEach(element => {448            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));449            url.searchParams.set('pagetitle', title);450            url.searchParams.set('pageurl', pageurl);451            url.searchParams.set('pagesource', "library/email.contentmanager.rst");452            element.href = url.toString();453        });454    });455  </script>456  <div role="note" aria-label="source link">457    <h3>This page</h3>458    <ul class="this-page-menu">459      <li><a href="../bugs.html">Report a bug</a></li>460      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>461      <li>462        <a href="https://github.com/python/cpython/blob/main/Doc/library/email.contentmanager.rst?plain=1"463            rel="nofollow">Show source464        </a>465      </li>466      467    </ul>468  </div>469        </div>470<div id="sidebarbutton" title="Collapse sidebar">471<span>«</span>472</div>473 474      </div>475      <div class="clearer"></div>476    </div>  477    <div class="related" role="navigation" aria-label="Related">478      <h3>Navigation</h3>479      <ul>480        <li class="right" style="margin-right: 10px">481          <a href="../genindex.html" title="General Index"482             >index</a></li>483        <li class="right" >484          <a href="../py-modindex.html" title="Python Module Index"485             >modules</a> |</li>486        <li class="right" >487          <a href="email.examples.html" title="email: Examples"488             >next</a> |</li>489        <li class="right" >490          <a href="email.headerregistry.html" title="email.headerregistry: Custom Header Objects"491             >previous</a> |</li>492 493          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>494          <li><a href="https://www.python.org/">Python</a> &#187;</li>495          <li class="switchers">496            <div class="language_switcher_placeholder"></div>497            <div class="version_switcher_placeholder"></div>498          </li>499          <li>500              501          </li>502    <li id="cpython-language-and-version">503      <a href="../index.html">3.15.0a6 Documentation</a> &#187;504    </li>505 506          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>507          <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>508          <li class="nav-item nav-item-3"><a href="email.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>509        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a></li>510                <li class="right">511                    512 513    <div class="inline-search" role="search">514        <form class="inline-search" action="../search.html" method="get">515          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">516          <input type="submit" value="Go">517        </form>518    </div>519                     |520                </li>521            <li class="right">522<label class="theme-selector-label">523    Theme524    <select class="theme-selector" oninput="activateTheme(this.value)">525        <option value="auto" selected>Auto</option>526        <option value="light">Light</option>527        <option value="dark">Dark</option>528    </select>529</label> |</li>530            531      </ul>532    </div>  533    <div class="footer">534    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.535    <br>536    This page is licensed under the Python Software Foundation License Version 2.537    <br>538    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.539    <br>540    541      See <a href="/license.html">History and License</a> for more information.<br>542    543    544    <br>545 546    The Python Software Foundation is a non-profit corporation.547<a href="https://www.python.org/psf/donations/">Please donate.</a>548<br>549    <br>550      Last updated on Mar 10, 2026 (08:58 UTC).551    552      <a href="/bugs.html">Found a bug</a>?553    554    <br>555 556    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.557    </div>558 559  </body>560</html>