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