1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>19.3. mailcap — Mailcap file handling — Python 3.5.1 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.1 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
<link rel="up" title="19. Internet Data Handling" href="netdata.html" />
34
<link rel="next" title="19.4. mailbox — Manipulate mailboxes in various formats" href="mailbox.html" />
35
<link rel="prev" title="19.2. json — JSON encoder and decoder" href="json.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="mailbox.html" title="19.4. mailbox — Manipulate mailboxes in various formats"
55
accesskey="N">next</a> |</li>
57
<a href="json.html" title="19.2. json — JSON encoder and decoder"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">3.5.1</span>
64
<a href="../index.html">Documentation </a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">19. Internet Data Handling</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-mailcap">
78
<span id="mailcap-mailcap-file-handling"></span><h1>19.3. <a class="reference internal" href="#module-mailcap" title="mailcap: Mailcap file handling."><code class="xref py py-mod docutils literal"><span class="pre">mailcap</span></code></a> — Mailcap file handling<a class="headerlink" href="#module-mailcap" title="Permalink to this headline">¶</a></h1>
79
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/mailcap.py">Lib/mailcap.py</a></p>
80
<hr class="docutils" />
81
<p>Mailcap files are used to configure how MIME-aware applications such as mail
82
readers and Web browsers react to files with different MIME types. (The name
83
“mailcap” is derived from the phrase “mail capability”.) For example, a mailcap
84
file might contain a line like <code class="docutils literal"><span class="pre">video/mpeg;</span> <span class="pre">xmpeg</span> <span class="pre">%s</span></code>. Then, if the user
85
encounters an email message or Web document with the MIME type
86
<em class="mimetype">video/mpeg</em>, <code class="docutils literal"><span class="pre">%s</span></code> will be replaced by a filename (usually one
87
belonging to a temporary file) and the <strong class="program">xmpeg</strong> program can be
88
automatically started to view the file.</p>
89
<p>The mailcap format is documented in <span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1524.html"><strong>RFC 1524</strong></a>, “A User Agent Configuration
90
Mechanism For Multimedia Mail Format Information,” but is not an Internet
91
standard. However, mailcap files are supported on most Unix systems.</p>
93
<dt id="mailcap.findmatch">
94
<code class="descclassname">mailcap.</code><code class="descname">findmatch</code><span class="sig-paren">(</span><em>caps</em>, <em>MIMEtype</em>, <em>key='view'</em>, <em>filename='/dev/null'</em>, <em>plist=[]</em><span class="sig-paren">)</span><a class="headerlink" href="#mailcap.findmatch" title="Permalink to this definition">¶</a></dt>
95
<dd><p>Return a 2-tuple; the first element is a string containing the command line to
96
be executed (which can be passed to <a class="reference internal" href="os.html#os.system" title="os.system"><code class="xref py py-func docutils literal"><span class="pre">os.system()</span></code></a>), and the second element
97
is the mailcap entry for a given MIME type. If no matching MIME type can be
98
found, <code class="docutils literal"><span class="pre">(None,</span> <span class="pre">None)</span></code> is returned.</p>
99
<p><em>key</em> is the name of the field desired, which represents the type of activity to
100
be performed; the default value is ‘view’, since in the most common case you
101
simply want to view the body of the MIME-typed data. Other possible values
102
might be ‘compose’ and ‘edit’, if you wanted to create a new body of the given
103
MIME type or alter the existing body data. See <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1524.html"><strong>RFC 1524</strong></a> for a complete list
105
<p><em>filename</em> is the filename to be substituted for <code class="docutils literal"><span class="pre">%s</span></code> in the command line; the
106
default value is <code class="docutils literal"><span class="pre">'/dev/null'</span></code> which is almost certainly not what you want, so
107
usually you’ll override it by specifying a filename.</p>
108
<p><em>plist</em> can be a list containing named parameters; the default value is simply
109
an empty list. Each entry in the list must be a string containing the parameter
110
name, an equals sign (<code class="docutils literal"><span class="pre">'='</span></code>), and the parameter’s value. Mailcap entries can
111
contain named parameters like <code class="docutils literal"><span class="pre">%{foo}</span></code>, which will be replaced by the value
112
of the parameter named ‘foo’. For example, if the command line <code class="docutils literal"><span class="pre">showpartial</span>
113
<span class="pre">%{id}</span> <span class="pre">%{number}</span> <span class="pre">%{total}</span></code> was in a mailcap file, and <em>plist</em> was set to
114
<code class="docutils literal"><span class="pre">['id=1',</span> <span class="pre">'number=2',</span> <span class="pre">'total=3']</span></code>, the resulting command line would be
115
<code class="docutils literal"><span class="pre">'showpartial</span> <span class="pre">1</span> <span class="pre">2</span> <span class="pre">3'</span></code>.</p>
116
<p>In a mailcap file, the “test” field can optionally be specified to test some
117
external condition (such as the machine architecture, or the window system in
118
use) to determine whether or not the mailcap line applies. <a class="reference internal" href="#mailcap.findmatch" title="mailcap.findmatch"><code class="xref py py-func docutils literal"><span class="pre">findmatch()</span></code></a>
119
will automatically check such conditions and skip the entry if the check fails.</p>
122
<dl class="function">
123
<dt id="mailcap.getcaps">
124
<code class="descclassname">mailcap.</code><code class="descname">getcaps</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailcap.getcaps" title="Permalink to this definition">¶</a></dt>
125
<dd><p>Returns a dictionary mapping MIME types to a list of mailcap file entries. This
126
dictionary must be passed to the <a class="reference internal" href="#mailcap.findmatch" title="mailcap.findmatch"><code class="xref py py-func docutils literal"><span class="pre">findmatch()</span></code></a> function. An entry is stored
127
as a list of dictionaries, but it shouldn’t be necessary to know the details of
128
this representation.</p>
129
<p>The information is derived from all of the mailcap files found on the system.
130
Settings in the user’s mailcap file <code class="file docutils literal"><span class="pre">$HOME/.mailcap</span></code> will override
131
settings in the system mailcap files <code class="file docutils literal"><span class="pre">/etc/mailcap</span></code>,
132
<code class="file docutils literal"><span class="pre">/usr/etc/mailcap</span></code>, and <code class="file docutils literal"><span class="pre">/usr/local/etc/mailcap</span></code>.</p>
135
<p>An example usage:</p>
136
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">mailcap</span>
137
<span class="gp">>>> </span><span class="n">d</span><span class="o">=</span><span class="n">mailcap</span><span class="o">.</span><span class="n">getcaps</span><span class="p">()</span>
138
<span class="gp">>>> </span><span class="n">mailcap</span><span class="o">.</span><span class="n">findmatch</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s1">'video/mpeg'</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'tmp1223'</span><span class="p">)</span>
139
<span class="go">('xmpeg tmp1223', {'view': 'xmpeg %s'})</span>
148
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
149
<div class="sphinxsidebarwrapper">
150
<h4>Previous topic</h4>
151
<p class="topless"><a href="json.html"
152
title="previous chapter">19.2. <code class="docutils literal"><span class="pre">json</span></code> — JSON encoder and decoder</a></p>
154
<p class="topless"><a href="mailbox.html"
155
title="next chapter">19.4. <code class="docutils literal"><span class="pre">mailbox</span></code> — Manipulate mailboxes in various formats</a></p>
157
<ul class="this-page-menu">
158
<li><a href="../bugs.html">Report a Bug</a></li>
159
<li><a href="../_sources/library/mailcap.txt"
160
rel="nofollow">Show Source</a></li>
163
<div id="searchbox" style="display: none" role="search">
164
<h3>Quick search</h3>
165
<form class="search" action="../search.html" method="get">
166
<input type="text" name="q" />
167
<input type="submit" value="Go" />
168
<input type="hidden" name="check_keywords" value="yes" />
169
<input type="hidden" name="area" value="default" />
171
<p class="searchtip" style="font-size: 90%">
172
Enter search terms or a module, class or function name.
175
<script type="text/javascript">$('#searchbox').show(0);</script>
178
<div class="clearer"></div>
180
<div class="related" role="navigation" aria-label="related navigation">
183
<li class="right" style="margin-right: 10px">
184
<a href="../genindex.html" title="General Index"
187
<a href="../py-modindex.html" title="Python Module Index"
190
<a href="mailbox.html" title="19.4. mailbox — Manipulate mailboxes in various formats"
193
<a href="json.html" title="19.2. json — JSON encoder and decoder"
195
<li><img src="../_static/py.png" alt=""
196
style="vertical-align: middle; margin-top: -1px"/></li>
197
<li><a href="https://www.python.org/">Python</a> »</li>
199
<span class="version_switcher_placeholder">3.5.1</span>
200
<a href="../index.html">Documentation </a> »
203
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
204
<li class="nav-item nav-item-2"><a href="netdata.html" >19. Internet Data Handling</a> »</li>
208
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
210
The Python Software Foundation is a non-profit corporation.
211
<a href="https://www.python.org/psf/donations/">Please donate.</a>
213
Last updated on Jan 22, 2016.
214
<a href="../bugs.html">Found a bug</a>?
216
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'