gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

986 lines
89KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="imaplib — IMAP4 protocol client" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/imaplib.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/imaplib.py This module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 cli..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/imaplib.py This module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 cli..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>imaplib — IMAP4 protocol client &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="smtplib — SMTP protocol client" href="smtplib.html" />
  33. <link rel="prev" title="poplib — POP3 protocol client" href="poplib.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/imaplib.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code> — IMAP4 protocol client</a><ul>
  86. <li><a class="reference internal" href="#imap4-objects">IMAP4 Objects</a></li>
  87. <li><a class="reference internal" href="#imap4-example">IMAP4 Example</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="poplib.html"
  95. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">poplib</span></code> — POP3 protocol client</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="smtplib.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code> — SMTP protocol client</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/imaplib.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="smtplib.html" title="smtplib — SMTP protocol client"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="poplib.html" title="poplib — POP3 protocol client"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">Internet Protocols and Support</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code> — IMAP4 protocol client</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-imaplib">
  170. <span id="imaplib-imap4-protocol-client"></span><h1><a class="reference internal" href="#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> — IMAP4 protocol client<a class="headerlink" href="#module-imaplib" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/imaplib.py">Lib/imaplib.py</a></p>
  172. <hr class="docutils" id="index-0" />
  173. <p>This module defines three classes, <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a>, <a class="reference internal" href="#imaplib.IMAP4_SSL" title="imaplib.IMAP4_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4_SSL</span></code></a> and
  174. <a class="reference internal" href="#imaplib.IMAP4_stream" title="imaplib.IMAP4_stream"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4_stream</span></code></a>, which encapsulate a connection to an IMAP4 server and
  175. implement a large subset of the IMAP4rev1 client protocol as defined in
  176. <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2060.html"><strong>RFC 2060</strong></a>. It is backward compatible with IMAP4 (<span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1730.html"><strong>RFC 1730</strong></a>) servers, but
  177. note that the <code class="docutils literal notranslate"><span class="pre">STATUS</span></code> command is not supported in IMAP4.</p>
  178. <div class="availability docutils container">
  179. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  180. <p>This module does not work or is not available on WebAssembly platforms
  181. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  182. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  183. </div>
  184. <p>Three classes are provided by the <a class="reference internal" href="#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> module, <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> is the
  185. base class:</p>
  186. <dl class="py class">
  187. <dt class="sig sig-object py" id="imaplib.IMAP4">
  188. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">imaplib.</span></span><span class="sig-name descname"><span class="pre">IMAP4</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">host</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">port</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">IMAP4_PORT</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4" title="Link to this definition">¶</a></dt>
  189. <dd><p>This class implements the actual IMAP4 protocol. The connection is created and
  190. protocol version (IMAP4 or IMAP4rev1) is determined when the instance is
  191. initialized. If <em>host</em> is not specified, <code class="docutils literal notranslate"><span class="pre">''</span></code> (the local host) is used. If
  192. <em>port</em> is omitted, the standard IMAP4 port (143) is used. The optional <em>timeout</em>
  193. parameter specifies a timeout in seconds for the connection attempt.
  194. If timeout is not given or is None, the global default socket timeout is used.</p>
  195. <p>The <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> class supports the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement. When used
  196. like this, the IMAP4 <code class="docutils literal notranslate"><span class="pre">LOGOUT</span></code> command is issued automatically when the
  197. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement exits. E.g.:</p>
  198. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">imaplib</span> <span class="kn">import</span> <span class="n">IMAP4</span>
  199. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">IMAP4</span><span class="p">(</span><span class="s2">&quot;domain.org&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">M</span><span class="p">:</span>
  200. <span class="gp">... </span> <span class="n">M</span><span class="o">.</span><span class="n">noop</span><span class="p">()</span>
  201. <span class="gp">...</span>
  202. <span class="go">(&#39;OK&#39;, [b&#39;Nothing Accomplished. d25if65hy903weo.87&#39;])</span>
  203. </pre></div>
  204. </div>
  205. <div class="versionchanged">
  206. <p><span class="versionmodified changed">Changed in version 3.5: </span>Support for the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement was added.</p>
  207. </div>
  208. <div class="versionchanged">
  209. <p><span class="versionmodified changed">Changed in version 3.9: </span>The optional <em>timeout</em> parameter was added.</p>
  210. </div>
  211. </dd></dl>
  212. <p>Three exceptions are defined as attributes of the <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> class:</p>
  213. <dl class="py exception">
  214. <dt class="sig sig-object py" id="imaplib.IMAP4.error">
  215. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#imaplib.IMAP4.error" title="Link to this definition">¶</a></dt>
  216. <dd><p>Exception raised on any errors. The reason for the exception is passed to the
  217. constructor as a string.</p>
  218. </dd></dl>
  219. <dl class="py exception">
  220. <dt class="sig sig-object py" id="imaplib.IMAP4.abort">
  221. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">abort</span></span><a class="headerlink" href="#imaplib.IMAP4.abort" title="Link to this definition">¶</a></dt>
  222. <dd><p>IMAP4 server errors cause this exception to be raised. This is a sub-class of
  223. <a class="reference internal" href="#imaplib.IMAP4.error" title="imaplib.IMAP4.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IMAP4.error</span></code></a>. Note that closing the instance and instantiating a new one
  224. will usually allow recovery from this exception.</p>
  225. </dd></dl>
  226. <dl class="py exception">
  227. <dt class="sig sig-object py" id="imaplib.IMAP4.readonly">
  228. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">readonly</span></span><a class="headerlink" href="#imaplib.IMAP4.readonly" title="Link to this definition">¶</a></dt>
  229. <dd><p>This exception is raised when a writable mailbox has its status changed by the
  230. server. This is a sub-class of <a class="reference internal" href="#imaplib.IMAP4.error" title="imaplib.IMAP4.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IMAP4.error</span></code></a>. Some other client now has
  231. write permission, and the mailbox will need to be re-opened to re-obtain write
  232. permission.</p>
  233. </dd></dl>
  234. <p>There’s also a subclass for secure connections:</p>
  235. <dl class="py class">
  236. <dt class="sig sig-object py" id="imaplib.IMAP4_SSL">
  237. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">imaplib.</span></span><span class="sig-name descname"><span class="pre">IMAP4_SSL</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">host</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">port</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">IMAP4_SSL_PORT</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ssl_context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4_SSL" title="Link to this definition">¶</a></dt>
  238. <dd><p>This is a subclass derived from <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> that connects over an SSL
  239. encrypted socket (to use this class you need a socket module that was compiled
  240. with SSL support). If <em>host</em> is not specified, <code class="docutils literal notranslate"><span class="pre">''</span></code> (the local host) is used.
  241. If <em>port</em> is omitted, the standard IMAP4-over-SSL port (993) is used.
  242. <em>ssl_context</em> is a <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> object which allows bundling
  243. SSL configuration options, certificates and private keys into a single
  244. (potentially long-lived) structure. Please read <a class="reference internal" href="ssl.html#ssl-security"><span class="std std-ref">Security considerations</span></a> for
  245. best practices.</p>
  246. <p>The optional <em>timeout</em> parameter specifies a timeout in seconds for the
  247. connection attempt. If timeout is not given or is None, the global default
  248. socket timeout is used.</p>
  249. <div class="versionchanged">
  250. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>ssl_context</em> parameter was added.</p>
  251. </div>
  252. <div class="versionchanged">
  253. <p><span class="versionmodified changed">Changed in version 3.4: </span>The class now supports hostname check with
  254. <a class="reference internal" href="ssl.html#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.check_hostname</span></code></a> and <em>Server Name Indication</em> (see
  255. <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-const docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a>).</p>
  256. </div>
  257. <div class="versionchanged">
  258. <p><span class="versionmodified changed">Changed in version 3.9: </span>The optional <em>timeout</em> parameter was added.</p>
  259. </div>
  260. <div class="versionchanged">
  261. <p><span class="versionmodified changed">Changed in version 3.12: </span>The deprecated <em>keyfile</em> and <em>certfile</em> parameters have been removed.</p>
  262. </div>
  263. </dd></dl>
  264. <p>The second subclass allows for connections created by a child process:</p>
  265. <dl class="py class">
  266. <dt class="sig sig-object py" id="imaplib.IMAP4_stream">
  267. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">imaplib.</span></span><span class="sig-name descname"><span class="pre">IMAP4_stream</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">command</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4_stream" title="Link to this definition">¶</a></dt>
  268. <dd><p>This is a subclass derived from <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> that connects to the
  269. <code class="docutils literal notranslate"><span class="pre">stdin/stdout</span></code> file descriptors created by passing <em>command</em> to
  270. <code class="docutils literal notranslate"><span class="pre">subprocess.Popen()</span></code>.</p>
  271. </dd></dl>
  272. <p>The following utility functions are defined:</p>
  273. <dl class="py function">
  274. <dt class="sig sig-object py" id="imaplib.Internaldate2tuple">
  275. <span class="sig-prename descclassname"><span class="pre">imaplib.</span></span><span class="sig-name descname"><span class="pre">Internaldate2tuple</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">datestr</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.Internaldate2tuple" title="Link to this definition">¶</a></dt>
  276. <dd><p>Parse an IMAP4 <code class="docutils literal notranslate"><span class="pre">INTERNALDATE</span></code> string and return corresponding local
  277. time. The return value is a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> tuple or
  278. <code class="docutils literal notranslate"><span class="pre">None</span></code> if the string has wrong format.</p>
  279. </dd></dl>
  280. <dl class="py function">
  281. <dt class="sig sig-object py" id="imaplib.Int2AP">
  282. <span class="sig-prename descclassname"><span class="pre">imaplib.</span></span><span class="sig-name descname"><span class="pre">Int2AP</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.Int2AP" title="Link to this definition">¶</a></dt>
  283. <dd><p>Converts an integer into a bytes representation using characters from the set
  284. [<code class="docutils literal notranslate"><span class="pre">A</span></code> .. <code class="docutils literal notranslate"><span class="pre">P</span></code>].</p>
  285. </dd></dl>
  286. <dl class="py function">
  287. <dt class="sig sig-object py" id="imaplib.ParseFlags">
  288. <span class="sig-prename descclassname"><span class="pre">imaplib.</span></span><span class="sig-name descname"><span class="pre">ParseFlags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flagstr</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.ParseFlags" title="Link to this definition">¶</a></dt>
  289. <dd><p>Converts an IMAP4 <code class="docutils literal notranslate"><span class="pre">FLAGS</span></code> response to a tuple of individual flags.</p>
  290. </dd></dl>
  291. <dl class="py function">
  292. <dt class="sig sig-object py" id="imaplib.Time2Internaldate">
  293. <span class="sig-prename descclassname"><span class="pre">imaplib.</span></span><span class="sig-name descname"><span class="pre">Time2Internaldate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">date_time</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.Time2Internaldate" title="Link to this definition">¶</a></dt>
  294. <dd><p>Convert <em>date_time</em> to an IMAP4 <code class="docutils literal notranslate"><span class="pre">INTERNALDATE</span></code> representation.
  295. The return value is a string in the form: <code class="docutils literal notranslate"><span class="pre">&quot;DD-Mmm-YYYY</span> <span class="pre">HH:MM:SS</span>
  296. <span class="pre">+HHMM&quot;</span></code> (including double-quotes). The <em>date_time</em> argument can
  297. be a number (int or float) representing seconds since epoch (as
  298. returned by <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a>), a 9-tuple representing local time
  299. an instance of <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> (as returned by
  300. <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a>), an aware instance of
  301. <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a>, or a double-quoted string. In the last
  302. case, it is assumed to already be in the correct format.</p>
  303. </dd></dl>
  304. <p>Note that IMAP4 message numbers change as the mailbox changes; in particular,
  305. after an <code class="docutils literal notranslate"><span class="pre">EXPUNGE</span></code> command performs deletions the remaining messages are
  306. renumbered. So it is highly advisable to use UIDs instead, with the UID command.</p>
  307. <p>At the end of the module, there is a test section that contains a more extensive
  308. example of usage.</p>
  309. <div class="admonition seealso">
  310. <p class="admonition-title">See also</p>
  311. <p>Documents describing the protocol, sources for servers
  312. implementing it, by the University of Washington’s IMAP Information Center
  313. can all be found at (<strong>Source Code</strong>) <a class="reference external" href="https://github.com/uw-imap/imap">https://github.com/uw-imap/imap</a> (<strong>Not Maintained</strong>).</p>
  314. </div>
  315. <section id="imap4-objects">
  316. <span id="id1"></span><h2>IMAP4 Objects<a class="headerlink" href="#imap4-objects" title="Link to this heading">¶</a></h2>
  317. <p>All IMAP4rev1 commands are represented by methods of the same name, either
  318. uppercase or lowercase.</p>
  319. <p>All arguments to commands are converted to strings, except for <code class="docutils literal notranslate"><span class="pre">AUTHENTICATE</span></code>,
  320. and the last argument to <code class="docutils literal notranslate"><span class="pre">APPEND</span></code> which is passed as an IMAP4 literal. If
  321. necessary (the string contains IMAP4 protocol-sensitive characters and isn’t
  322. enclosed with either parentheses or double quotes) each string is quoted.
  323. However, the <em>password</em> argument to the <code class="docutils literal notranslate"><span class="pre">LOGIN</span></code> command is always quoted. If
  324. you want to avoid having an argument string quoted (eg: the <em>flags</em> argument to
  325. <code class="docutils literal notranslate"><span class="pre">STORE</span></code>) then enclose the string in parentheses (eg: <code class="docutils literal notranslate"><span class="pre">r'(\Deleted)'</span></code>).</p>
  326. <p>Each command returns a tuple: <code class="docutils literal notranslate"><span class="pre">(type,</span> <span class="pre">[data,</span> <span class="pre">...])</span></code> where <em>type</em> is usually
  327. <code class="docutils literal notranslate"><span class="pre">'OK'</span></code> or <code class="docutils literal notranslate"><span class="pre">'NO'</span></code>, and <em>data</em> is either the text from the command response,
  328. or mandated results from the command. Each <em>data</em> is either a <code class="docutils literal notranslate"><span class="pre">bytes</span></code>, or a
  329. tuple. If a tuple, then the first part is the header of the response, and the
  330. second part contains the data (ie: ‘literal’ value).</p>
  331. <p>The <em>message_set</em> options to commands below is a string specifying one or more
  332. messages to be acted upon. It may be a simple message number (<code class="docutils literal notranslate"><span class="pre">'1'</span></code>), a range
  333. of message numbers (<code class="docutils literal notranslate"><span class="pre">'2:4'</span></code>), or a group of non-contiguous ranges separated by
  334. commas (<code class="docutils literal notranslate"><span class="pre">'1:3,6:9'</span></code>). A range can contain an asterisk to indicate an infinite
  335. upper bound (<code class="docutils literal notranslate"><span class="pre">'3:*'</span></code>).</p>
  336. <p>An <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> instance has the following methods:</p>
  337. <dl class="py method">
  338. <dt class="sig sig-object py" id="imaplib.IMAP4.append">
  339. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">append</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">date_time</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.append" title="Link to this definition">¶</a></dt>
  340. <dd><p>Append <em>message</em> to named mailbox.</p>
  341. </dd></dl>
  342. <dl class="py method">
  343. <dt class="sig sig-object py" id="imaplib.IMAP4.authenticate">
  344. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">authenticate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mechanism</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">authobject</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.authenticate" title="Link to this definition">¶</a></dt>
  345. <dd><p>Authenticate command — requires response processing.</p>
  346. <p><em>mechanism</em> specifies which authentication mechanism is to be used - it should
  347. appear in the instance variable <code class="docutils literal notranslate"><span class="pre">capabilities</span></code> in the form <code class="docutils literal notranslate"><span class="pre">AUTH=mechanism</span></code>.</p>
  348. <p><em>authobject</em> must be a callable object:</p>
  349. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="n">authobject</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
  350. </pre></div>
  351. </div>
  352. <p>It will be called to process server continuation responses; the <em>response</em>
  353. argument it is passed will be <code class="docutils literal notranslate"><span class="pre">bytes</span></code>. It should return <code class="docutils literal notranslate"><span class="pre">bytes</span></code> <em>data</em>
  354. that will be base64 encoded and sent to the server. It should return
  355. <code class="docutils literal notranslate"><span class="pre">None</span></code> if the client abort response <code class="docutils literal notranslate"><span class="pre">*</span></code> should be sent instead.</p>
  356. <div class="versionchanged">
  357. <p><span class="versionmodified changed">Changed in version 3.5: </span>string usernames and passwords are now encoded to <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> instead of
  358. being limited to ASCII.</p>
  359. </div>
  360. </dd></dl>
  361. <dl class="py method">
  362. <dt class="sig sig-object py" id="imaplib.IMAP4.check">
  363. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">check</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.check" title="Link to this definition">¶</a></dt>
  364. <dd><p>Checkpoint mailbox on server.</p>
  365. </dd></dl>
  366. <dl class="py method">
  367. <dt class="sig sig-object py" id="imaplib.IMAP4.close">
  368. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.close" title="Link to this definition">¶</a></dt>
  369. <dd><p>Close currently selected mailbox. Deleted messages are removed from writable
  370. mailbox. This is the recommended command before <code class="docutils literal notranslate"><span class="pre">LOGOUT</span></code>.</p>
  371. </dd></dl>
  372. <dl class="py method">
  373. <dt class="sig sig-object py" id="imaplib.IMAP4.copy">
  374. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message_set</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.copy" title="Link to this definition">¶</a></dt>
  375. <dd><p>Copy <em>message_set</em> messages onto end of <em>new_mailbox</em>.</p>
  376. </dd></dl>
  377. <dl class="py method">
  378. <dt class="sig sig-object py" id="imaplib.IMAP4.create">
  379. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">create</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.create" title="Link to this definition">¶</a></dt>
  380. <dd><p>Create new mailbox named <em>mailbox</em>.</p>
  381. </dd></dl>
  382. <dl class="py method">
  383. <dt class="sig sig-object py" id="imaplib.IMAP4.delete">
  384. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">delete</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.delete" title="Link to this definition">¶</a></dt>
  385. <dd><p>Delete old mailbox named <em>mailbox</em>.</p>
  386. </dd></dl>
  387. <dl class="py method">
  388. <dt class="sig sig-object py" id="imaplib.IMAP4.deleteacl">
  389. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">deleteacl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">who</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.deleteacl" title="Link to this definition">¶</a></dt>
  390. <dd><p>Delete the ACLs (remove any rights) set for who on mailbox.</p>
  391. </dd></dl>
  392. <dl class="py method">
  393. <dt class="sig sig-object py" id="imaplib.IMAP4.enable">
  394. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">enable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">capability</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.enable" title="Link to this definition">¶</a></dt>
  395. <dd><p>Enable <em>capability</em> (see <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5161.html"><strong>RFC 5161</strong></a>). Most capabilities do not need to be
  396. enabled. Currently only the <code class="docutils literal notranslate"><span class="pre">UTF8=ACCEPT</span></code> capability is supported
  397. (see <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc6855.html"><strong>RFC 6855</strong></a>).</p>
  398. <div class="versionadded">
  399. <p><span class="versionmodified added">New in version 3.5: </span>The <a class="reference internal" href="#imaplib.IMAP4.enable" title="imaplib.IMAP4.enable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enable()</span></code></a> method itself, and <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc6855.html"><strong>RFC 6855</strong></a> support.</p>
  400. </div>
  401. </dd></dl>
  402. <dl class="py method">
  403. <dt class="sig sig-object py" id="imaplib.IMAP4.expunge">
  404. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">expunge</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.expunge" title="Link to this definition">¶</a></dt>
  405. <dd><p>Permanently remove deleted items from selected mailbox. Generates an <code class="docutils literal notranslate"><span class="pre">EXPUNGE</span></code>
  406. response for each deleted message. Returned data contains a list of <code class="docutils literal notranslate"><span class="pre">EXPUNGE</span></code>
  407. message numbers in order received.</p>
  408. </dd></dl>
  409. <dl class="py method">
  410. <dt class="sig sig-object py" id="imaplib.IMAP4.fetch">
  411. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">fetch</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message_set</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message_parts</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.fetch" title="Link to this definition">¶</a></dt>
  412. <dd><p>Fetch (parts of) messages. <em>message_parts</em> should be a string of message part
  413. names enclosed within parentheses, eg: <code class="docutils literal notranslate"><span class="pre">&quot;(UID</span> <span class="pre">BODY[TEXT])&quot;</span></code>. Returned data
  414. are tuples of message part envelope and data.</p>
  415. </dd></dl>
  416. <dl class="py method">
  417. <dt class="sig sig-object py" id="imaplib.IMAP4.getacl">
  418. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">getacl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.getacl" title="Link to this definition">¶</a></dt>
  419. <dd><p>Get the <code class="docutils literal notranslate"><span class="pre">ACL</span></code>s for <em>mailbox</em>. The method is non-standard, but is supported
  420. by the <code class="docutils literal notranslate"><span class="pre">Cyrus</span></code> server.</p>
  421. </dd></dl>
  422. <dl class="py method">
  423. <dt class="sig sig-object py" id="imaplib.IMAP4.getannotation">
  424. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">getannotation</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">entry</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attribute</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.getannotation" title="Link to this definition">¶</a></dt>
  425. <dd><p>Retrieve the specified <code class="docutils literal notranslate"><span class="pre">ANNOTATION</span></code>s for <em>mailbox</em>. The method is
  426. non-standard, but is supported by the <code class="docutils literal notranslate"><span class="pre">Cyrus</span></code> server.</p>
  427. </dd></dl>
  428. <dl class="py method">
  429. <dt class="sig sig-object py" id="imaplib.IMAP4.getquota">
  430. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">getquota</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">root</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.getquota" title="Link to this definition">¶</a></dt>
  431. <dd><p>Get the <code class="docutils literal notranslate"><span class="pre">quota</span></code> <em>root</em>’s resource usage and limits. This method is part of the
  432. IMAP4 QUOTA extension defined in rfc2087.</p>
  433. </dd></dl>
  434. <dl class="py method">
  435. <dt class="sig sig-object py" id="imaplib.IMAP4.getquotaroot">
  436. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">getquotaroot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.getquotaroot" title="Link to this definition">¶</a></dt>
  437. <dd><p>Get the list of <code class="docutils literal notranslate"><span class="pre">quota</span></code> <code class="docutils literal notranslate"><span class="pre">roots</span></code> for the named <em>mailbox</em>. This method is part
  438. of the IMAP4 QUOTA extension defined in rfc2087.</p>
  439. </dd></dl>
  440. <dl class="py method">
  441. <dt class="sig sig-object py" id="imaplib.IMAP4.list">
  442. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">list</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">directory</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">pattern</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.list" title="Link to this definition">¶</a></dt>
  443. <dd><p>List mailbox names in <em>directory</em> matching <em>pattern</em>. <em>directory</em> defaults to
  444. the top-level mail folder, and <em>pattern</em> defaults to match anything. Returned
  445. data contains a list of <code class="docutils literal notranslate"><span class="pre">LIST</span></code> responses.</p>
  446. </dd></dl>
  447. <dl class="py method">
  448. <dt class="sig sig-object py" id="imaplib.IMAP4.login">
  449. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">login</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">user</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">password</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.login" title="Link to this definition">¶</a></dt>
  450. <dd><p>Identify the client using a plaintext password. The <em>password</em> will be quoted.</p>
  451. </dd></dl>
  452. <dl class="py method">
  453. <dt class="sig sig-object py" id="imaplib.IMAP4.login_cram_md5">
  454. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">login_cram_md5</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">user</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">password</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.login_cram_md5" title="Link to this definition">¶</a></dt>
  455. <dd><p>Force use of <code class="docutils literal notranslate"><span class="pre">CRAM-MD5</span></code> authentication when identifying the client to protect
  456. the password. Will only work if the server <code class="docutils literal notranslate"><span class="pre">CAPABILITY</span></code> response includes the
  457. phrase <code class="docutils literal notranslate"><span class="pre">AUTH=CRAM-MD5</span></code>.</p>
  458. </dd></dl>
  459. <dl class="py method">
  460. <dt class="sig sig-object py" id="imaplib.IMAP4.logout">
  461. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">logout</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.logout" title="Link to this definition">¶</a></dt>
  462. <dd><p>Shutdown connection to server. Returns server <code class="docutils literal notranslate"><span class="pre">BYE</span></code> response.</p>
  463. <div class="versionchanged">
  464. <p><span class="versionmodified changed">Changed in version 3.8: </span>The method no longer ignores silently arbitrary exceptions.</p>
  465. </div>
  466. </dd></dl>
  467. <dl class="py method">
  468. <dt class="sig sig-object py" id="imaplib.IMAP4.lsub">
  469. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">lsub</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">directory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'&quot;&quot;'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pattern</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'*'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.lsub" title="Link to this definition">¶</a></dt>
  470. <dd><p>List subscribed mailbox names in directory matching pattern. <em>directory</em>
  471. defaults to the top level directory and <em>pattern</em> defaults to match any mailbox.
  472. Returned data are tuples of message part envelope and data.</p>
  473. </dd></dl>
  474. <dl class="py method">
  475. <dt class="sig sig-object py" id="imaplib.IMAP4.myrights">
  476. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">myrights</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.myrights" title="Link to this definition">¶</a></dt>
  477. <dd><p>Show my ACLs for a mailbox (i.e. the rights that I have on mailbox).</p>
  478. </dd></dl>
  479. <dl class="py method">
  480. <dt class="sig sig-object py" id="imaplib.IMAP4.namespace">
  481. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">namespace</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.namespace" title="Link to this definition">¶</a></dt>
  482. <dd><p>Returns IMAP namespaces as defined in <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2342.html"><strong>RFC 2342</strong></a>.</p>
  483. </dd></dl>
  484. <dl class="py method">
  485. <dt class="sig sig-object py" id="imaplib.IMAP4.noop">
  486. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">noop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.noop" title="Link to this definition">¶</a></dt>
  487. <dd><p>Send <code class="docutils literal notranslate"><span class="pre">NOOP</span></code> to server.</p>
  488. </dd></dl>
  489. <dl class="py method">
  490. <dt class="sig sig-object py" id="imaplib.IMAP4.open">
  491. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">host</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">port</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.open" title="Link to this definition">¶</a></dt>
  492. <dd><p>Opens socket to <em>port</em> at <em>host</em>. The optional <em>timeout</em> parameter
  493. specifies a timeout in seconds for the connection attempt.
  494. If timeout is not given or is None, the global default socket timeout
  495. is used. Also note that if the <em>timeout</em> parameter is set to be zero,
  496. it will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-class docutils literal notranslate"><span class="pre">ValueError</span></code></a> to reject creating a non-blocking socket.
  497. This method is implicitly called by the <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> constructor.
  498. The connection objects established by this method will be used in
  499. the <a class="reference internal" href="#imaplib.IMAP4.read" title="imaplib.IMAP4.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">IMAP4.read()</span></code></a>, <a class="reference internal" href="#imaplib.IMAP4.readline" title="imaplib.IMAP4.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">IMAP4.readline()</span></code></a>, <a class="reference internal" href="#imaplib.IMAP4.send" title="imaplib.IMAP4.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">IMAP4.send()</span></code></a>,
  500. and <a class="reference internal" href="#imaplib.IMAP4.shutdown" title="imaplib.IMAP4.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">IMAP4.shutdown()</span></code></a> methods. You may override this method.</p>
  501. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">imaplib.open</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">self</span></code>, <code class="docutils literal notranslate"><span class="pre">host</span></code>, <code class="docutils literal notranslate"><span class="pre">port</span></code>.</p>
  502. <div class="versionchanged">
  503. <p><span class="versionmodified changed">Changed in version 3.9: </span>The <em>timeout</em> parameter was added.</p>
  504. </div>
  505. </dd></dl>
  506. <dl class="py method">
  507. <dt class="sig sig-object py" id="imaplib.IMAP4.partial">
  508. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">partial</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message_num</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message_part</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">start</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">length</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.partial" title="Link to this definition">¶</a></dt>
  509. <dd><p>Fetch truncated part of a message. Returned data is a tuple of message part
  510. envelope and data.</p>
  511. </dd></dl>
  512. <dl class="py method">
  513. <dt class="sig sig-object py" id="imaplib.IMAP4.proxyauth">
  514. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">proxyauth</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">user</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.proxyauth" title="Link to this definition">¶</a></dt>
  515. <dd><p>Assume authentication as <em>user</em>. Allows an authorised administrator to proxy
  516. into any user’s mailbox.</p>
  517. </dd></dl>
  518. <dl class="py method">
  519. <dt class="sig sig-object py" id="imaplib.IMAP4.read">
  520. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.read" title="Link to this definition">¶</a></dt>
  521. <dd><p>Reads <em>size</em> bytes from the remote server. You may override this method.</p>
  522. </dd></dl>
  523. <dl class="py method">
  524. <dt class="sig sig-object py" id="imaplib.IMAP4.readline">
  525. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">readline</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.readline" title="Link to this definition">¶</a></dt>
  526. <dd><p>Reads one line from the remote server. You may override this method.</p>
  527. </dd></dl>
  528. <dl class="py method">
  529. <dt class="sig sig-object py" id="imaplib.IMAP4.recent">
  530. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">recent</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.recent" title="Link to this definition">¶</a></dt>
  531. <dd><p>Prompt server for an update. Returned data is <code class="docutils literal notranslate"><span class="pre">None</span></code> if no new messages, else
  532. value of <code class="docutils literal notranslate"><span class="pre">RECENT</span></code> response.</p>
  533. </dd></dl>
  534. <dl class="py method">
  535. <dt class="sig sig-object py" id="imaplib.IMAP4.rename">
  536. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">rename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">oldmailbox</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newmailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.rename" title="Link to this definition">¶</a></dt>
  537. <dd><p>Rename mailbox named <em>oldmailbox</em> to <em>newmailbox</em>.</p>
  538. </dd></dl>
  539. <dl class="py method">
  540. <dt class="sig sig-object py" id="imaplib.IMAP4.response">
  541. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">response</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.response" title="Link to this definition">¶</a></dt>
  542. <dd><p>Return data for response <em>code</em> if received, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. Returns the given
  543. code, instead of the usual type.</p>
  544. </dd></dl>
  545. <dl class="py method">
  546. <dt class="sig sig-object py" id="imaplib.IMAP4.search">
  547. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">search</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">charset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">criterion</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.search" title="Link to this definition">¶</a></dt>
  548. <dd><p>Search mailbox for matching messages. <em>charset</em> may be <code class="docutils literal notranslate"><span class="pre">None</span></code>, in which case
  549. no <code class="docutils literal notranslate"><span class="pre">CHARSET</span></code> will be specified in the request to the server. The IMAP
  550. protocol requires that at least one criterion be specified; an exception will be
  551. raised when the server returns an error. <em>charset</em> must be <code class="docutils literal notranslate"><span class="pre">None</span></code> if
  552. the <code class="docutils literal notranslate"><span class="pre">UTF8=ACCEPT</span></code> capability was enabled using the <a class="reference internal" href="#imaplib.IMAP4.enable" title="imaplib.IMAP4.enable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enable()</span></code></a>
  553. command.</p>
  554. <p>Example:</p>
  555. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># M is a connected IMAP4 instance...</span>
  556. <span class="n">typ</span><span class="p">,</span> <span class="n">msgnums</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;FROM&#39;</span><span class="p">,</span> <span class="s1">&#39;&quot;LDJ&quot;&#39;</span><span class="p">)</span>
  557. <span class="c1"># or:</span>
  558. <span class="n">typ</span><span class="p">,</span> <span class="n">msgnums</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;(FROM &quot;LDJ&quot;)&#39;</span><span class="p">)</span>
  559. </pre></div>
  560. </div>
  561. </dd></dl>
  562. <dl class="py method">
  563. <dt class="sig sig-object py" id="imaplib.IMAP4.select">
  564. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">select</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'INBOX'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">readonly</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.select" title="Link to this definition">¶</a></dt>
  565. <dd><p>Select a mailbox. Returned data is the count of messages in <em>mailbox</em>
  566. (<code class="docutils literal notranslate"><span class="pre">EXISTS</span></code> response). The default <em>mailbox</em> is <code class="docutils literal notranslate"><span class="pre">'INBOX'</span></code>. If the <em>readonly</em>
  567. flag is set, modifications to the mailbox are not allowed.</p>
  568. </dd></dl>
  569. <dl class="py method">
  570. <dt class="sig sig-object py" id="imaplib.IMAP4.send">
  571. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">send</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.send" title="Link to this definition">¶</a></dt>
  572. <dd><p>Sends <code class="docutils literal notranslate"><span class="pre">data</span></code> to the remote server. You may override this method.</p>
  573. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">imaplib.send</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">self</span></code>, <code class="docutils literal notranslate"><span class="pre">data</span></code>.</p>
  574. </dd></dl>
  575. <dl class="py method">
  576. <dt class="sig sig-object py" id="imaplib.IMAP4.setacl">
  577. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">setacl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">who</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">what</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.setacl" title="Link to this definition">¶</a></dt>
  578. <dd><p>Set an <code class="docutils literal notranslate"><span class="pre">ACL</span></code> for <em>mailbox</em>. The method is non-standard, but is supported by
  579. the <code class="docutils literal notranslate"><span class="pre">Cyrus</span></code> server.</p>
  580. </dd></dl>
  581. <dl class="py method">
  582. <dt class="sig sig-object py" id="imaplib.IMAP4.setannotation">
  583. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">setannotation</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">entry</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attribute</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.setannotation" title="Link to this definition">¶</a></dt>
  584. <dd><p>Set <code class="docutils literal notranslate"><span class="pre">ANNOTATION</span></code>s for <em>mailbox</em>. The method is non-standard, but is
  585. supported by the <code class="docutils literal notranslate"><span class="pre">Cyrus</span></code> server.</p>
  586. </dd></dl>
  587. <dl class="py method">
  588. <dt class="sig sig-object py" id="imaplib.IMAP4.setquota">
  589. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">setquota</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">root</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limits</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.setquota" title="Link to this definition">¶</a></dt>
  590. <dd><p>Set the <code class="docutils literal notranslate"><span class="pre">quota</span></code> <em>root</em>’s resource <em>limits</em>. This method is part of the IMAP4
  591. QUOTA extension defined in rfc2087.</p>
  592. </dd></dl>
  593. <dl class="py method">
  594. <dt class="sig sig-object py" id="imaplib.IMAP4.shutdown">
  595. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">shutdown</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.shutdown" title="Link to this definition">¶</a></dt>
  596. <dd><p>Close connection established in <code class="docutils literal notranslate"><span class="pre">open</span></code>. This method is implicitly
  597. called by <a class="reference internal" href="#imaplib.IMAP4.logout" title="imaplib.IMAP4.logout"><code class="xref py py-meth docutils literal notranslate"><span class="pre">IMAP4.logout()</span></code></a>. You may override this method.</p>
  598. </dd></dl>
  599. <dl class="py method">
  600. <dt class="sig sig-object py" id="imaplib.IMAP4.socket">
  601. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">socket</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.socket" title="Link to this definition">¶</a></dt>
  602. <dd><p>Returns socket instance used to connect to server.</p>
  603. </dd></dl>
  604. <dl class="py method">
  605. <dt class="sig sig-object py" id="imaplib.IMAP4.sort">
  606. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">sort</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sort_criteria</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">search_criterion</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.sort" title="Link to this definition">¶</a></dt>
  607. <dd><p>The <code class="docutils literal notranslate"><span class="pre">sort</span></code> command is a variant of <code class="docutils literal notranslate"><span class="pre">search</span></code> with sorting semantics for the
  608. results. Returned data contains a space separated list of matching message
  609. numbers.</p>
  610. <p>Sort has two arguments before the <em>search_criterion</em> argument(s); a
  611. parenthesized list of <em>sort_criteria</em>, and the searching <em>charset</em>. Note that
  612. unlike <code class="docutils literal notranslate"><span class="pre">search</span></code>, the searching <em>charset</em> argument is mandatory. There is also
  613. a <code class="docutils literal notranslate"><span class="pre">uid</span> <span class="pre">sort</span></code> command which corresponds to <code class="docutils literal notranslate"><span class="pre">sort</span></code> the way that <code class="docutils literal notranslate"><span class="pre">uid</span> <span class="pre">search</span></code>
  614. corresponds to <code class="docutils literal notranslate"><span class="pre">search</span></code>. The <code class="docutils literal notranslate"><span class="pre">sort</span></code> command first searches the mailbox for
  615. messages that match the given searching criteria using the charset argument for
  616. the interpretation of strings in the searching criteria. It then returns the
  617. numbers of matching messages.</p>
  618. <p>This is an <code class="docutils literal notranslate"><span class="pre">IMAP4rev1</span></code> extension command.</p>
  619. </dd></dl>
  620. <dl class="py method">
  621. <dt class="sig sig-object py" id="imaplib.IMAP4.starttls">
  622. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">starttls</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ssl_context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.starttls" title="Link to this definition">¶</a></dt>
  623. <dd><p>Send a <code class="docutils literal notranslate"><span class="pre">STARTTLS</span></code> command. The <em>ssl_context</em> argument is optional
  624. and should be a <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> object. This will enable
  625. encryption on the IMAP connection. Please read <a class="reference internal" href="ssl.html#ssl-security"><span class="std std-ref">Security considerations</span></a> for
  626. best practices.</p>
  627. <div class="versionadded">
  628. <p><span class="versionmodified added">New in version 3.2.</span></p>
  629. </div>
  630. <div class="versionchanged">
  631. <p><span class="versionmodified changed">Changed in version 3.4: </span>The method now supports hostname check with
  632. <a class="reference internal" href="ssl.html#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.check_hostname</span></code></a> and <em>Server Name Indication</em> (see
  633. <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-const docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a>).</p>
  634. </div>
  635. </dd></dl>
  636. <dl class="py method">
  637. <dt class="sig sig-object py" id="imaplib.IMAP4.status">
  638. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">status</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">names</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.status" title="Link to this definition">¶</a></dt>
  639. <dd><p>Request named status conditions for <em>mailbox</em>.</p>
  640. </dd></dl>
  641. <dl class="py method">
  642. <dt class="sig sig-object py" id="imaplib.IMAP4.store">
  643. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">store</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message_set</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">command</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flag_list</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.store" title="Link to this definition">¶</a></dt>
  644. <dd><p>Alters flag dispositions for messages in mailbox. <em>command</em> is specified by
  645. section 6.4.6 of <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2060.html"><strong>RFC 2060</strong></a> as being one of “FLAGS”, “+FLAGS”, or “-FLAGS”,
  646. optionally with a suffix of “.SILENT”.</p>
  647. <p>For example, to set the delete flag on all messages:</p>
  648. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">typ</span><span class="p">,</span> <span class="n">data</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;ALL&#39;</span><span class="p">)</span>
  649. <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">():</span>
  650. <span class="n">M</span><span class="o">.</span><span class="n">store</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="s1">&#39;+FLAGS&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">Deleted&#39;</span><span class="p">)</span>
  651. <span class="n">M</span><span class="o">.</span><span class="n">expunge</span><span class="p">()</span>
  652. </pre></div>
  653. </div>
  654. <div class="admonition note">
  655. <p class="admonition-title">Note</p>
  656. <p>Creating flags containing ‘]’ (for example: “[test]”) violates
  657. <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3501.html"><strong>RFC 3501</strong></a> (the IMAP protocol). However, imaplib has historically
  658. allowed creation of such tags, and popular IMAP servers, such as Gmail,
  659. accept and produce such flags. There are non-Python programs which also
  660. create such tags. Although it is an RFC violation and IMAP clients and
  661. servers are supposed to be strict, imaplib nonetheless continues to allow
  662. such tags to be created for backward compatibility reasons, and as of
  663. Python 3.6, handles them if they are sent from the server, since this
  664. improves real-world compatibility.</p>
  665. </div>
  666. </dd></dl>
  667. <dl class="py method">
  668. <dt class="sig sig-object py" id="imaplib.IMAP4.subscribe">
  669. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">subscribe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.subscribe" title="Link to this definition">¶</a></dt>
  670. <dd><p>Subscribe to new mailbox.</p>
  671. </dd></dl>
  672. <dl class="py method">
  673. <dt class="sig sig-object py" id="imaplib.IMAP4.thread">
  674. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">thread</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">threading_algorithm</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">search_criterion</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.thread" title="Link to this definition">¶</a></dt>
  675. <dd><p>The <code class="docutils literal notranslate"><span class="pre">thread</span></code> command is a variant of <code class="docutils literal notranslate"><span class="pre">search</span></code> with threading semantics for
  676. the results. Returned data contains a space separated list of thread members.</p>
  677. <p>Thread members consist of zero or more messages numbers, delimited by spaces,
  678. indicating successive parent and child.</p>
  679. <p>Thread has two arguments before the <em>search_criterion</em> argument(s); a
  680. <em>threading_algorithm</em>, and the searching <em>charset</em>. Note that unlike
  681. <code class="docutils literal notranslate"><span class="pre">search</span></code>, the searching <em>charset</em> argument is mandatory. There is also a
  682. <code class="docutils literal notranslate"><span class="pre">uid</span> <span class="pre">thread</span></code> command which corresponds to <code class="docutils literal notranslate"><span class="pre">thread</span></code> the way that <code class="docutils literal notranslate"><span class="pre">uid</span>
  683. <span class="pre">search</span></code> corresponds to <code class="docutils literal notranslate"><span class="pre">search</span></code>. The <code class="docutils literal notranslate"><span class="pre">thread</span></code> command first searches the
  684. mailbox for messages that match the given searching criteria using the <em>charset</em>
  685. argument for the interpretation of strings in the searching criteria. It then
  686. returns the matching messages threaded according to the specified threading
  687. algorithm.</p>
  688. <p>This is an <code class="docutils literal notranslate"><span class="pre">IMAP4rev1</span></code> extension command.</p>
  689. </dd></dl>
  690. <dl class="py method">
  691. <dt class="sig sig-object py" id="imaplib.IMAP4.uid">
  692. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">uid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">command</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.uid" title="Link to this definition">¶</a></dt>
  693. <dd><p>Execute command args with messages identified by UID, rather than message
  694. number. Returns response appropriate to command. At least one argument must be
  695. supplied; if none are provided, the server will return an error and an exception
  696. will be raised.</p>
  697. </dd></dl>
  698. <dl class="py method">
  699. <dt class="sig sig-object py" id="imaplib.IMAP4.unsubscribe">
  700. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">unsubscribe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mailbox</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.unsubscribe" title="Link to this definition">¶</a></dt>
  701. <dd><p>Unsubscribe from old mailbox.</p>
  702. </dd></dl>
  703. <dl class="py method">
  704. <dt class="sig sig-object py" id="imaplib.IMAP4.unselect">
  705. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">unselect</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.unselect" title="Link to this definition">¶</a></dt>
  706. <dd><p><a class="reference internal" href="#imaplib.IMAP4.unselect" title="imaplib.IMAP4.unselect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">imaplib.IMAP4.unselect()</span></code></a> frees server’s resources associated with the
  707. selected mailbox and returns the server to the authenticated
  708. state. This command performs the same actions as <a class="reference internal" href="#imaplib.IMAP4.close" title="imaplib.IMAP4.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">imaplib.IMAP4.close()</span></code></a>, except
  709. that no messages are permanently removed from the currently
  710. selected mailbox.</p>
  711. <div class="versionadded">
  712. <p><span class="versionmodified added">New in version 3.9.</span></p>
  713. </div>
  714. </dd></dl>
  715. <dl class="py method">
  716. <dt class="sig sig-object py" id="imaplib.IMAP4.xatom">
  717. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">xatom</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imaplib.IMAP4.xatom" title="Link to this definition">¶</a></dt>
  718. <dd><p>Allow simple extension commands notified by server in <code class="docutils literal notranslate"><span class="pre">CAPABILITY</span></code> response.</p>
  719. </dd></dl>
  720. <p>The following attributes are defined on instances of <a class="reference internal" href="#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a>:</p>
  721. <dl class="py attribute">
  722. <dt class="sig sig-object py" id="imaplib.IMAP4.PROTOCOL_VERSION">
  723. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">PROTOCOL_VERSION</span></span><a class="headerlink" href="#imaplib.IMAP4.PROTOCOL_VERSION" title="Link to this definition">¶</a></dt>
  724. <dd><p>The most recent supported protocol in the <code class="docutils literal notranslate"><span class="pre">CAPABILITY</span></code> response from the
  725. server.</p>
  726. </dd></dl>
  727. <dl class="py attribute">
  728. <dt class="sig sig-object py" id="imaplib.IMAP4.debug">
  729. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">debug</span></span><a class="headerlink" href="#imaplib.IMAP4.debug" title="Link to this definition">¶</a></dt>
  730. <dd><p>Integer value to control debugging output. The initialize value is taken from
  731. the module variable <code class="docutils literal notranslate"><span class="pre">Debug</span></code>. Values greater than three trace each command.</p>
  732. </dd></dl>
  733. <dl class="py attribute">
  734. <dt class="sig sig-object py" id="imaplib.IMAP4.utf8_enabled">
  735. <span class="sig-prename descclassname"><span class="pre">IMAP4.</span></span><span class="sig-name descname"><span class="pre">utf8_enabled</span></span><a class="headerlink" href="#imaplib.IMAP4.utf8_enabled" title="Link to this definition">¶</a></dt>
  736. <dd><p>Boolean value that is normally <code class="docutils literal notranslate"><span class="pre">False</span></code>, but is set to <code class="docutils literal notranslate"><span class="pre">True</span></code> if an
  737. <a class="reference internal" href="#imaplib.IMAP4.enable" title="imaplib.IMAP4.enable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enable()</span></code></a> command is successfully issued for the <code class="docutils literal notranslate"><span class="pre">UTF8=ACCEPT</span></code>
  738. capability.</p>
  739. <div class="versionadded">
  740. <p><span class="versionmodified added">New in version 3.5.</span></p>
  741. </div>
  742. </dd></dl>
  743. </section>
  744. <section id="imap4-example">
  745. <span id="id2"></span><h2>IMAP4 Example<a class="headerlink" href="#imap4-example" title="Link to this heading">¶</a></h2>
  746. <p>Here is a minimal example (without error checking) that opens a mailbox and
  747. retrieves and prints all messages:</p>
  748. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">getpass</span><span class="o">,</span> <span class="nn">imaplib</span>
  749. <span class="n">M</span> <span class="o">=</span> <span class="n">imaplib</span><span class="o">.</span><span class="n">IMAP4</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="s1">&#39;example.org&#39;</span><span class="p">)</span>
  750. <span class="n">M</span><span class="o">.</span><span class="n">login</span><span class="p">(</span><span class="n">getpass</span><span class="o">.</span><span class="n">getuser</span><span class="p">(),</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">())</span>
  751. <span class="n">M</span><span class="o">.</span><span class="n">select</span><span class="p">()</span>
  752. <span class="n">typ</span><span class="p">,</span> <span class="n">data</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;ALL&#39;</span><span class="p">)</span>
  753. <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">():</span>
  754. <span class="n">typ</span><span class="p">,</span> <span class="n">data</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="s1">&#39;(RFC822)&#39;</span><span class="p">)</span>
  755. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Message </span><span class="si">%s</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]))</span>
  756. <span class="n">M</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  757. <span class="n">M</span><span class="o">.</span><span class="n">logout</span><span class="p">()</span>
  758. </pre></div>
  759. </div>
  760. </section>
  761. </section>
  762. <div class="clearer"></div>
  763. </div>
  764. </div>
  765. </div>
  766. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  767. <div class="sphinxsidebarwrapper">
  768. <div>
  769. <h3><a href="../contents.html">Table of Contents</a></h3>
  770. <ul>
  771. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code> — IMAP4 protocol client</a><ul>
  772. <li><a class="reference internal" href="#imap4-objects">IMAP4 Objects</a></li>
  773. <li><a class="reference internal" href="#imap4-example">IMAP4 Example</a></li>
  774. </ul>
  775. </li>
  776. </ul>
  777. </div>
  778. <div>
  779. <h4>Previous topic</h4>
  780. <p class="topless"><a href="poplib.html"
  781. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">poplib</span></code> — POP3 protocol client</a></p>
  782. </div>
  783. <div>
  784. <h4>Next topic</h4>
  785. <p class="topless"><a href="smtplib.html"
  786. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code> — SMTP protocol client</a></p>
  787. </div>
  788. <div role="note" aria-label="source link">
  789. <h3>This Page</h3>
  790. <ul class="this-page-menu">
  791. <li><a href="../bugs.html">Report a Bug</a></li>
  792. <li>
  793. <a href="https://github.com/python/cpython/blob/main/Doc/library/imaplib.rst"
  794. rel="nofollow">Show Source
  795. </a>
  796. </li>
  797. </ul>
  798. </div>
  799. </div>
  800. <div id="sidebarbutton" title="Collapse sidebar">
  801. <span>«</span>
  802. </div>
  803. </div>
  804. <div class="clearer"></div>
  805. </div>
  806. <div class="related" role="navigation" aria-label="related navigation">
  807. <h3>Navigation</h3>
  808. <ul>
  809. <li class="right" style="margin-right: 10px">
  810. <a href="../genindex.html" title="General Index"
  811. >index</a></li>
  812. <li class="right" >
  813. <a href="../py-modindex.html" title="Python Module Index"
  814. >modules</a> |</li>
  815. <li class="right" >
  816. <a href="smtplib.html" title="smtplib — SMTP protocol client"
  817. >next</a> |</li>
  818. <li class="right" >
  819. <a href="poplib.html" title="poplib — POP3 protocol client"
  820. >previous</a> |</li>
  821. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  822. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  823. <li class="switchers">
  824. <div class="language_switcher_placeholder"></div>
  825. <div class="version_switcher_placeholder"></div>
  826. </li>
  827. <li>
  828. </li>
  829. <li id="cpython-language-and-version">
  830. <a href="../index.html">3.12.3 Documentation</a> &#187;
  831. </li>
  832. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  833. <li class="nav-item nav-item-2"><a href="internet.html" >Internet Protocols and Support</a> &#187;</li>
  834. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code> — IMAP4 protocol client</a></li>
  835. <li class="right">
  836. <div class="inline-search" role="search">
  837. <form class="inline-search" action="../search.html" method="get">
  838. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  839. <input type="submit" value="Go" />
  840. </form>
  841. </div>
  842. |
  843. </li>
  844. <li class="right">
  845. <label class="theme-selector-label">
  846. Theme
  847. <select class="theme-selector" oninput="activateTheme(this.value)">
  848. <option value="auto" selected>Auto</option>
  849. <option value="light">Light</option>
  850. <option value="dark">Dark</option>
  851. </select>
  852. </label> |</li>
  853. </ul>
  854. </div>
  855. <div class="footer">
  856. &copy;
  857. <a href="../copyright.html">
  858. Copyright
  859. </a>
  860. 2001-2024, Python Software Foundation.
  861. <br />
  862. This page is licensed under the Python Software Foundation License Version 2.
  863. <br />
  864. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  865. <br />
  866. See <a href="/license.html">History and License</a> for more information.<br />
  867. <br />
  868. The Python Software Foundation is a non-profit corporation.
  869. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  870. <br />
  871. <br />
  872. Last updated on Apr 09, 2024 (13:47 UTC).
  873. <a href="/bugs.html">Found a bug</a>?
  874. <br />
  875. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  876. </div>
  877. </body>
  878. </html>
上海开阖软件有限公司 沪ICP备12045867号-1