| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`imaplib` --- IMAP4 protocol client | 
 | 2 | ======================================== | 
 | 3 |  | 
 | 4 | .. module:: imaplib | 
 | 5 |    :synopsis: IMAP4 protocol client (requires sockets). | 
 | 6 | .. moduleauthor:: Piers Lauder <piers@communitysolutions.com.au> | 
 | 7 | .. sectionauthor:: Piers Lauder <piers@communitysolutions.com.au> | 
| Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 8 | .. revised by ESR, January 2000 | 
 | 9 | .. changes for IMAP4_SSL by Tino Lange <Tino.Lange@isg.de>, March 2002 | 
 | 10 | .. changes for IMAP4_stream by Piers Lauder <piers@communitysolutions.com.au>, | 
 | 11 |    November 2002 | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 |  | 
 | 13 |  | 
 | 14 | .. index:: | 
 | 15 |    pair: IMAP4; protocol | 
 | 16 |    pair: IMAP4_SSL; protocol | 
 | 17 |    pair: IMAP4_stream; protocol | 
 | 18 |  | 
| Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 19 | **Source code:** :source:`Lib/imaplib.py` | 
 | 20 |  | 
 | 21 | -------------- | 
 | 22 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | This module defines three classes, :class:`IMAP4`, :class:`IMAP4_SSL` and | 
 | 24 | :class:`IMAP4_stream`, which encapsulate a connection to an IMAP4 server and | 
 | 25 | implement a large subset of the IMAP4rev1 client protocol as defined in | 
 | 26 | :rfc:`2060`. It is backward compatible with IMAP4 (:rfc:`1730`) servers, but | 
 | 27 | note that the ``STATUS`` command is not supported in IMAP4. | 
 | 28 |  | 
 | 29 | Three classes are provided by the :mod:`imaplib` module, :class:`IMAP4` is the | 
 | 30 | base class: | 
 | 31 |  | 
 | 32 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 33 | .. class:: IMAP4(host='', port=IMAP4_PORT) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 |  | 
 | 35 |    This class implements the actual IMAP4 protocol.  The connection is created and | 
 | 36 |    protocol version (IMAP4 or IMAP4rev1) is determined when the instance is | 
 | 37 |    initialized. If *host* is not specified, ``''`` (the local host) is used. If | 
 | 38 |    *port* is omitted, the standard IMAP4 port (143) is used. | 
 | 39 |  | 
| Serhiy Storchaka | 38684c3 | 2014-09-09 19:07:49 +0300 | [diff] [blame] | 40 |    The :class:`IMAP4` class supports the :keyword:`with` statement.  When used | 
 | 41 |    like this, the IMAP4 ``LOGOUT`` command is issued automatically when the | 
 | 42 |    :keyword:`with` statement exits.  E.g.:: | 
 | 43 |  | 
 | 44 |     >>> from imaplib import IMAP4 | 
 | 45 |     >>> with IMAP4("domain.org") as M: | 
 | 46 |     ...     M.noop() | 
 | 47 |     ... | 
 | 48 |     ('OK', [b'Nothing Accomplished. d25if65hy903weo.87']) | 
 | 49 |  | 
 | 50 |    .. versionchanged:: 3.5 | 
 | 51 |       Support for the :keyword:`with` statement was added. | 
 | 52 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | Three exceptions are defined as attributes of the :class:`IMAP4` class: | 
 | 54 |  | 
 | 55 |  | 
 | 56 | .. exception:: IMAP4.error | 
 | 57 |  | 
 | 58 |    Exception raised on any errors.  The reason for the exception is passed to the | 
 | 59 |    constructor as a string. | 
 | 60 |  | 
 | 61 |  | 
 | 62 | .. exception:: IMAP4.abort | 
 | 63 |  | 
 | 64 |    IMAP4 server errors cause this exception to be raised.  This is a sub-class of | 
 | 65 |    :exc:`IMAP4.error`.  Note that closing the instance and instantiating a new one | 
 | 66 |    will usually allow recovery from this exception. | 
 | 67 |  | 
 | 68 |  | 
 | 69 | .. exception:: IMAP4.readonly | 
 | 70 |  | 
 | 71 |    This exception is raised when a writable mailbox has its status changed by the | 
 | 72 |    server.  This is a sub-class of :exc:`IMAP4.error`.  Some other client now has | 
 | 73 |    write permission, and the mailbox will need to be re-opened to re-obtain write | 
 | 74 |    permission. | 
 | 75 |  | 
