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