blob: b710ce774fc7e5d5f63864fc81e9e732576d648c [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.
180\end{methoddesc}
181
182\begin{methoddesc}{getquotaroot}{mailbox}
183 Get the list of \samp{quota} \samp{roots} for the named \var{mailbox}.
184 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
185\end{methoddesc}
186
Fred Drakee5cf53a1998-04-11 05:02:45 +0000187\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
188 List mailbox names in \var{directory} matching
189 \var{pattern}. \var{directory} defaults to the top-level mail
190 folder, and \var{pattern} defaults to match anything. Returned data
191 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000192\end{methoddesc}
193
194\begin{methoddesc}{login}{user, password}
195 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000196 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000197\end{methoddesc}
198
199\begin{methoddesc}{logout}{}
200 Shutdown connection to server. Returns server \samp{BYE} response.
201\end{methoddesc}
202
Fred Drakee5cf53a1998-04-11 05:02:45 +0000203\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
204 List subscribed mailbox names in directory matching pattern.
205 \var{directory} defaults to the top level directory and
206 \var{pattern} defaults to match any mailbox.
207 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000208\end{methoddesc}
209
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000210\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000211 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000212\end{methoddesc}
213
214\begin{methoddesc}{open}{host, port}
215 Opens socket to \var{port} at \var{host}.
Piers Laudera3a16682001-07-20 11:04:19 +0000216 The connection objects established by this method
Piers Lauder3fca2912002-06-17 07:07:20 +0000217 will be used in the \code{read}, \code{readline}, \code{send}, and \code{shutdown} methods.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000218 You may override this method.
219\end{methoddesc}
220
221\begin{methoddesc}{partial}{message_num, message_part, start, length}
222 Fetch truncated part of a message.
223 Returned data is a tuple of message part envelope and data.
224\end{methoddesc}
225
Piers Laudera3a16682001-07-20 11:04:19 +0000226\begin{methoddesc}{read}{size}
227 Reads \var{size} bytes from the remote server.
228 You may override this method.
229\end{methoddesc}
230
231\begin{methoddesc}{readline}{}
232 Reads one line from the remote server.
233 You may override this method.
234\end{methoddesc}
235
Fred Drake89de3141998-04-11 04:19:04 +0000236\begin{methoddesc}{recent}{}
237 Prompt server for an update. Returned data is \code{None} if no new
238 messages, else value of \samp{RECENT} response.
239\end{methoddesc}
240
241\begin{methoddesc}{rename}{oldmailbox, newmailbox}
242 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
243\end{methoddesc}
244
245\begin{methoddesc}{response}{code}
246 Return data for response \var{code} if received, or
247 \code{None}. Returns the given code, instead of the usual type.
248\end{methoddesc}
249
Fred Drake99d707a2000-05-26 04:08:37 +0000250\begin{methoddesc}{search}{charset, criterium\optional{, ...}}
251 Search mailbox for matching messages. Returned data contains a space
252 separated list of matching message numbers. \var{charset} may be
253 \code{None}, in which case no \samp{CHARSET} will be specified in the
254 request to the server. The IMAP protocol requires that at least one
255 criterium be specified; an exception will be raised when the server
256 returns an error.
257
258 Example:
259
260\begin{verbatim}
261# M is a connected IMAP4 instance...
262msgnums = M.search(None, 'FROM', '"LDJ"')
263
264# or:
265msgnums = M.search(None, '(FROM "LDJ")')
266\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000267\end{methoddesc}
268
269\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
270 Select a mailbox. Returned data is the count of messages in
271 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
272 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
273 to the mailbox are not allowed.
274\end{methoddesc}
275
Piers Lauder3fca2912002-06-17 07:07:20 +0000276\begin{methoddesc}{send}{data}
277 Sends \code{data} to the remote server.
278 You may override this method.
279\end{methoddesc}
280
Piers Laudera3a16682001-07-20 11:04:19 +0000281\begin{methoddesc}{setacl}{mailbox, who, what}
282 Set an \samp{ACL} for \var{mailbox}.
283 The method is non-standard, but is supported by the \samp{Cyrus} server.
284\end{methoddesc}
285
Piers Lauder3fca2912002-06-17 07:07:20 +0000286\begin{methoddesc}{setquota}{root, limits}
287 Set the \samp{quota} \var{root}'s resource \var{limits}.
288 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
289\end{methoddesc}
290
Piers Laudera3a16682001-07-20 11:04:19 +0000291\begin{methoddesc}{shutdown}{}
292 Close connection established in \code{open}.
293 You may override this method.
294\end{methoddesc}
295
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000296\begin{methoddesc}{socket}{}
Piers Laudera3a16682001-07-20 11:04:19 +0000297 Returns socket instance used to connect to server.
298\end{methoddesc}
299
300\begin{methoddesc}{sort}{sort_criteria, charset, search_criterium\optional{, ...}}
301 The \code{sort} command is a variant of \code{search} with sorting semantics for
302 the results. Returned data contains a space
303 separated list of matching message numbers.
304
305 Sort has two arguments before the \var{search_criterium}
306 argument(s); a parenthesized list of \var{sort_criteria}, and the searching \var{charset}.
307 Note that unlike \code{search}, the searching \var{charset} argument is mandatory.
308 There is also a \code{uid sort} command which corresponds to \code{sort} the way
309 that \code{uid search} corresponds to \code{search}.
310 The \code{sort} command first searches the mailbox for messages that
311 match the given searching criteria using the charset argument for
312 the interpretation of strings in the searching criteria. It then
313 returns the numbers of matching messages.
314
315 This is an \samp{IMAP4rev1} extension command.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000316\end{methoddesc}
317
Fred Drake89de3141998-04-11 04:19:04 +0000318\begin{methoddesc}{status}{mailbox, names}
319 Request named status conditions for \var{mailbox}.
320\end{methoddesc}
321
322\begin{methoddesc}{store}{message_set, command, flag_list}
323 Alters flag dispositions for messages in mailbox.
324\end{methoddesc}
325
326\begin{methoddesc}{subscribe}{mailbox}
327 Subscribe to new mailbox.
328\end{methoddesc}
329
Fred Drake99d707a2000-05-26 04:08:37 +0000330\begin{methoddesc}{uid}{command, arg\optional{, ...}}
Fred Drake89de3141998-04-11 04:19:04 +0000331 Execute command args with messages identified by UID, rather than
Fred Drake99d707a2000-05-26 04:08:37 +0000332 message number. Returns response appropriate to command. At least
333 one argument must be supplied; if none are provided, the server will
334 return an error and an exception will be raised.
Fred Drake89de3141998-04-11 04:19:04 +0000335\end{methoddesc}
336
337\begin{methoddesc}{unsubscribe}{mailbox}
338 Unsubscribe from old mailbox.
339\end{methoddesc}
340
Fred Drake99d707a2000-05-26 04:08:37 +0000341\begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}}
Fred Drake89de3141998-04-11 04:19:04 +0000342 Allow simple extension commands notified by server in
343 \samp{CAPABILITY} response.
344\end{methoddesc}
345
346
Piers Laudera4f83132002-03-08 01:53:24 +0000347Instances of \class{IMAP4_SSL} have just one additional method:
348
349\begin{methoddesc}{ssl}{}
350 Returns SSLObject instance used for the secure connection with the server.
351\end{methoddesc}
352
353
Fred Drake8f6b9581998-04-11 15:11:55 +0000354The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000355
Fred Drake8f6b9581998-04-11 15:11:55 +0000356
357\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000358The most recent supported protocol in the
359\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000360\end{memberdesc}
361
362\begin{memberdesc}{debug}
363Integer value to control debugging output. The initialize value is
364taken from the module variable \code{Debug}. Values greater than
365three trace each command.
366\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000367
368
Fred Drake363d67c1999-07-07 13:42:56 +0000369\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000370
371Here is a minimal example (without error checking) that opens a
372mailbox and retrieves and prints all messages:
373
374\begin{verbatim}
Piers Laudera3a16682001-07-20 11:04:19 +0000375import getpass, imaplib
Fred Drake363d67c1999-07-07 13:42:56 +0000376
Fred Drake89de3141998-04-11 04:19:04 +0000377M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000378M.login(getpass.getuser(), getpass.getpass())
379M.select()
380typ, data = M.search(None, 'ALL')
Piers Laudera3a16682001-07-20 11:04:19 +0000381for num in data[0].split():
Fred Drake363d67c1999-07-07 13:42:56 +0000382 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000383 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000384M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000385\end{verbatim}