| Antoine Pitrou | f3b001f | 2010-11-12 18:49:16 +0000 | [diff] [blame] | 76 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | There's also a subclass for secure connections: | 
 | 78 |  | 
 | 79 |  | 
| R David Murray | a6429db | 2015-05-10 19:17:23 -0400 | [diff] [blame] | 80 | .. class:: IMAP4_SSL(host='', port=IMAP4_SSL_PORT, keyfile=None, \ | 
 | 81 |                      certfile=None, ssl_context=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 |  | 
 | 83 |    This is a subclass derived from :class:`IMAP4` that connects over an SSL | 
 | 84 |    encrypted socket (to use this class you need a socket module that was compiled | 
 | 85 |    with SSL support).  If *host* is not specified, ``''`` (the local host) is used. | 
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 86 |    If *port* is omitted, the standard IMAP4-over-SSL port (993) is used. | 
 | 87 |    *ssl_context* is a :class:`ssl.SSLContext` object which allows bundling | 
 | 88 |    SSL configuration options, certificates and private keys into a single | 
 | 89 |    (potentially long-lived) structure.  Please read :ref:`ssl-security` for | 
 | 90 |    best practices. | 
 | 91 |  | 
 | 92 |    *keyfile* and *certfile* are a legacy alternative to *ssl_context* - they | 
 | 93 |    can point to PEM-formatted private key and certificate chain files for | 
 | 94 |    the SSL connection.  Note that the *keyfile*/*certfile* parameters are | 
 | 95 |    mutually exclusive with *ssl_context*, a :class:`ValueError` is raised | 
 | 96 |    if *keyfile*/*certfile* is provided along with *ssl_context*. | 
| Antoine Pitrou | 0872816 | 2011-05-06 18:49:52 +0200 | [diff] [blame] | 97 |  | 
 | 98 |    .. versionchanged:: 3.3 | 
 | 99 |       *ssl_context* parameter added. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 |  | 
| Christian Heimes | 48aae57 | 2013-12-02 20:01:29 +0100 | [diff] [blame] | 101 |    .. versionchanged:: 3.4 | 
 | 102 |       The class now supports hostname check with | 
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 103 |       :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see | 
 | 104 |       :data:`ssl.HAS_SNI`). | 
| Antoine Pitrou | f3b001f | 2010-11-12 18:49:16 +0000 | [diff] [blame] | 105 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | The second subclass allows for connections created by a child process: | 
 | 107 |  | 
 | 108 |  | 
 | 109 | .. class:: IMAP4_stream(command) | 
 | 110 |  | 
 | 111 |    This is a subclass derived from :class:`IMAP4` that connects to the | 
 | 112 |    ``stdin/stdout`` file descriptors created by passing *command* to | 
| Christian Heimes | fb5faf0 | 2008-11-05 19:39:50 +0000 | [diff] [blame] | 113 |    ``subprocess.Popen()``. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 |  | 
 | 116 | The following utility functions are defined: | 
 | 117 |  | 
 | 118 |  | 
 | 119 | .. function:: Internaldate2tuple(datestr) | 
 | 120 |  | 
| Alexander Belopolsky | 41a99bc | 2011-01-19 19:53:30 +0000 | [diff] [blame] | 121 |    Parse an IMAP4 ``INTERNALDATE`` string and return corresponding local | 
 | 122 |    time.  The return value is a :class:`time.struct_time` tuple or | 
 | 123 |    None if the string has wrong format. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 |  | 
 | 125 | .. function:: Int2AP(num) | 
 | 126 |  | 
 | 127 |    Converts an integer into a string representation using characters from the set | 
 | 128 |    [``A`` .. ``P``]. | 
 | 129 |  | 
 | 130 |  | 
 | 131 | .. function:: ParseFlags(flagstr) | 
 | 132 |  | 
 | 133 |    Converts an IMAP4 ``FLAGS`` response to a tuple of individual flags. | 
 | 134 |  | 
 | 135 |  | 
 | 136 | .. function:: Time2Internaldate(date_time) | 
 | 137 |  | 
