blob: ba7667edaa0c2d798af3bcfdbb8d10a7419db3aa [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{imaplib} ---
Fred Drakeb7745501999-04-22 16:46:18 +00002 IMAP4 protocol client}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{imaplib}
Fred Drakeb7745501999-04-22 16:46:18 +00005\modulesynopsis{IMAP4 protocol client (requires sockets).}
Fred Drake295da241998-08-10 19:42:37 +00006\moduleauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au}
7\sectionauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Fred Drake38e5d272000-04-03 20:13:55 +00009% Based on HTML documentation by Piers Lauder <piers@staff.cs.usyd.edu.au>;
10% converted by Fred L. Drake, Jr. <fdrake@acm.org>.
Eric S. Raymond5ac97952001-01-11 04:19:52 +000011% Revised by ESR, January 2000.
Piers Laudera4f83132002-03-08 01:53:24 +000012% Changes for IMAP4_SSL by Tino Lange <Tino.Lange@isg.de>, March 2002
Fred Drake38e5d272000-04-03 20:13:55 +000013
Fred Drakee5cf53a1998-04-11 05:02:45 +000014\indexii{IMAP4}{protocol}
Piers Laudera4f83132002-03-08 01:53:24 +000015\indexii{IMAP4_SSL}{protocol}
Fred Drake89de3141998-04-11 04:19:04 +000016
Piers Laudera4f83132002-03-08 01:53:24 +000017This module defines two classes, \class{IMAP4} and \class{IMAP4_SSL}, which encapsulate a
18connection to an IMAP4 server and implement a large subset of the
Eric S. Raymond5ac97952001-01-11 04:19:52 +000019IMAP4rev1 client protocol as defined in \rfc{2060}. It is backward
20compatible with IMAP4 (\rfc{1730}) servers, but note that the
21\samp{STATUS} command is not supported in IMAP4.
Fred Drake89de3141998-04-11 04:19:04 +000022
Piers Laudera4f83132002-03-08 01:53:24 +000023Two classes are provided by the \module{imaplib} module, \class{IMAP4} is the base class:
Fred Drake89de3141998-04-11 04:19:04 +000024
25\begin{classdesc}{IMAP4}{\optional{host\optional{, port}}}
26This class implements the actual IMAP4 protocol. The connection is
27created and protocol version (IMAP4 or IMAP4rev1) is determined when
28the instance is initialized.
29If \var{host} is not specified, \code{''} (the local host) is used.
30If \var{port} is omitted, the standard IMAP4 port (143) is used.
31\end{classdesc}
32
33Two exceptions are defined as attributes of the \class{IMAP4} class:
34
35\begin{excdesc}{IMAP4.error}
36Exception raised on any errors. The reason for the exception is
37passed to the constructor as a string.
38\end{excdesc}
39
40\begin{excdesc}{IMAP4.abort}
41IMAP4 server errors cause this exception to be raised. This is a
42sub-class of \exception{IMAP4.error}. Note that closing the instance
43and instantiating a new one will usually allow recovery from this
44exception.
45\end{excdesc}
46
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000047\begin{excdesc}{IMAP4.readonly}
Thomas Woutersf8316632000-07-16 19:01:10 +000048This exception is raised when a writable mailbox has its status changed by the server. This is a
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000049sub-class of \exception{IMAP4.error}. Some other client now has write permission,
50and the mailbox will need to be re-opened to re-obtain write permission.
51\end{excdesc}
52
Piers Laudera4f83132002-03-08 01:53:24 +000053There's also a subclass for secure connections:
54
55\begin{classdesc}{IMAP4_SSL}{\optional{host\optional{, port\optional{, keyfile\optional{, certfile}}}}}
56This is a subclass derived from \class{IMAP4} that connects over an SSL encrypted socket
57(to use this class you need a socket module that was compiled with SSL support).
58If \var{host} is not specified, \code{''} (the local host) is used.
59If \var{port} is omitted, the standard IMAP4-over-SSL port (993) is used.
60\var{keyfile} and \var{certfile} are also optional - they can contain a PEM formatted
61private key and certificate chain file for the SSL connection.
62\end{classdesc}
63
Fred Drake89de3141998-04-11 04:19:04 +000064The following utility functions are defined:
65
66\begin{funcdesc}{Internaldate2tuple}{datestr}
67 Converts an IMAP4 INTERNALDATE string to Coordinated Universal
Fred Drakeb7745501999-04-22 16:46:18 +000068 Time. Returns a \refmodule{time} module tuple.
Fred Drake89de3141998-04-11 04:19:04 +000069\end{funcdesc}
70
71\begin{funcdesc}{Int2AP}{num}
72 Converts an integer into a string representation using characters
73 from the set [\code{A} .. \code{P}].
74\end{funcdesc}
75
76\begin{funcdesc}{ParseFlags}{flagstr}
77 Converts an IMAP4 \samp{FLAGS} response to a tuple of individual
78 flags.
79\end{funcdesc}
80
81\begin{funcdesc}{Time2Internaldate}{date_time}
Fred Drakeb7745501999-04-22 16:46:18 +000082 Converts a \refmodule{time} module tuple to an IMAP4
Fred Drake89de3141998-04-11 04:19:04 +000083 \samp{INTERNALDATE} representation. Returns a string in the form:
84 \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes).
85\end{funcdesc}
86
87
Eric S. Raymond5ac97952001-01-11 04:19:52 +000088Note that IMAP4 message numbers change as the mailbox changes; in
89particular, after an \samp{EXPUNGE} command performs deletions the
90remaining messages are renumbered. So it is highly advisable to use
91UIDs instead, with the UID command.
Fred Drake363d67c1999-07-07 13:42:56 +000092
93At the end of the module, there is a test section that contains a more
94extensive example of usage.
95
96\begin{seealso}
Fred Drake37f15741999-11-10 16:21:37 +000097 \seetext{Documents describing the protocol, and sources and binaries
98 for servers implementing it, can all be found at the
99 University of Washington's \emph{IMAP Information Center}
100 (\url{http://www.cac.washington.edu/imap/}).}
Fred Drake363d67c1999-07-07 13:42:56 +0000101\end{seealso}
102
103
104\subsection{IMAP4 Objects \label{imap4-objects}}
Fred Drake89de3141998-04-11 04:19:04 +0000105
106All IMAP4rev1 commands are represented by methods of the same name,
107either upper-case or lower-case.
108
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000109All arguments to commands are converted to strings, except for
Fred Draked16b5ab1999-12-13 23:34:42 +0000110\samp{AUTHENTICATE}, and the last argument to \samp{APPEND} which is
111passed as an IMAP4 literal. If necessary (the string contains IMAP4
112protocol-sensitive characters and isn't enclosed with either
113parentheses or double quotes) each string is quoted. However, the
114\var{password} argument to the \samp{LOGIN} command is always quoted.
Fred Drake99d707a2000-05-26 04:08:37 +0000115If you want to avoid having an argument string quoted
116(eg: the \var{flags} argument to \samp{STORE}) then enclose the string in
117parentheses (eg: \code{r'(\e Deleted)'}).
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000118
Fred Drakeb7745501999-04-22 16:46:18 +0000119Each command returns a tuple: \code{(\var{type}, [\var{data},
120...])} where \var{type} is usually \code{'OK'} or \code{'NO'},
Fred Drake89de3141998-04-11 04:19:04 +0000121and \var{data} is either the text from the command response, or
122mandated results from the command.
123
124An \class{IMAP4} instance has the following methods:
125
126
127\begin{methoddesc}{append}{mailbox, flags, date_time, message}
128 Append message to named mailbox.
129\end{methoddesc}
130
131\begin{methoddesc}{authenticate}{func}
132 Authenticate command --- requires response processing. This is
133 currently unimplemented, and raises an exception.
134\end{methoddesc}
135
136\begin{methoddesc}{check}{}
137 Checkpoint mailbox on server.
138\end{methoddesc}
139
140\begin{methoddesc}{close}{}
141 Close currently selected mailbox. Deleted messages are removed from
142 writable mailbox. This is the recommended command before
143 \samp{LOGOUT}.
144\end{methoddesc}
145
146\begin{methoddesc}{copy}{message_set, new_mailbox}
147 Copy \var{message_set} messages onto end of \var{new_mailbox}.
148\end{methoddesc}
149
150\begin{methoddesc}{create}{mailbox}
151 Create new mailbox named \var{mailbox}.
152\end{methoddesc}
153
154\begin{methoddesc}{delete}{mailbox}
155 Delete old mailbox named \var{mailbox}.
156\end{methoddesc}
157
158\begin{methoddesc}{expunge}{}
159 Permanently remove deleted items from selected mailbox. Generates an
160 \samp{EXPUNGE} response for each deleted message. Returned data
161 contains a list of \samp{EXPUNGE} message numbers in order
162 received.
163\end{methoddesc}
164
165\begin{methoddesc}{fetch}{message_set, message_parts}
Fred Drake99d707a2000-05-26 04:08:37 +0000166 Fetch (parts of) messages. \var{message_parts} should be
167 a string of message part names enclosed within parentheses,
168 eg: \samp{"(UID BODY[TEXT])"}. Returned data are tuples
169 of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000170\end{methoddesc}
171
Piers Laudera3a16682001-07-20 11:04:19 +0000172\begin{methoddesc}{getacl}{mailbox}
173 Get the \samp{ACL}s for \var{mailbox}.
174 The method is non-standard, but is supported by the \samp{Cyrus} server.
175\end{methoddesc}
176
Piers Lauder3fca2912002-06-17 07:07:20 +0000177\begin{methoddesc}{getquota}{root}
178 Get the \samp{quota} \var{root}'s resource usage and limits.
179 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000180\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000181\end{methoddesc}
182
183\begin{methoddesc}{getquotaroot}{mailbox}
184 Get the list of \samp{quota} \samp{roots} for the named \var{mailbox}.
185 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000186\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000187\end{methoddesc}
188
Fred Drakee5cf53a1998-04-11 05:02:45 +0000189\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
190 List mailbox names in \var{directory} matching
191 \var{pattern}. \var{directory} defaults to the top-level mail
192 folder, and \var{pattern} defaults to match anything. Returned data
193 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000194\end{methoddesc}
195
196\begin{methoddesc}{login}{user, password}
197 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000198 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000199\end{methoddesc}
200
201\begin{methoddesc}{logout}{}
202 Shutdown connection to server. Returns server \samp{BYE} response.
203\end{methoddesc}
204
Fred Drakee5cf53a1998-04-11 05:02:45 +0000205\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
206 List subscribed mailbox names in directory matching pattern.
207 \var{directory} defaults to the top level directory and
208 \var{pattern} defaults to match any mailbox.
209 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000210\end{methoddesc}
211
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000212\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000213 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000214\end{methoddesc}
215
216\begin{methoddesc}{open}{host, port}
217 Opens socket to \var{port} at \var{host}.
Piers Laudera3a16682001-07-20 11:04:19 +0000218 The connection objects established by this method
Piers Lauder3fca2912002-06-17 07:07:20 +0000219 will be used in the \code{read}, \code{readline}, \code{send}, and \code{shutdown} methods.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000220 You may override this method.
221\end{methoddesc}
222
223\begin{methoddesc}{partial}{message_num, message_part, start, length}
224 Fetch truncated part of a message.
225 Returned data is a tuple of message part envelope and data.
226\end{methoddesc}
227
Piers Laudera3a16682001-07-20 11:04:19 +0000228\begin{methoddesc}{read}{size}
229 Reads \var{size} bytes from the remote server.
230 You may override this method.
231\end{methoddesc}
232
233\begin{methoddesc}{readline}{}
234 Reads one line from the remote server.
235 You may override this method.
236\end{methoddesc}
237
Fred Drake89de3141998-04-11 04:19:04 +0000238\begin{methoddesc}{recent}{}
239 Prompt server for an update. Returned data is \code{None} if no new
240 messages, else value of \samp{RECENT} response.
241\end{methoddesc}
242
243\begin{methoddesc}{rename}{oldmailbox, newmailbox}
244 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
245\end{methoddesc}
246
247\begin{methoddesc}{response}{code}
248 Return data for response \var{code} if received, or
249 \code{None}. Returns the given code, instead of the usual type.
250\end{methoddesc}
251
Fred Drake99d707a2000-05-26 04:08:37 +0000252\begin{methoddesc}{search}{charset, criterium\optional{, ...}}
253 Search mailbox for matching messages. Returned data contains a space
254 separated list of matching message numbers. \var{charset} may be
255 \code{None}, in which case no \samp{CHARSET} will be specified in the
256 request to the server. The IMAP protocol requires that at least one
257 criterium be specified; an exception will be raised when the server
258 returns an error.
259
260 Example:
261
262\begin{verbatim}
263# M is a connected IMAP4 instance...
264msgnums = M.search(None, 'FROM', '"LDJ"')
265
266# or:
267msgnums = M.search(None, '(FROM "LDJ")')
268\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000269\end{methoddesc}
270
271\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
272 Select a mailbox. Returned data is the count of messages in
273 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
274 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
275 to the mailbox are not allowed.
276\end{methoddesc}
277
Piers Lauder3fca2912002-06-17 07:07:20 +0000278\begin{methoddesc}{send}{data}
279 Sends \code{data} to the remote server.
280 You may override this method.
281\end{methoddesc}
282
Piers Laudera3a16682001-07-20 11:04:19 +0000283\begin{methoddesc}{setacl}{mailbox, who, what}
284 Set an \samp{ACL} for \var{mailbox}.
285 The method is non-standard, but is supported by the \samp{Cyrus} server.
286\end{methoddesc}
287
Piers Lauder3fca2912002-06-17 07:07:20 +0000288\begin{methoddesc}{setquota}{root, limits}
289 Set the \samp{quota} \var{root}'s resource \var{limits}.
290 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000291\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000292\end{methoddesc}
293
Piers Laudera3a16682001-07-20 11:04:19 +0000294\begin{methoddesc}{shutdown}{}
295 Close connection established in \code{open}.
296 You may override this method.
297\end{methoddesc}
298
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000299\begin{methoddesc}{socket}{}
Piers Laudera3a16682001-07-20 11:04:19 +0000300 Returns socket instance used to connect to server.
301\end{methoddesc}
302
303\begin{methoddesc}{sort}{sort_criteria, charset, search_criterium\optional{, ...}}
304 The \code{sort} command is a variant of \code{search} with sorting semantics for
305 the results. Returned data contains a space
306 separated list of matching message numbers.
307
308 Sort has two arguments before the \var{search_criterium}
309 argument(s); a parenthesized list of \var{sort_criteria}, and the searching \var{charset}.
310 Note that unlike \code{search}, the searching \var{charset} argument is mandatory.
311 There is also a \code{uid sort} command which corresponds to \code{sort} the way
312 that \code{uid search} corresponds to \code{search}.
313 The \code{sort} command first searches the mailbox for messages that
314 match the given searching criteria using the charset argument for
315 the interpretation of strings in the searching criteria. It then
316 returns the numbers of matching messages.
317
318 This is an \samp{IMAP4rev1} extension command.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000319\end{methoddesc}
320
Fred Drake89de3141998-04-11 04:19:04 +0000321\begin{methoddesc}{status}{mailbox, names}
322 Request named status conditions for \var{mailbox}.
323\end{methoddesc}
324
325\begin{methoddesc}{store}{message_set, command, flag_list}
326 Alters flag dispositions for messages in mailbox.
327\end{methoddesc}
328
329\begin{methoddesc}{subscribe}{mailbox}
330 Subscribe to new mailbox.
331\end{methoddesc}
332
Fred Drake99d707a2000-05-26 04:08:37 +0000333\begin{methoddesc}{uid}{command, arg\optional{, ...}}
Fred Drake89de3141998-04-11 04:19:04 +0000334 Execute command args with messages identified by UID, rather than
Fred Drake99d707a2000-05-26 04:08:37 +0000335 message number. Returns response appropriate to command. At least
336 one argument must be supplied; if none are provided, the server will
337 return an error and an exception will be raised.
Fred Drake89de3141998-04-11 04:19:04 +0000338\end{methoddesc}
339
340\begin{methoddesc}{unsubscribe}{mailbox}
341 Unsubscribe from old mailbox.
342\end{methoddesc}
343
Fred Drake99d707a2000-05-26 04:08:37 +0000344\begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}}
Fred Drake89de3141998-04-11 04:19:04 +0000345 Allow simple extension commands notified by server in
346 \samp{CAPABILITY} response.
347\end{methoddesc}
348
349
Piers Laudera4f83132002-03-08 01:53:24 +0000350Instances of \class{IMAP4_SSL} have just one additional method:
351
352\begin{methoddesc}{ssl}{}
353 Returns SSLObject instance used for the secure connection with the server.
354\end{methoddesc}
355
356
Fred Drake8f6b9581998-04-11 15:11:55 +0000357The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000358
Fred Drake8f6b9581998-04-11 15:11:55 +0000359
360\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000361The most recent supported protocol in the
362\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000363\end{memberdesc}
364
365\begin{memberdesc}{debug}
366Integer value to control debugging output. The initialize value is
367taken from the module variable \code{Debug}. Values greater than
368three trace each command.
369\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000370
371
Fred Drake363d67c1999-07-07 13:42:56 +0000372\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000373
374Here is a minimal example (without error checking) that opens a
375mailbox and retrieves and prints all messages:
376
377\begin{verbatim}
Piers Laudera3a16682001-07-20 11:04:19 +0000378import getpass, imaplib
Fred Drake363d67c1999-07-07 13:42:56 +0000379
Fred Drake89de3141998-04-11 04:19:04 +0000380M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000381M.login(getpass.getuser(), getpass.getpass())
382M.select()
383typ, data = M.search(None, 'ALL')
Piers Laudera3a16682001-07-20 11:04:19 +0000384for num in data[0].split():
Fred Drake363d67c1999-07-07 13:42:56 +0000385 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000386 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000387M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000388\end{verbatim}