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).} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 6 | \moduleauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au} |
| 7 | \sectionauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 8 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 9 | % Based on HTML documentation by Piers Lauder <piers@staff.cs.usyd.edu.au>; |
| 10 | % converted by Fred L. Drake, Jr. <fdrake@acm.org>. |
| 11 | |
Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 12 | \indexii{IMAP4}{protocol} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 13 | |
| 14 | This module defines a class, \class{IMAP4}, which encapsulates a |
| 15 | connection to an IMAP4 server and implements the IMAP4rev1 client |
| 16 | protocol as defined in \rfc{2060}. It is backward compatible with |
| 17 | IMAP4 (\rfc{1730}) servers, but note that the \samp{STATUS} command is |
| 18 | not supported in IMAP4. |
| 19 | |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 20 | A single class is provided by the \module{imaplib} module: |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 21 | |
| 22 | \begin{classdesc}{IMAP4}{\optional{host\optional{, port}}} |
| 23 | This class implements the actual IMAP4 protocol. The connection is |
| 24 | created and protocol version (IMAP4 or IMAP4rev1) is determined when |
| 25 | the instance is initialized. |
| 26 | If \var{host} is not specified, \code{''} (the local host) is used. |
| 27 | If \var{port} is omitted, the standard IMAP4 port (143) is used. |
| 28 | \end{classdesc} |
| 29 | |
| 30 | Two exceptions are defined as attributes of the \class{IMAP4} class: |
| 31 | |
| 32 | \begin{excdesc}{IMAP4.error} |
| 33 | Exception raised on any errors. The reason for the exception is |
| 34 | passed to the constructor as a string. |
| 35 | \end{excdesc} |
| 36 | |
| 37 | \begin{excdesc}{IMAP4.abort} |
| 38 | IMAP4 server errors cause this exception to be raised. This is a |
| 39 | sub-class of \exception{IMAP4.error}. Note that closing the instance |
| 40 | and instantiating a new one will usually allow recovery from this |
| 41 | exception. |
| 42 | \end{excdesc} |
| 43 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 44 | \begin{excdesc}{IMAP4.readonly} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 45 | This exception is raised when a writable mailbox has its status changed by the server. This is a |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 46 | sub-class of \exception{IMAP4.error}. Some other client now has write permission, |
| 47 | and the mailbox will need to be re-opened to re-obtain write permission. |
| 48 | \end{excdesc} |
| 49 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 50 | The following utility functions are defined: |
| 51 | |
| 52 | \begin{funcdesc}{Internaldate2tuple}{datestr} |
| 53 | Converts an IMAP4 INTERNALDATE string to Coordinated Universal |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 54 | Time. Returns a \refmodule{time} module tuple. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 55 | \end{funcdesc} |
| 56 | |
| 57 | \begin{funcdesc}{Int2AP}{num} |
| 58 | Converts an integer into a string representation using characters |
| 59 | from the set [\code{A} .. \code{P}]. |
| 60 | \end{funcdesc} |
| 61 | |
| 62 | \begin{funcdesc}{ParseFlags}{flagstr} |
| 63 | Converts an IMAP4 \samp{FLAGS} response to a tuple of individual |
| 64 | flags. |
| 65 | \end{funcdesc} |
| 66 | |
| 67 | \begin{funcdesc}{Time2Internaldate}{date_time} |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 68 | Converts a \refmodule{time} module tuple to an IMAP4 |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 69 | \samp{INTERNALDATE} representation. Returns a string in the form: |
| 70 | \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes). |
| 71 | \end{funcdesc} |
| 72 | |
| 73 | |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 74 | Note that IMAP4 message numbers change as the mailbox changes, so it |
| 75 | is highly advisable to use UIDs instead, with the UID command. |
| 76 | |
| 77 | At the end of the module, there is a test section that contains a more |
| 78 | extensive example of usage. |
| 79 | |
| 80 | \begin{seealso} |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 81 | \seetext{Documents describing the protocol, and sources and binaries |
| 82 | for servers implementing it, can all be found at the |
| 83 | University of Washington's \emph{IMAP Information Center} |
| 84 | (\url{http://www.cac.washington.edu/imap/}).} |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 85 | \end{seealso} |
| 86 | |
| 87 | |
| 88 | \subsection{IMAP4 Objects \label{imap4-objects}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 89 | |
| 90 | All IMAP4rev1 commands are represented by methods of the same name, |
| 91 | either upper-case or lower-case. |
| 92 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 93 | All arguments to commands are converted to strings, except for |
Fred Drake | d16b5ab | 1999-12-13 23:34:42 +0000 | [diff] [blame] | 94 | \samp{AUTHENTICATE}, and the last argument to \samp{APPEND} which is |
| 95 | passed as an IMAP4 literal. If necessary (the string contains IMAP4 |
| 96 | protocol-sensitive characters and isn't enclosed with either |
| 97 | parentheses or double quotes) each string is quoted. However, the |
| 98 | \var{password} argument to the \samp{LOGIN} command is always quoted. |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 99 | If you want to avoid having an argument string quoted |
| 100 | (eg: the \var{flags} argument to \samp{STORE}) then enclose the string in |
| 101 | parentheses (eg: \code{r'(\e Deleted)'}). |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 102 | |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 103 | Each command returns a tuple: \code{(\var{type}, [\var{data}, |
| 104 | ...])} where \var{type} is usually \code{'OK'} or \code{'NO'}, |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 105 | and \var{data} is either the text from the command response, or |
| 106 | mandated results from the command. |
| 107 | |
| 108 | An \class{IMAP4} instance has the following methods: |
| 109 | |
| 110 | |
| 111 | \begin{methoddesc}{append}{mailbox, flags, date_time, message} |
| 112 | Append message to named mailbox. |
| 113 | \end{methoddesc} |
| 114 | |
| 115 | \begin{methoddesc}{authenticate}{func} |
| 116 | Authenticate command --- requires response processing. This is |
| 117 | currently unimplemented, and raises an exception. |
| 118 | \end{methoddesc} |
| 119 | |
| 120 | \begin{methoddesc}{check}{} |
| 121 | Checkpoint mailbox on server. |
| 122 | \end{methoddesc} |
| 123 | |
| 124 | \begin{methoddesc}{close}{} |
| 125 | Close currently selected mailbox. Deleted messages are removed from |
| 126 | writable mailbox. This is the recommended command before |
| 127 | \samp{LOGOUT}. |
| 128 | \end{methoddesc} |
| 129 | |
| 130 | \begin{methoddesc}{copy}{message_set, new_mailbox} |
| 131 | Copy \var{message_set} messages onto end of \var{new_mailbox}. |
| 132 | \end{methoddesc} |
| 133 | |
| 134 | \begin{methoddesc}{create}{mailbox} |
| 135 | Create new mailbox named \var{mailbox}. |
| 136 | \end{methoddesc} |
| 137 | |
| 138 | \begin{methoddesc}{delete}{mailbox} |
| 139 | Delete old mailbox named \var{mailbox}. |
| 140 | \end{methoddesc} |
| 141 | |
| 142 | \begin{methoddesc}{expunge}{} |
| 143 | Permanently remove deleted items from selected mailbox. Generates an |
| 144 | \samp{EXPUNGE} response for each deleted message. Returned data |
| 145 | contains a list of \samp{EXPUNGE} message numbers in order |
| 146 | received. |
| 147 | \end{methoddesc} |
| 148 | |
| 149 | \begin{methoddesc}{fetch}{message_set, message_parts} |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 150 | Fetch (parts of) messages. \var{message_parts} should be |
| 151 | a string of message part names enclosed within parentheses, |
| 152 | eg: \samp{"(UID BODY[TEXT])"}. Returned data are tuples |
| 153 | of message part envelope and data. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 154 | \end{methoddesc} |
| 155 | |
Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 156 | \begin{methoddesc}{list}{\optional{directory\optional{, pattern}}} |
| 157 | List mailbox names in \var{directory} matching |
| 158 | \var{pattern}. \var{directory} defaults to the top-level mail |
| 159 | folder, and \var{pattern} defaults to match anything. Returned data |
| 160 | contains a list of \samp{LIST} responses. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 161 | \end{methoddesc} |
| 162 | |
| 163 | \begin{methoddesc}{login}{user, password} |
| 164 | Identify the client using a plaintext password. |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 165 | The \var{password} will be quoted. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 166 | \end{methoddesc} |
| 167 | |
| 168 | \begin{methoddesc}{logout}{} |
| 169 | Shutdown connection to server. Returns server \samp{BYE} response. |
| 170 | \end{methoddesc} |
| 171 | |
Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 172 | \begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}} |
| 173 | List subscribed mailbox names in directory matching pattern. |
| 174 | \var{directory} defaults to the top level directory and |
| 175 | \var{pattern} defaults to match any mailbox. |
| 176 | Returned data are tuples of message part envelope and data. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 177 | \end{methoddesc} |
| 178 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 179 | \begin{methoddesc}{noop}{} |
Fred Drake | d16b5ab | 1999-12-13 23:34:42 +0000 | [diff] [blame] | 180 | Send \samp{NOOP} to server. |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 181 | \end{methoddesc} |
| 182 | |
| 183 | \begin{methoddesc}{open}{host, port} |
| 184 | Opens socket to \var{port} at \var{host}. |
| 185 | You may override this method. |
| 186 | \end{methoddesc} |
| 187 | |
| 188 | \begin{methoddesc}{partial}{message_num, message_part, start, length} |
| 189 | Fetch truncated part of a message. |
| 190 | Returned data is a tuple of message part envelope and data. |
| 191 | \end{methoddesc} |
| 192 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 193 | \begin{methoddesc}{recent}{} |
| 194 | Prompt server for an update. Returned data is \code{None} if no new |
| 195 | messages, else value of \samp{RECENT} response. |
| 196 | \end{methoddesc} |
| 197 | |
| 198 | \begin{methoddesc}{rename}{oldmailbox, newmailbox} |
| 199 | Rename mailbox named \var{oldmailbox} to \var{newmailbox}. |
| 200 | \end{methoddesc} |
| 201 | |
| 202 | \begin{methoddesc}{response}{code} |
| 203 | Return data for response \var{code} if received, or |
| 204 | \code{None}. Returns the given code, instead of the usual type. |
| 205 | \end{methoddesc} |
| 206 | |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 207 | \begin{methoddesc}{search}{charset, criterium\optional{, ...}} |
| 208 | Search mailbox for matching messages. Returned data contains a space |
| 209 | separated list of matching message numbers. \var{charset} may be |
| 210 | \code{None}, in which case no \samp{CHARSET} will be specified in the |
| 211 | request to the server. The IMAP protocol requires that at least one |
| 212 | criterium be specified; an exception will be raised when the server |
| 213 | returns an error. |
| 214 | |
| 215 | Example: |
| 216 | |
| 217 | \begin{verbatim} |
| 218 | # M is a connected IMAP4 instance... |
| 219 | msgnums = M.search(None, 'FROM', '"LDJ"') |
| 220 | |
| 221 | # or: |
| 222 | msgnums = M.search(None, '(FROM "LDJ")') |
| 223 | \end{verbatim} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 224 | \end{methoddesc} |
| 225 | |
| 226 | \begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}} |
| 227 | Select a mailbox. Returned data is the count of messages in |
| 228 | \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox} |
| 229 | is \code{'INBOX'}. If the \var{readonly} flag is set, modifications |
| 230 | to the mailbox are not allowed. |
| 231 | \end{methoddesc} |
| 232 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 233 | \begin{methoddesc}{socket}{} |
| 234 | Returns socket instance used to connect to server. |
| 235 | \end{methoddesc} |
| 236 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 237 | \begin{methoddesc}{status}{mailbox, names} |
| 238 | Request named status conditions for \var{mailbox}. |
| 239 | \end{methoddesc} |
| 240 | |
| 241 | \begin{methoddesc}{store}{message_set, command, flag_list} |
| 242 | Alters flag dispositions for messages in mailbox. |
| 243 | \end{methoddesc} |
| 244 | |
| 245 | \begin{methoddesc}{subscribe}{mailbox} |
| 246 | Subscribe to new mailbox. |
| 247 | \end{methoddesc} |
| 248 | |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 249 | \begin{methoddesc}{uid}{command, arg\optional{, ...}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 250 | Execute command args with messages identified by UID, rather than |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 251 | message number. Returns response appropriate to command. At least |
| 252 | one argument must be supplied; if none are provided, the server will |
| 253 | return an error and an exception will be raised. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 254 | \end{methoddesc} |
| 255 | |
| 256 | \begin{methoddesc}{unsubscribe}{mailbox} |
| 257 | Unsubscribe from old mailbox. |
| 258 | \end{methoddesc} |
| 259 | |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 260 | \begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 261 | Allow simple extension commands notified by server in |
| 262 | \samp{CAPABILITY} response. |
| 263 | \end{methoddesc} |
| 264 | |
| 265 | |
Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 266 | The following attributes are defined on instances of \class{IMAP4}: |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 267 | |
Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 268 | |
| 269 | \begin{memberdesc}{PROTOCOL_VERSION} |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 270 | The most recent supported protocol in the |
| 271 | \samp{CAPABILITY} response from the server. |
Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 272 | \end{memberdesc} |
| 273 | |
| 274 | \begin{memberdesc}{debug} |
| 275 | Integer value to control debugging output. The initialize value is |
| 276 | taken from the module variable \code{Debug}. Values greater than |
| 277 | three trace each command. |
| 278 | \end{memberdesc} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 279 | |
| 280 | |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 281 | \subsection{IMAP4 Example \label{imap4-example}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 282 | |
| 283 | Here is a minimal example (without error checking) that opens a |
| 284 | mailbox and retrieves and prints all messages: |
| 285 | |
| 286 | \begin{verbatim} |
| 287 | import getpass, imaplib, string |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 288 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 289 | M = imaplib.IMAP4() |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 290 | M.login(getpass.getuser(), getpass.getpass()) |
| 291 | M.select() |
| 292 | typ, data = M.search(None, 'ALL') |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 293 | for num in string.split(data[0]): |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 294 | typ, data = M.fetch(num, '(RFC822)') |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 295 | print 'Message %s\n%s\n' % (num, data[0][1]) |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 296 | M.logout() |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 297 | \end{verbatim} |