| Alexander Belopolsky | 8141cc7 | 2012-06-22 21:03:39 -0400 | [diff] [blame] | 138 |    Convert *date_time* to an IMAP4 ``INTERNALDATE`` representation. | 
 | 139 |    The return value is a string in the form: ``"DD-Mmm-YYYY HH:MM:SS | 
 | 140 |    +HHMM"`` (including double-quotes).  The *date_time* argument can | 
 | 141 |    be a number (int or float) representing seconds since epoch (as | 
 | 142 |    returned by :func:`time.time`), a 9-tuple representing local time | 
 | 143 |    an instance of :class:`time.struct_time` (as returned by | 
 | 144 |    :func:`time.localtime`), an aware instance of | 
 | 145 |    :class:`datetime.datetime`, or a double-quoted string.  In the last | 
 | 146 |    case, it is assumed to already be in the correct format. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 |  | 
 | 148 | Note that IMAP4 message numbers change as the mailbox changes; in particular, | 
 | 149 | after an ``EXPUNGE`` command performs deletions the remaining messages are | 
 | 150 | renumbered. So it is highly advisable to use UIDs instead, with the UID command. | 
 | 151 |  | 
 | 152 | At the end of the module, there is a test section that contains a more extensive | 
 | 153 | example of usage. | 
 | 154 |  | 
 | 155 |  | 
 | 156 | .. seealso:: | 
 | 157 |  | 
 | 158 |    Documents describing the protocol, and sources and binaries  for servers | 
 | 159 |    implementing it, can all be found at the University of Washington's *IMAP | 
| Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 160 |    Information Center* (https://www.washington.edu/imap/). | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 |  | 
 | 162 |  | 
 | 163 | .. _imap4-objects: | 
 | 164 |  | 
 | 165 | IMAP4 Objects | 
 | 166 | ------------- | 
 | 167 |  | 
 | 168 | All IMAP4rev1 commands are represented by methods of the same name, either | 
 | 169 | upper-case or lower-case. | 
 | 170 |  | 
 | 171 | All arguments to commands are converted to strings, except for ``AUTHENTICATE``, | 
 | 172 | and the last argument to ``APPEND`` which is passed as an IMAP4 literal.  If | 
 | 173 | necessary (the string contains IMAP4 protocol-sensitive characters and isn't | 
 | 174 | enclosed with either parentheses or double quotes) each string is quoted. | 
 | 175 | However, the *password* argument to the ``LOGIN`` command is always quoted. If | 
 | 176 | you want to avoid having an argument string quoted (eg: the *flags* argument to | 
 | 177 | ``STORE``) then enclose the string in parentheses (eg: ``r'(\Deleted)'``). | 
 | 178 |  | 
 | 179 | Each command returns a tuple: ``(type, [data, ...])`` where *type* is usually | 
 | 180 | ``'OK'`` or ``'NO'``, and *data* is either the text from the command response, | 
 | 181 | or mandated results from the command. Each *data* is either a string, or a | 
 | 182 | tuple. If a tuple, then the first part is the header of the response, and the | 
 | 183 | second part contains the data (ie: 'literal' value). | 
 | 184 |  | 
 | 185 | The *message_set* options to commands below is a string specifying one or more | 
 | 186 | messages to be acted upon.  It may be a simple message number (``'1'``), a range | 
 | 187 | of message numbers (``'2:4'``), or a group of non-contiguous ranges separated by | 
 | 188 | commas (``'1:3,6:9'``).  A range can contain an asterisk to indicate an infinite | 
 | 189 | upper bound (``'3:*'``). | 
 | 190 |  | 
 | 191 | An :class:`IMAP4` instance has the following methods: | 
 | 192 |  | 
 | 193 |  | 
 | 194 | .. method:: IMAP4.append(mailbox, flags, date_time, message) | 
 | 195 |  | 
 | 196 |    Append *message* to named mailbox. | 
 | 197 |  | 
 | 198 |  | 
 | 199 | .. method:: IMAP4.authenticate(mechanism, authobject) | 
 | 200 |  | 
 | 201 |    Authenticate command --- requires response processing. | 
 | 202 |  | 
 | 203 |    *mechanism* specifies which authentication mechanism is to be used - it should | 
 | 204 |    appear in the instance variable ``capabilities`` in the form ``AUTH=mechanism``. | 
 | 205 |  | 
 | 206 |    *authobject* must be a callable object:: | 
 | 207 |  | 
 | 208 |       data = authobject(response) | 
 | 209 |  | 
| R David Murray | 774a39f | 2013-02-19 12:17:31 -0500 | [diff] [blame] | 210 |    It will be called to process server continuation responses; the *response* | 
 | 211 |    argument it is passed will be ``bytes``.  It should return ``bytes`` *data* | 
 | 212 |    that will be base64 encoded and sent to the server.  It should return | 
 | 213 |    ``None`` if the client abort response ``*`` should be sent instead. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 |  | 
| R David Murray | a6429db | 2015-05-10 19:17:23 -0400 | [diff] [blame] | 215 |    .. versionchanged:: 3.5 | 
 | 216 |       string usernames and passwords are now encoded to ``utf-8`` instead of | 
 | 217 |       being limited to ASCII. | 
 | 218 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 |  | 
 | 220 | .. method:: IMAP4.check() | 
 | 221 |  | 
 | 222 |    Checkpoint mailbox on server. | 
 | 223 |  | 
 | 224 |  | 
 | 225 | .. method:: IMAP4.close() | 
 | 226 |  | 
 | 227 |    Close currently selected mailbox. Deleted messages are removed from writable | 
 | 228 |    mailbox. This is the recommended command before ``LOGOUT``. | 
 | 229 |  | 
 | 230 |  | 
 | 231 | .. method:: IMAP4.copy(message_set, new_mailbox) | 
 | 232 |  | 
 | 233 |    Copy *message_set* messages onto end of *new_mailbox*. | 
 | 234 |  | 
 | 235 |  | 
 | 236 | .. method:: IMAP4.create(mailbox) | 
 | 237 |  | 
 | 238 |    Create new mailbox named *mailbox*. | 
 | 239 |  | 
 | 240 |  | 
 | 241 | .. method:: IMAP4.delete(mailbox) | 
 | 242 |  | 
 | 243 |    Delete old mailbox named *mailbox*. | 
 | 244 |  | 
 | 245 |  | 
 | 246 | .. method:: IMAP4.deleteacl(mailbox, who) | 
 | 247 |  | 
 | 248 |    Delete the ACLs (remove any rights) set for who on mailbox. | 
 | 249 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 |  | 
| R David Murray | a6429db | 2015-05-10 19:17:23 -0400 | [diff] [blame] | 251 | .. method:: IMAP4.enable(capability) | 
 | 252 |  | 
 | 253 |    Enable *capability* (see :rfc:`5161`).  Most capabilities do not need to be | 
 | 254 |    enabled.  Currently only the ``UTF8=ACCEPT`` capability is supported | 
 | 255 |    (see :RFC:`6855`). | 
 | 256 |  | 
 | 257 |    .. versionadded:: 3.5 | 
 | 258 |       The :meth:`enable` method itself, and :RFC:`6855` support. | 
 | 259 |  | 
 | 260 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | .. method:: IMAP4.expunge() | 
 | 262 |  | 
 | 263 |    Permanently remove deleted items from selected mailbox. Generates an ``EXPUNGE`` | 
 | 264 |    response for each deleted message. Returned data contains a list of ``EXPUNGE`` | 
 | 265 |    message numbers in order received. | 
 | 266 |  | 
 | 267 |  | 
 | 268 | .. method:: IMAP4.fetch(message_set, message_parts) | 
 | 269 |  | 
 | 270 |    Fetch (parts of) messages.  *message_parts* should be a string of message part | 
 | 271 |    names enclosed within parentheses, eg: ``"(UID BODY[TEXT])"``.  Returned data | 
 | 272 |    are tuples of message part envelope and data. | 
 | 273 |  | 
 | 274 |  | 
 | 275 | .. method:: IMAP4.getacl(mailbox) | 
 | 276 |  | 
 | 277 |    Get the ``ACL``\ s for *mailbox*. The method is non-standard, but is supported | 
 | 278 |    by the ``Cyrus`` server. | 
 | 279 |  | 
 | 280 |  | 
 | 281 | .. method:: IMAP4.getannotation(mailbox, entry, attribute) | 
 | 282 |  | 
 | 283 |    Retrieve the specified ``ANNOTATION``\ s for *mailbox*. The method is | 
 | 284 |    non-standard, but is supported by the ``Cyrus`` server. | 
 | 285 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 |  | 
 | 287 | .. method:: IMAP4.getquota(root) | 
 | 288 |  | 
 | 289 |    Get the ``quota`` *root*'s resource usage and limits. This method is part of the | 
 | 290 |    IMAP4 QUOTA extension defined in rfc2087. | 
 | 291 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 |  | 
 | 293 | .. method:: IMAP4.getquotaroot(mailbox) | 
 | 294 |  | 
 | 295 |    Get the list of ``quota`` ``roots`` for the named *mailbox*. This method is part | 
 | 296 |    of the IMAP4 QUOTA extension defined in rfc2087. | 
 | 297 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 |  | 
 | 299 | .. method:: IMAP4.list([directory[, pattern]]) | 
 | 300 |  | 
 | 301 |    List mailbox names in *directory* matching *pattern*.  *directory* defaults to | 
 | 302 |    the top-level mail folder, and *pattern* defaults to match anything.  Returned | 
 | 303 |    data contains a list of ``LIST`` responses. | 
 | 304 |  | 
 | 305 |  | 
 | 306 | .. method:: IMAP4.login(user, password) | 
 | 307 |  | 
 | 308 |    Identify the client using a plaintext password. The *password* will be quoted. | 
 | 309 |  | 
 | 310 |  | 
 | 311 | .. method:: IMAP4.login_cram_md5(user, password) | 
 | 312 |  | 
 | 313 |    Force use of ``CRAM-MD5`` authentication when identifying the client to protect | 
 | 314 |    the password.  Will only work if the server ``CAPABILITY`` response includes the | 
 | 315 |    phrase ``AUTH=CRAM-MD5``. | 
 | 316 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 |  | 
 | 318 | .. method:: IMAP4.logout() | 
 | 319 |  | 
 | 320 |    Shutdown connection to server. Returns server ``BYE`` response. | 
 | 321 |  | 
 | 322 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 323 | .. method:: IMAP4.lsub(directory='""', pattern='*') | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 324 |  | 
 | 325 |    List subscribed mailbox names in directory matching pattern. *directory* | 
 | 326 |    defaults to the top level directory and *pattern* defaults to match any mailbox. | 
 | 327 |    Returned data are tuples of message part envelope and data. | 
 | 328 |  | 
 | 329 |  | 
 | 330 | .. method:: IMAP4.myrights(mailbox) | 
 | 331 |  | 
 | 332 |    Show my ACLs for a mailbox (i.e. the rights that I have on mailbox). | 
 | 333 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 334 |  | 
 | 335 | .. method:: IMAP4.namespace() | 
 | 336 |  | 
 | 337 |    Returns IMAP namespaces as defined in RFC2342. | 
 | 338 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 |  | 
 | 340 | .. method:: IMAP4.noop() | 
 | 341 |  | 
 | 342 |    Send ``NOOP`` to server. | 
 | 343 |  | 
 | 344 |  | 
 | 345 | .. method:: IMAP4.open(host, port) | 
 | 346 |  | 
| Antoine Pitrou | fdded56 | 2011-02-07 15:58:11 +0000 | [diff] [blame] | 347 |    Opens socket to *port* at *host*.  This method is implicitly called by | 
 | 348 |    the :class:`IMAP4` constructor.  The connection objects established by this | 
| Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 349 |    method will be used in the :meth:`IMAP4.read`, :meth:`IMAP4.readline`, | 
 | 350 |    :meth:`IMAP4.send`, and :meth:`IMAP4.shutdown` methods.  You may override | 
 | 351 |    this method. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 352 |  | 
 | 353 |  | 
 | 354 | .. method:: IMAP4.partial(message_num, message_part, start, length) | 
 | 355 |  | 
 | 356 |    Fetch truncated part of a message. Returned data is a tuple of message part | 
 | 357 |    envelope and data. | 
 | 358 |  | 
 | 359 |  | 
 | 360 | .. method:: IMAP4.proxyauth(user) | 
 | 361 |  | 
 | 362 |    Assume authentication as *user*. Allows an authorised administrator to proxy | 
 | 363 |    into any user's mailbox. | 
 | 364 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 |  | 
 | 366 | .. method:: IMAP4.read(size) | 
 | 367 |  | 
 | 368 |    Reads *size* bytes from the remote server. You may override this method. | 
 | 369 |  | 
 | 370 |  | 
 | 371 | .. method:: IMAP4.readline() | 
 | 372 |  | 
 | 373 |    Reads one line from the remote server. You may override this method. | 
 | 374 |  | 
 | 375 |  | 
 | 376 | .. method:: IMAP4.recent() | 
 | 377 |  | 
 | 378 |    Prompt server for an update. Returned data is ``None`` if no new messages, else | 
 | 379 |    value of ``RECENT`` response. | 
 | 380 |  | 
 | 381 |  | 
 | 382 | .. method:: IMAP4.rename(oldmailbox, newmailbox) | 
 | 383 |  | 
 | 384 |    Rename mailbox named *oldmailbox* to *newmailbox*. | 
 | 385 |  | 
 | 386 |  | 
 | 387 | .. method:: IMAP4.response(code) | 
 | 388 |  | 
 | 389 |    Return data for response *code* if received, or ``None``. Returns the given | 
 | 390 |    code, instead of the usual type. | 
 | 391 |  | 
 | 392 |  | 
 | 393 | .. method:: IMAP4.search(charset, criterion[, ...]) | 
 | 394 |  | 
 | 395 |    Search mailbox for matching messages.  *charset* may be ``None``, in which case | 
 | 396 |    no ``CHARSET`` will be specified in the request to the server.  The IMAP | 
 | 397 |    protocol requires that at least one criterion be specified; an exception will be | 
| R David Murray | a6429db | 2015-05-10 19:17:23 -0400 | [diff] [blame] | 398 |    raised when the server returns an error.  *charset* must be ``None`` if | 
 | 399 |    the ``UTF8=ACCEPT`` capability was enabled using the :meth:`enable` | 
 | 400 |    command. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 |  | 
 | 402 |    Example:: | 
 | 403 |  | 
 | 404 |       # M is a connected IMAP4 instance... | 
 | 405 |       typ, msgnums = M.search(None, 'FROM', '"LDJ"') | 
 | 406 |  | 
 | 407 |       # or: | 
 | 408 |       typ, msgnums = M.search(None, '(FROM "LDJ")') | 
 | 409 |  | 
 | 410 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 411 | .. method:: IMAP4.select(mailbox='INBOX', readonly=False) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 412 |  | 
 | 413 |    Select a mailbox. Returned data is the count of messages in *mailbox* | 
 | 414 |    (``EXISTS`` response).  The default *mailbox* is ``'INBOX'``.  If the *readonly* | 
 | 415 |    flag is set, modifications to the mailbox are not allowed. | 
 | 416 |  | 
 | 417 |  | 
 | 418 | .. method:: IMAP4.send(data) | 
 | 419 |  | 
 | 420 |    Sends ``data`` to the remote server. You may override this method. | 
 | 421 |  | 
 | 422 |  | 
 | 423 | .. method:: IMAP4.setacl(mailbox, who, what) | 
 | 424 |  | 
 | 425 |    Set an ``ACL`` for *mailbox*. The method is non-standard, but is supported by | 
 | 426 |    the ``Cyrus`` server. | 
 | 427 |  | 
 | 428 |  | 
 | 429 | .. method:: IMAP4.setannotation(mailbox, entry, attribute[, ...]) | 
 | 430 |  | 
 | 431 |    Set ``ANNOTATION``\ s for *mailbox*. The method is non-standard, but is | 
 | 432 |    supported by the ``Cyrus`` server. | 
 | 433 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 |  | 
 | 435 | .. method:: IMAP4.setquota(root, limits) | 
 | 436 |  | 
 | 437 |    Set the ``quota`` *root*'s resource *limits*. This method is part of the IMAP4 | 
 | 438 |    QUOTA extension defined in rfc2087. | 
 | 439 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 |  | 
 | 441 | .. method:: IMAP4.shutdown() | 
 | 442 |  | 
| Antoine Pitrou | fdded56 | 2011-02-07 15:58:11 +0000 | [diff] [blame] | 443 |    Close connection established in ``open``.  This method is implicitly | 
 | 444 |    called by :meth:`IMAP4.logout`.  You may override this method. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 445 |  | 
 | 446 |  | 
 | 447 | .. method:: IMAP4.socket() | 
 | 448 |  | 
 | 449 |    Returns socket instance used to connect to server. | 
 | 450 |  | 
 | 451 |  | 
 | 452 | .. method:: IMAP4.sort(sort_criteria, charset, search_criterion[, ...]) | 
 | 453 |  | 
 | 454 |    The ``sort`` command is a variant of ``search`` with sorting semantics for the | 
 | 455 |    results.  Returned data contains a space separated list of matching message | 
 | 456 |    numbers. | 
 | 457 |  | 
 | 458 |    Sort has two arguments before the *search_criterion* argument(s); a | 
 | 459 |    parenthesized list of *sort_criteria*, and the searching *charset*.  Note that | 
 | 460 |    unlike ``search``, the searching *charset* argument is mandatory.  There is also | 
 | 461 |    a ``uid sort`` command which corresponds to ``sort`` the way that ``uid search`` | 
 | 462 |    corresponds to ``search``.  The ``sort`` command first searches the mailbox for | 
 | 463 |    messages that match the given searching criteria using the charset argument for | 
 | 464 |    the interpretation of strings in the searching criteria.  It then returns the | 
 | 465 |    numbers of matching messages. | 
 | 466 |  | 
 | 467 |    This is an ``IMAP4rev1`` extension command. | 
 | 468 |  | 
 | 469 |  | 
| Antoine Pitrou | f3b001f | 2010-11-12 18:49:16 +0000 | [diff] [blame] | 470 | .. method:: IMAP4.starttls(ssl_context=None) | 
 | 471 |  | 
 | 472 |    Send a ``STARTTLS`` command.  The *ssl_context* argument is optional | 
 | 473 |    and should be a :class:`ssl.SSLContext` object.  This will enable | 
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 474 |    encryption on the IMAP connection.  Please read :ref:`ssl-security` for | 
 | 475 |    best practices. | 
| Antoine Pitrou | f3b001f | 2010-11-12 18:49:16 +0000 | [diff] [blame] | 476 |  | 
 | 477 |    .. versionadded:: 3.2 | 
 | 478 |  | 
| Christian Heimes | 48aae57 | 2013-12-02 20:01:29 +0100 | [diff] [blame] | 479 |    .. versionchanged:: 3.4 | 
 | 480 |       The method now supports hostname check with | 
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 481 |       :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see | 
 | 482 |       :data:`ssl.HAS_SNI`). | 
