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