569 lines
57 KiB
HTML
569 lines
57 KiB
HTML
|
|
|||
|
<!DOCTYPE html>
|
|||
|
|
|||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|||
|
<head>
|
|||
|
<meta charset="utf-8" />
|
|||
|
<title>email: Examples — Python 3.7.4 documentation</title>
|
|||
|
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|||
|
|
|||
|
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/language_data.js"></script>
|
|||
|
|
|||
|
<script type="text/javascript" src="../_static/sidebar.js"></script>
|
|||
|
|
|||
|
<link rel="search" type="application/opensearchdescription+xml"
|
|||
|
title="Search within Python 3.7.4 documentation"
|
|||
|
href="../_static/opensearch.xml"/>
|
|||
|
<link rel="author" title="About these documents" href="../about.html" />
|
|||
|
<link rel="index" title="Index" href="../genindex.html" />
|
|||
|
<link rel="search" title="Search" href="../search.html" />
|
|||
|
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
|||
|
<link rel="next" title="email.message.Message: Representing an email message using the compat32 API" href="email.compat32-message.html" />
|
|||
|
<link rel="prev" title="email.contentmanager: Managing MIME Content" href="email.contentmanager.html" />
|
|||
|
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
|||
|
<link rel="canonical" href="https://docs.python.org/3/library/email.examples.html" />
|
|||
|
|
|||
|
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/switchers.js"></script>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<style>
|
|||
|
@media only screen {
|
|||
|
table.full-width-table {
|
|||
|
width: 100%;
|
|||
|
}
|
|||
|
}
|
|||
|
</style>
|
|||
|
|
|||
|
|
|||
|
</head><body>
|
|||
|
|
|||
|
<div class="related" role="navigation" aria-label="related navigation">
|
|||
|
<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="../py-modindex.html" title="Python Module Index"
|
|||
|
>modules</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="email.compat32-message.html" title="email.message.Message: Representing an email message using the compat32 API"
|
|||
|
accesskey="N">next</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="email.contentmanager.html" title="email.contentmanager: Managing MIME Content"
|
|||
|
accesskey="P">previous</a> |</li>
|
|||
|
<li><img src="../_static/py.png" alt=""
|
|||
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|||
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|||
|
<li>
|
|||
|
<span class="language_switcher_placeholder">en</span>
|
|||
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|||
|
<a href="../index.html">Documentation </a> »
|
|||
|
</li>
|
|||
|
|
|||
|
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
|||
|
<li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> »</li>
|
|||
|
<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> »</li>
|
|||
|
<li class="right">
|
|||
|
|
|||
|
|
|||
|
<div class="inline-search" style="display: none" role="search">
|
|||
|
<form class="inline-search" action="../search.html" method="get">
|
|||
|
<input placeholder="Quick search" type="text" name="q" />
|
|||
|
<input type="submit" value="Go" />
|
|||
|
<input type="hidden" name="check_keywords" value="yes" />
|
|||
|
<input type="hidden" name="area" value="default" />
|
|||
|
</form>
|
|||
|
</div>
|
|||
|
<script type="text/javascript">$('.inline-search').show(0);</script>
|
|||
|
|
|
|||
|
</li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="document">
|
|||
|
<div class="documentwrapper">
|
|||
|
<div class="bodywrapper">
|
|||
|
<div class="body" role="main">
|
|||
|
|
|||
|
<div class="section" id="email-examples">
|
|||
|
<span id="id1"></span><h1><a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a>: Examples<a class="headerlink" href="#email-examples" title="Permalink to this headline">¶</a></h1>
|
|||
|
<p>Here are a few examples of how to use the <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package to read, write,
|
|||
|
and send simple email messages, as well as more complex MIME messages.</p>
|
|||
|
<p>First, let’s see how to create and send a simple text message (both the
|
|||
|
text content and the addresses may contain unicode characters):</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Import smtplib for the actual sending function</span>
|
|||
|
<span class="kn">import</span> <span class="nn">smtplib</span>
|
|||
|
|
|||
|
<span class="c1"># Import the email modules we'll need</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.message</span> <span class="k">import</span> <span class="n">EmailMessage</span>
|
|||
|
|
|||
|
<span class="c1"># Open the plain text file whose name is in textfile for reading.</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">textfile</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
|||
|
<span class="c1"># Create a text/plain message</span>
|
|||
|
<span class="n">msg</span> <span class="o">=</span> <span class="n">EmailMessage</span><span class="p">()</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">set_content</span><span class="p">(</span><span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
|
|||
|
|
|||
|
<span class="c1"># me == the sender's email address</span>
|
|||
|
<span class="c1"># you == the recipient's email address</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'Subject'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'The contents of </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="n">textfile</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'From'</span><span class="p">]</span> <span class="o">=</span> <span class="n">me</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'To'</span><span class="p">]</span> <span class="o">=</span> <span class="n">you</span>
|
|||
|
|
|||
|
<span class="c1"># Send the message via our own SMTP server.</span>
|
|||
|
<span class="n">s</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">)</span>
|
|||
|
<span class="n">s</span><span class="o">.</span><span class="n">send_message</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
|
|||
|
<span class="n">s</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Parsing <span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a> headers can easily be done by the using the classes
|
|||
|
from 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 notranslate"><span class="pre">parser</span></code></a> module:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Import the email modules we'll need</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.parser</span> <span class="k">import</span> <span class="n">BytesParser</span><span class="p">,</span> <span class="n">Parser</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.policy</span> <span class="k">import</span> <span class="n">default</span>
|
|||
|
|
|||
|
<span class="c1"># If the e-mail headers are in a file, uncomment these two lines:</span>
|
|||
|
<span class="c1"># with open(messagefile, 'rb') as fp:</span>
|
|||
|
<span class="c1"># headers = BytesParser(policy=default).parse(fp)</span>
|
|||
|
|
|||
|
<span class="c1"># Or for parsing headers in a string (this is an uncommon operation), use:</span>
|
|||
|
<span class="n">headers</span> <span class="o">=</span> <span class="n">Parser</span><span class="p">(</span><span class="n">policy</span><span class="o">=</span><span class="n">default</span><span class="p">)</span><span class="o">.</span><span class="n">parsestr</span><span class="p">(</span>
|
|||
|
<span class="s1">'From: Foo Bar <user@example.com></span><span class="se">\n</span><span class="s1">'</span>
|
|||
|
<span class="s1">'To: <someone_else@example.com></span><span class="se">\n</span><span class="s1">'</span>
|
|||
|
<span class="s1">'Subject: Test message</span><span class="se">\n</span><span class="s1">'</span>
|
|||
|
<span class="s1">'</span><span class="se">\n</span><span class="s1">'</span>
|
|||
|
<span class="s1">'Body would go here</span><span class="se">\n</span><span class="s1">'</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="c1"># Now the header items can be accessed as a dictionary:</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'To: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">headers</span><span class="p">[</span><span class="s1">'to'</span><span class="p">]))</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'From: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">headers</span><span class="p">[</span><span class="s1">'from'</span><span class="p">]))</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Subject: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">headers</span><span class="p">[</span><span class="s1">'subject'</span><span class="p">]))</span>
|
|||
|
|
|||
|
<span class="c1"># You can also access the parts of the addresses:</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Recipient username: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">headers</span><span class="p">[</span><span class="s1">'to'</span><span class="p">]</span><span class="o">.</span><span class="n">addresses</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">username</span><span class="p">))</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Sender name: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">headers</span><span class="p">[</span><span class="s1">'from'</span><span class="p">]</span><span class="o">.</span><span class="n">addresses</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">display_name</span><span class="p">))</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Here’s an example of how to send a MIME message containing a bunch of family
|
|||
|
pictures that may be residing in a directory:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Import smtplib for the actual sending function</span>
|
|||
|
<span class="kn">import</span> <span class="nn">smtplib</span>
|
|||
|
|
|||
|
<span class="c1"># And imghdr to find the types of our images</span>
|
|||
|
<span class="kn">import</span> <span class="nn">imghdr</span>
|
|||
|
|
|||
|
<span class="c1"># Here are the email package modules we'll need</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.message</span> <span class="k">import</span> <span class="n">EmailMessage</span>
|
|||
|
|
|||
|
<span class="c1"># Create the container email message.</span>
|
|||
|
<span class="n">msg</span> <span class="o">=</span> <span class="n">EmailMessage</span><span class="p">()</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'Subject'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Our family reunion'</span>
|
|||
|
<span class="c1"># me == the sender's email address</span>
|
|||
|
<span class="c1"># family = the list of all recipients' email addresses</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'From'</span><span class="p">]</span> <span class="o">=</span> <span class="n">me</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'To'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">', '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">family</span><span class="p">)</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">preamble</span> <span class="o">=</span> <span class="s1">'Our family reunion'</span>
|
|||
|
|
|||
|
<span class="c1"># Open the files in binary mode. Use imghdr to figure out the</span>
|
|||
|
<span class="c1"># MIME subtype for each specific image.</span>
|
|||
|
<span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">pngfiles</span><span class="p">:</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
|||
|
<span class="n">img_data</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">add_attachment</span><span class="p">(</span><span class="n">img_data</span><span class="p">,</span> <span class="n">maintype</span><span class="o">=</span><span class="s1">'image'</span><span class="p">,</span>
|
|||
|
<span class="n">subtype</span><span class="o">=</span><span class="n">imghdr</span><span class="o">.</span><span class="n">what</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">img_data</span><span class="p">))</span>
|
|||
|
|
|||
|
<span class="c1"># Send the email via our own SMTP server.</span>
|
|||
|
<span class="k">with</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">)</span> <span class="k">as</span> <span class="n">s</span><span class="p">:</span>
|
|||
|
<span class="n">s</span><span class="o">.</span><span class="n">send_message</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Here’s an example of how to send the entire contents of a directory as an email
|
|||
|
message: <a class="footnote-reference brackets" href="#id3" id="id2">1</a></p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3</span>
|
|||
|
|
|||
|
<span class="sd">"""Send the contents of a directory as a MIME message."""</span>
|
|||
|
|
|||
|
<span class="kn">import</span> <span class="nn">os</span>
|
|||
|
<span class="kn">import</span> <span class="nn">smtplib</span>
|
|||
|
<span class="c1"># For guessing MIME type based on file name extension</span>
|
|||
|
<span class="kn">import</span> <span class="nn">mimetypes</span>
|
|||
|
|
|||
|
<span class="kn">from</span> <span class="nn">argparse</span> <span class="k">import</span> <span class="n">ArgumentParser</span>
|
|||
|
|
|||
|
<span class="kn">from</span> <span class="nn">email.message</span> <span class="k">import</span> <span class="n">EmailMessage</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.policy</span> <span class="k">import</span> <span class="n">SMTP</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
|
|||
|
<span class="n">parser</span> <span class="o">=</span> <span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s2">"""</span><span class="se">\</span>
|
|||
|
<span class="s2">Send the contents of a directory as a MIME message.</span>
|
|||
|
<span class="s2">Unless the -o option is given, the email is sent by forwarding to your local</span>
|
|||
|
<span class="s2">SMTP server, which then does the normal delivery process. Your local machine</span>
|
|||
|
<span class="s2">must be running an SMTP server.</span>
|
|||
|
<span class="s2">"""</span><span class="p">)</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-d'</span><span class="p">,</span> <span class="s1">'--directory'</span><span class="p">,</span>
|
|||
|
<span class="n">help</span><span class="o">=</span><span class="s2">"""Mail the contents of the specified directory,</span>
|
|||
|
<span class="s2"> otherwise use the current directory. Only the regular</span>
|
|||
|
<span class="s2"> files in the directory are sent, and we don't recurse to</span>
|
|||
|
<span class="s2"> subdirectories."""</span><span class="p">)</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-o'</span><span class="p">,</span> <span class="s1">'--output'</span><span class="p">,</span>
|
|||
|
<span class="n">metavar</span><span class="o">=</span><span class="s1">'FILE'</span><span class="p">,</span>
|
|||
|
<span class="n">help</span><span class="o">=</span><span class="s2">"""Print the composed message to FILE instead of</span>
|
|||
|
<span class="s2"> sending the message to the SMTP server."""</span><span class="p">)</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-s'</span><span class="p">,</span> <span class="s1">'--sender'</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
|||
|
<span class="n">help</span><span class="o">=</span><span class="s1">'The value of the From: header (required)'</span><span class="p">)</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-r'</span><span class="p">,</span> <span class="s1">'--recipient'</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
|||
|
<span class="n">action</span><span class="o">=</span><span class="s1">'append'</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s1">'RECIPIENT'</span><span class="p">,</span>
|
|||
|
<span class="n">default</span><span class="o">=</span><span class="p">[],</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'recipients'</span><span class="p">,</span>
|
|||
|
<span class="n">help</span><span class="o">=</span><span class="s1">'A To: header value (at least one required)'</span><span class="p">)</span>
|
|||
|
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
|
|||
|
<span class="n">directory</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">directory</span>
|
|||
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">directory</span><span class="p">:</span>
|
|||
|
<span class="n">directory</span> <span class="o">=</span> <span class="s1">'.'</span>
|
|||
|
<span class="c1"># Create the message</span>
|
|||
|
<span class="n">msg</span> <span class="o">=</span> <span class="n">EmailMessage</span><span class="p">()</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'Subject'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Contents of directory </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">directory</span><span class="p">)</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'To'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">', '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">recipients</span><span class="p">)</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'From'</span><span class="p">]</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">sender</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">preamble</span> <span class="o">=</span> <span class="s1">'You will not see this in a MIME-aware mail reader.</span><span class="se">\n</span><span class="s1">'</span>
|
|||
|
|
|||
|
<span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">directory</span><span class="p">):</span>
|
|||
|
<span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
|
|||
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
|
|||
|
<span class="k">continue</span>
|
|||
|
<span class="c1"># Guess the content type based on the file's extension. Encoding</span>
|
|||
|
<span class="c1"># will be ignored, although we should check for simple things like</span>
|
|||
|
<span class="c1"># gzip'd or compressed files.</span>
|
|||
|
<span class="n">ctype</span><span class="p">,</span> <span class="n">encoding</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_type</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
|
|||
|
<span class="k">if</span> <span class="n">ctype</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">encoding</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
|||
|
<span class="c1"># No guess could be made, or the file is encoded (compressed), so</span>
|
|||
|
<span class="c1"># use a generic bag-of-bits type.</span>
|
|||
|
<span class="n">ctype</span> <span class="o">=</span> <span class="s1">'application/octet-stream'</span>
|
|||
|
<span class="n">maintype</span><span class="p">,</span> <span class="n">subtype</span> <span class="o">=</span> <span class="n">ctype</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'/'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">add_attachment</span><span class="p">(</span><span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span>
|
|||
|
<span class="n">maintype</span><span class="o">=</span><span class="n">maintype</span><span class="p">,</span>
|
|||
|
<span class="n">subtype</span><span class="o">=</span><span class="n">subtype</span><span class="p">,</span>
|
|||
|
<span class="n">filename</span><span class="o">=</span><span class="n">filename</span><span class="p">)</span>
|
|||
|
<span class="c1"># Now send or store the message</span>
|
|||
|
<span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">output</span><span class="p">:</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">output</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
|||
|
<span class="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">as_bytes</span><span class="p">(</span><span class="n">policy</span><span class="o">=</span><span class="n">SMTP</span><span class="p">))</span>
|
|||
|
<span class="k">else</span><span class="p">:</span>
|
|||
|
<span class="k">with</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">)</span> <span class="k">as</span> <span class="n">s</span><span class="p">:</span>
|
|||
|
<span class="n">s</span><span class="o">.</span><span class="n">send_message</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
|
|||
|
<span class="n">main</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Here’s an example of how to unpack a MIME message like the one
|
|||
|
above, into a directory of files:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3</span>
|
|||
|
|
|||
|
<span class="sd">"""Unpack a MIME message into a directory of files."""</span>
|
|||
|
|
|||
|
<span class="kn">import</span> <span class="nn">os</span>
|
|||
|
<span class="kn">import</span> <span class="nn">email</span>
|
|||
|
<span class="kn">import</span> <span class="nn">mimetypes</span>
|
|||
|
|
|||
|
<span class="kn">from</span> <span class="nn">email.policy</span> <span class="k">import</span> <span class="n">default</span>
|
|||
|
|
|||
|
<span class="kn">from</span> <span class="nn">argparse</span> <span class="k">import</span> <span class="n">ArgumentParser</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
|
|||
|
<span class="n">parser</span> <span class="o">=</span> <span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s2">"""</span><span class="se">\</span>
|
|||
|
<span class="s2">Unpack a MIME message into a directory of files.</span>
|
|||
|
<span class="s2">"""</span><span class="p">)</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-d'</span><span class="p">,</span> <span class="s1">'--directory'</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
|||
|
<span class="n">help</span><span class="o">=</span><span class="s2">"""Unpack the MIME message into the named</span>
|
|||
|
<span class="s2"> directory, which will be created if it doesn't already</span>
|
|||
|
<span class="s2"> exist."""</span><span class="p">)</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'msgfile'</span><span class="p">)</span>
|
|||
|
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
|
|||
|
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">msgfile</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
|||
|
<span class="n">msg</span> <span class="o">=</span> <span class="n">email</span><span class="o">.</span><span class="n">message_from_binary_file</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">policy</span><span class="o">=</span><span class="n">default</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">try</span><span class="p">:</span>
|
|||
|
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">directory</span><span class="p">)</span>
|
|||
|
<span class="k">except</span> <span class="ne">FileExistsError</span><span class="p">:</span>
|
|||
|
<span class="k">pass</span>
|
|||
|
|
|||
|
<span class="n">counter</span> <span class="o">=</span> <span class="mi">1</span>
|
|||
|
<span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">msg</span><span class="o">.</span><span class="n">walk</span><span class="p">():</span>
|
|||
|
<span class="c1"># multipart/* are just containers</span>
|
|||
|
<span class="k">if</span> <span class="n">part</span><span class="o">.</span><span class="n">get_content_maintype</span><span class="p">()</span> <span class="o">==</span> <span class="s1">'multipart'</span><span class="p">:</span>
|
|||
|
<span class="k">continue</span>
|
|||
|
<span class="c1"># Applications should really sanitize the given filename so that an</span>
|
|||
|
<span class="c1"># email message can't be used to overwrite important files</span>
|
|||
|
<span class="n">filename</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">get_filename</span><span class="p">()</span>
|
|||
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">filename</span><span class="p">:</span>
|
|||
|
<span class="n">ext</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_extension</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">get_content_type</span><span class="p">())</span>
|
|||
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">ext</span><span class="p">:</span>
|
|||
|
<span class="c1"># Use a generic bag-of-bits extension</span>
|
|||
|
<span class="n">ext</span> <span class="o">=</span> <span class="s1">'.bin'</span>
|
|||
|
<span class="n">filename</span> <span class="o">=</span> <span class="s1">'part-</span><span class="si">%03d%s</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">counter</span><span class="p">,</span> <span class="n">ext</span><span class="p">)</span>
|
|||
|
<span class="n">counter</span> <span class="o">+=</span> <span class="mi">1</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">directory</span><span class="p">,</span> <span class="n">filename</span><span class="p">),</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
|||
|
<span class="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">get_payload</span><span class="p">(</span><span class="n">decode</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
|
|||
|
<span class="n">main</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Here’s an example of how to create an HTML message with an alternative plain
|
|||
|
text version. To make things a bit more interesting, we include a related
|
|||
|
image in the html part, and we save a copy of what we are going to send to
|
|||
|
disk, as well as sending it.</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python3</span>
|
|||
|
|
|||
|
<span class="kn">import</span> <span class="nn">smtplib</span>
|
|||
|
|
|||
|
<span class="kn">from</span> <span class="nn">email.message</span> <span class="k">import</span> <span class="n">EmailMessage</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.headerregistry</span> <span class="k">import</span> <span class="n">Address</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.utils</span> <span class="k">import</span> <span class="n">make_msgid</span>
|
|||
|
|
|||
|
<span class="c1"># Create the base text message.</span>
|
|||
|
<span class="n">msg</span> <span class="o">=</span> <span class="n">EmailMessage</span><span class="p">()</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'Subject'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"Ayons asperges pour le déjeuner"</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'From'</span><span class="p">]</span> <span class="o">=</span> <span class="n">Address</span><span class="p">(</span><span class="s2">"Pepé Le Pew"</span><span class="p">,</span> <span class="s2">"pepe"</span><span class="p">,</span> <span class="s2">"example.com"</span><span class="p">)</span>
|
|||
|
<span class="n">msg</span><span class="p">[</span><span class="s1">'To'</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">Address</span><span class="p">(</span><span class="s2">"Penelope Pussycat"</span><span class="p">,</span> <span class="s2">"penelope"</span><span class="p">,</span> <span class="s2">"example.com"</span><span class="p">),</span>
|
|||
|
<span class="n">Address</span><span class="p">(</span><span class="s2">"Fabrette Pussycat"</span><span class="p">,</span> <span class="s2">"fabrette"</span><span class="p">,</span> <span class="s2">"example.com"</span><span class="p">))</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">set_content</span><span class="p">(</span><span class="s2">"""</span><span class="se">\</span>
|
|||
|
<span class="s2">Salut!</span>
|
|||
|
|
|||
|
<span class="s2">Cela ressemble à un excellent recipie[1] déjeuner.</span>
|
|||
|
|
|||
|
<span class="s2">[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718</span>
|
|||
|
|
|||
|
<span class="s2">--Pepé</span>
|
|||
|
<span class="s2">"""</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="c1"># Add the html version. This converts the message into a multipart/alternative</span>
|
|||
|
<span class="c1"># container, with the original text message as the first part and the new html</span>
|
|||
|
<span class="c1"># message as the second part.</span>
|
|||
|
<span class="n">asparagus_cid</span> <span class="o">=</span> <span class="n">make_msgid</span><span class="p">()</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">add_alternative</span><span class="p">(</span><span class="s2">"""</span><span class="se">\</span>
|
|||
|
<span class="s2"><html></span>
|
|||
|
<span class="s2"> <head></head></span>
|
|||
|
<span class="s2"> <body></span>
|
|||
|
<span class="s2"> <p>Salut!</p></span>
|
|||
|
<span class="s2"> <p>Cela ressemble à un excellent</span>
|
|||
|
<span class="s2"> <a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718"></span>
|
|||
|
<span class="s2"> recipie</span>
|
|||
|
<span class="s2"> </a> déjeuner.</span>
|
|||
|
<span class="s2"> </p></span>
|
|||
|
<span class="s2"> <img src="cid:</span><span class="si">{asparagus_cid}</span><span class="s2">" /></span>
|
|||
|
<span class="s2"> </body></span>
|
|||
|
<span class="s2"></html></span>
|
|||
|
<span class="s2">"""</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">asparagus_cid</span><span class="o">=</span><span class="n">asparagus_cid</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">subtype</span><span class="o">=</span><span class="s1">'html'</span><span class="p">)</span>
|
|||
|
<span class="c1"># note that we needed to peel the <> off the msgid for use in the html.</span>
|
|||
|
|
|||
|
<span class="c1"># Now add the related image to the html part.</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"roasted-asparagus.jpg"</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">img</span><span class="p">:</span>
|
|||
|
<span class="n">msg</span><span class="o">.</span><span class="n">get_payload</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">add_related</span><span class="p">(</span><span class="n">img</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span> <span class="s1">'image'</span><span class="p">,</span> <span class="s1">'jpeg'</span><span class="p">,</span>
|
|||
|
<span class="n">cid</span><span class="o">=</span><span class="n">asparagus_cid</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="c1"># Make a local copy of what we are going to send.</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'outgoing.msg'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
|||
|
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">bytes</span><span class="p">(</span><span class="n">msg</span><span class="p">))</span>
|
|||
|
|
|||
|
<span class="c1"># Send the message via local SMTP server.</span>
|
|||
|
<span class="k">with</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">'localhost'</span><span class="p">)</span> <span class="k">as</span> <span class="n">s</span><span class="p">:</span>
|
|||
|
<span class="n">s</span><span class="o">.</span><span class="n">send_message</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>If we were sent the message from the last example, here is one way we could
|
|||
|
process it:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
|
|||
|
<span class="kn">import</span> <span class="nn">sys</span>
|
|||
|
<span class="kn">import</span> <span class="nn">tempfile</span>
|
|||
|
<span class="kn">import</span> <span class="nn">mimetypes</span>
|
|||
|
<span class="kn">import</span> <span class="nn">webbrowser</span>
|
|||
|
|
|||
|
<span class="c1"># Import the email modules we'll need</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email</span> <span class="k">import</span> <span class="n">policy</span>
|
|||
|
<span class="kn">from</span> <span class="nn">email.parser</span> <span class="k">import</span> <span class="n">BytesParser</span>
|
|||
|
|
|||
|
<span class="c1"># An imaginary module that would make this work and be safe.</span>
|
|||
|
<span class="kn">from</span> <span class="nn">imaginary</span> <span class="k">import</span> <span class="n">magic_html_parser</span>
|
|||
|
|
|||
|
<span class="c1"># In a real program you'd get the filename from the arguments.</span>
|
|||
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'outgoing.msg'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
|||
|
<span class="n">msg</span> <span class="o">=</span> <span class="n">BytesParser</span><span class="p">(</span><span class="n">policy</span><span class="o">=</span><span class="n">policy</span><span class="o">.</span><span class="n">default</span><span class="p">)</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="c1"># Now the header items can be accessed as a dictionary, and any non-ASCII will</span>
|
|||
|
<span class="c1"># be converted to unicode:</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'To:'</span><span class="p">,</span> <span class="n">msg</span><span class="p">[</span><span class="s1">'to'</span><span class="p">])</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'From:'</span><span class="p">,</span> <span class="n">msg</span><span class="p">[</span><span class="s1">'from'</span><span class="p">])</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Subject:'</span><span class="p">,</span> <span class="n">msg</span><span class="p">[</span><span class="s1">'subject'</span><span class="p">])</span>
|
|||
|
|
|||
|
<span class="c1"># If we want to print a preview of the message content, we can extract whatever</span>
|
|||
|
<span class="c1"># the least formatted payload is and print the first three lines. Of course,</span>
|
|||
|
<span class="c1"># if the message has no plain text part printing the first three lines of html</span>
|
|||
|
<span class="c1"># is probably useless, but this is just a conceptual example.</span>
|
|||
|
<span class="n">simplest</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_body</span><span class="p">(</span><span class="n">preferencelist</span><span class="o">=</span><span class="p">(</span><span class="s1">'plain'</span><span class="p">,</span> <span class="s1">'html'</span><span class="p">))</span>
|
|||
|
<span class="nb">print</span><span class="p">()</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">simplest</span><span class="o">.</span><span class="n">get_content</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(</span><span class="n">keepends</span><span class="o">=</span><span class="kc">True</span><span class="p">)[:</span><span class="mi">3</span><span class="p">]))</span>
|
|||
|
|
|||
|
<span class="n">ans</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">"View full message?"</span><span class="p">)</span>
|
|||
|
<span class="k">if</span> <span class="n">ans</span><span class="o">.</span><span class="n">lower</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">'n'</span><span class="p">:</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
|
|||
|
|
|||
|
<span class="c1"># We can extract the richest alternative in order to display it:</span>
|
|||
|
<span class="n">richest</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_body</span><span class="p">()</span>
|
|||
|
<span class="n">partfiles</span> <span class="o">=</span> <span class="p">{}</span>
|
|||
|
<span class="k">if</span> <span class="n">richest</span><span class="p">[</span><span class="s1">'content-type'</span><span class="p">]</span><span class="o">.</span><span class="n">maintype</span> <span class="o">==</span> <span class="s1">'text'</span><span class="p">:</span>
|
|||
|
<span class="k">if</span> <span class="n">richest</span><span class="p">[</span><span class="s1">'content-type'</span><span class="p">]</span><span class="o">.</span><span class="n">subtype</span> <span class="o">==</span> <span class="s1">'plain'</span><span class="p">:</span>
|
|||
|
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">richest</span><span class="o">.</span><span class="n">get_content</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">():</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
|
|||
|
<span class="k">elif</span> <span class="n">richest</span><span class="p">[</span><span class="s1">'content-type'</span><span class="p">]</span><span class="o">.</span><span class="n">subtype</span> <span class="o">==</span> <span class="s1">'html'</span><span class="p">:</span>
|
|||
|
<span class="n">body</span> <span class="o">=</span> <span class="n">richest</span>
|
|||
|
<span class="k">else</span><span class="p">:</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"Don't know how to display </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">richest</span><span class="o">.</span><span class="n">get_content_type</span><span class="p">()))</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
|
|||
|
<span class="k">elif</span> <span class="n">richest</span><span class="p">[</span><span class="s1">'content-type'</span><span class="p">]</span><span class="o">.</span><span class="n">content_type</span> <span class="o">==</span> <span class="s1">'multipart/related'</span><span class="p">:</span>
|
|||
|
<span class="n">body</span> <span class="o">=</span> <span class="n">richest</span><span class="o">.</span><span class="n">get_body</span><span class="p">(</span><span class="n">preferencelist</span><span class="o">=</span><span class="p">(</span><span class="s1">'html'</span><span class="p">))</span>
|
|||
|
<span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">richest</span><span class="o">.</span><span class="n">iter_attachments</span><span class="p">():</span>
|
|||
|
<span class="n">fn</span> <span class="o">=</span> <span class="n">part</span><span class="o">.</span><span class="n">get_filename</span><span class="p">()</span>
|
|||
|
<span class="k">if</span> <span class="n">fn</span><span class="p">:</span>
|
|||
|
<span class="n">extension</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">get_filename</span><span class="p">())[</span><span class="mi">1</span><span class="p">]</span>
|
|||
|
<span class="k">else</span><span class="p">:</span>
|
|||
|
<span class="n">extension</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_extension</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">get_content_type</span><span class="p">())</span>
|
|||
|
<span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">NamedTemporaryFile</span><span class="p">(</span><span class="n">suffix</span><span class="o">=</span><span class="n">extension</span><span class="p">,</span> <span class="n">delete</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
|||
|
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">get_content</span><span class="p">())</span>
|
|||
|
<span class="c1"># again strip the <> to go from email form of cid to html form.</span>
|
|||
|
<span class="n">partfiles</span><span class="p">[</span><span class="n">part</span><span class="p">[</span><span class="s1">'content-id'</span><span class="p">][</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span>
|
|||
|
<span class="k">else</span><span class="p">:</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"Don't know how to display </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">richest</span><span class="o">.</span><span class="n">get_content_type</span><span class="p">()))</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
|
|||
|
<span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">NamedTemporaryFile</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s1">'w'</span><span class="p">,</span> <span class="n">delete</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
|||
|
<span class="c1"># The magic_html_parser has to rewrite the href="cid:...." attributes to</span>
|
|||
|
<span class="c1"># point to the filenames in partfiles. It also has to do a safety-sanitize</span>
|
|||
|
<span class="c1"># of the html. It could be written using html.parser.</span>
|
|||
|
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">magic_html_parser</span><span class="p">(</span><span class="n">body</span><span class="o">.</span><span class="n">get_content</span><span class="p">(),</span> <span class="n">partfiles</span><span class="p">))</span>
|
|||
|
<span class="n">webbrowser</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
|
|||
|
<span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
|
|||
|
<span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">partfiles</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
|
|||
|
<span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">fn</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="c1"># Of course, there are lots of email messages that could break this simple</span>
|
|||
|
<span class="c1"># minded program, but it will handle the most common ones.</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Up to the prompt, the output from the above is:</p>
|
|||
|
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>To: Penelope Pussycat <penelope@example.com>, Fabrette Pussycat <fabrette@example.com>
|
|||
|
From: Pepé Le Pew <pepe@example.com>
|
|||
|
Subject: Ayons asperges pour le déjeuner
|
|||
|
|
|||
|
Salut!
|
|||
|
|
|||
|
Cela ressemble à un excellent recipie[1] déjeuner.
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p class="rubric">Footnotes</p>
|
|||
|
<dl class="footnote brackets">
|
|||
|
<dt class="label" id="id3"><span class="brackets"><a class="fn-backref" href="#id2">1</a></span></dt>
|
|||
|
<dd><p>Thanks to Matthew Dixon Cowles for the original inspiration and examples.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|||
|
<div class="sphinxsidebarwrapper">
|
|||
|
<h4>Previous topic</h4>
|
|||
|
<p class="topless"><a href="email.contentmanager.html"
|
|||
|
title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a></p>
|
|||
|
<h4>Next topic</h4>
|
|||
|
<p class="topless"><a href="email.compat32-message.html"
|
|||
|
title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.message.Message</span></code>: Representing an email message using the <code class="xref py py-data docutils literal notranslate"><span class="pre">compat32</span></code> API</a></p>
|
|||
|
<div role="note" aria-label="source link">
|
|||
|
<h3>This Page</h3>
|
|||
|
<ul class="this-page-menu">
|
|||
|
<li><a href="../bugs.html">Report a Bug</a></li>
|
|||
|
<li>
|
|||
|
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/email.examples.rst"
|
|||
|
rel="nofollow">Show Source
|
|||
|
</a>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="clearer"></div>
|
|||
|
</div>
|
|||
|
<div class="related" role="navigation" aria-label="related navigation">
|
|||
|
<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="../py-modindex.html" title="Python Module Index"
|
|||
|
>modules</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="email.compat32-message.html" title="email.message.Message: Representing an email message using the compat32 API"
|
|||
|
>next</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="email.contentmanager.html" title="email.contentmanager: Managing MIME Content"
|
|||
|
>previous</a> |</li>
|
|||
|
<li><img src="../_static/py.png" alt=""
|
|||
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|||
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|||
|
<li>
|
|||
|
<span class="language_switcher_placeholder">en</span>
|
|||
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|||
|
<a href="../index.html">Documentation </a> »
|
|||
|
</li>
|
|||
|
|
|||
|
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
|||
|
<li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> »</li>
|
|||
|
<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> »</li>
|
|||
|
<li class="right">
|
|||
|
|
|||
|
|
|||
|
<div class="inline-search" style="display: none" role="search">
|
|||
|
<form class="inline-search" action="../search.html" method="get">
|
|||
|
<input placeholder="Quick search" type="text" name="q" />
|
|||
|
<input type="submit" value="Go" />
|
|||
|
<input type="hidden" name="check_keywords" value="yes" />
|
|||
|
<input type="hidden" name="area" value="default" />
|
|||
|
</form>
|
|||
|
</div>
|
|||
|
<script type="text/javascript">$('.inline-search').show(0);</script>
|
|||
|
|
|
|||
|
</li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="footer">
|
|||
|
© <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
|
|||
|
<br />
|
|||
|
The Python Software Foundation is a non-profit corporation.
|
|||
|
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
|||
|
<br />
|
|||
|
Last updated on Jul 13, 2019.
|
|||
|
<a href="../bugs.html">Found a bug</a>?
|
|||
|
<br />
|
|||
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
|||
|
</div>
|
|||
|
|
|||
|
</body>
|
|||
|
</html>
|