| Antoine Pitrou | f3b001f | 2010-11-12 18:49:16 +0000 | [diff] [blame] | 483 |  | 
| Georg Brandl | df48b97 | 2014-03-24 09:06:18 +0100 | [diff] [blame] | 484 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 485 | .. method:: IMAP4.status(mailbox, names) | 
 | 486 |  | 
 | 487 |    Request named status conditions for *mailbox*. | 
 | 488 |  | 
 | 489 |  | 
 | 490 | .. method:: IMAP4.store(message_set, command, flag_list) | 
 | 491 |  | 
 | 492 |    Alters flag dispositions for messages in mailbox.  *command* is specified by | 
 | 493 |    section 6.4.6 of :rfc:`2060` as being one of "FLAGS", "+FLAGS", or "-FLAGS", | 
 | 494 |    optionally with a suffix of ".SILENT". | 
 | 495 |  | 
 | 496 |    For example, to set the delete flag on all messages:: | 
 | 497 |  | 
 | 498 |       typ, data = M.search(None, 'ALL') | 
 | 499 |       for num in data[0].split(): | 
 | 500 |          M.store(num, '+FLAGS', '\\Deleted') | 
 | 501 |       M.expunge() | 
 | 502 |  | 
 | 503 |  | 
 | 504 | .. method:: IMAP4.subscribe(mailbox) | 
 | 505 |  | 
 | 506 |    Subscribe to new mailbox. | 
 | 507 |  | 
 | 508 |  | 
 | 509 | .. method:: IMAP4.thread(threading_algorithm, charset, search_criterion[, ...]) | 
 | 510 |  | 
 | 511 |    The ``thread`` command is a variant of ``search`` with threading semantics for | 
 | 512 |    the results.  Returned data contains a space separated list of thread members. | 
 | 513 |  | 
 | 514 |    Thread members consist of zero or more messages numbers, delimited by spaces, | 
 | 515 |    indicating successive parent and child. | 
 | 516 |  | 
 | 517 |    Thread has two arguments before the *search_criterion* argument(s); a | 
 | 518 |    *threading_algorithm*, and the searching *charset*.  Note that unlike | 
 | 519 |    ``search``, the searching *charset* argument is mandatory.  There is also a | 
 | 520 |    ``uid thread`` command which corresponds to ``thread`` the way that ``uid | 
 | 521 |    search`` corresponds to ``search``.  The ``thread`` command first searches the | 
 | 522 |    mailbox for messages that match the given searching criteria using the charset | 
 | 523 |    argument for the interpretation of strings in the searching criteria. It then | 
 | 524 |    returns the matching messages threaded according to the specified threading | 
 | 525 |    algorithm. | 
 | 526 |  | 
 | 527 |    This is an ``IMAP4rev1`` extension command. | 
 | 528 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 529 |  | 
 | 530 | .. method:: IMAP4.uid(command, arg[, ...]) | 
 | 531 |  | 
 | 532 |    Execute command args with messages identified by UID, rather than message | 
 | 533 |    number.  Returns response appropriate to command.  At least one argument must be | 
 | 534 |    supplied; if none are provided, the server will return an error and an exception | 
 | 535 |    will be raised. | 
 | 536 |  | 
 | 537 |  | 
 | 538 | .. method:: IMAP4.unsubscribe(mailbox) | 
 | 539 |  | 
 | 540 |    Unsubscribe from old mailbox. | 
 | 541 |  | 
 | 542 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 543 | .. method:: IMAP4.xatom(name[, ...]) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 544 |  | 
 | 545 |    Allow simple extension commands notified by server in ``CAPABILITY`` response. | 
 | 546 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 547 |  | 
| Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 548 | The following attributes are defined on instances of :class:`IMAP4`: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 549 |  | 
 | 550 | .. attribute:: IMAP4.PROTOCOL_VERSION | 
 | 551 |  | 
 | 552 |    The most recent supported protocol in the ``CAPABILITY`` response from the | 
 | 553 |    server. | 
 | 554 |  | 
 | 555 |  | 
 | 556 | .. attribute:: IMAP4.debug | 
 | 557 |  | 
 | 558 |    Integer value to control debugging output.  The initialize value is taken from | 
 | 559 |    the module variable ``Debug``.  Values greater than three trace each command. | 
 | 560 |  | 
 | 561 |  | 
| R David Murray | a6429db | 2015-05-10 19:17:23 -0400 | [diff] [blame] | 562 | .. attribute:: IMAP4.utf8_enabled | 
 | 563 |  | 
 | 564 |    Boolean value that is normally ``False``, but is set to ``True`` if an | 
 | 565 |    :meth:`enable` command is successfully issued for the ``UTF8=ACCEPT`` | 
 | 566 |    capability. | 
 | 567 |  | 
 | 568 |    .. versionadded:: 3.5 | 
 | 569 |  | 
 | 570 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 571 | .. _imap4-example: | 
 | 572 |  | 
 | 573 | IMAP4 Example | 
 | 574 | ------------- | 
 | 575 |  | 
 | 576 | Here is a minimal example (without error checking) that opens a mailbox and | 
 | 577 | retrieves and prints all messages:: | 
 | 578 |  | 
 | 579 |    import getpass, imaplib | 
 | 580 |  | 
 | 581 |    M = imaplib.IMAP4() | 
 | 582 |    M.login(getpass.getuser(), getpass.getpass()) | 
 | 583 |    M.select() | 
 | 584 |    typ, data = M.search(None, 'ALL') | 
 | 585 |    for num in data[0].split(): | 
 | 586 |        typ, data = M.fetch(num, '(RFC822)') | 
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 587 |        print('Message %s\n%s\n' % (num, data[0][1])) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 588 |    M.close() | 
 | 589 |    M.logout() | 
 | 590 |  |