blob: 38323cfc2f8ecffa3ba568c6366d90553ab68be5 [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
Fred Drakee5cf53a1998-04-11 05:02:45 +0000177\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
178 List mailbox names in \var{directory} matching
179 \var{pattern}. \var{directory} defaults to the top-level mail
180 folder, and \var{pattern} defaults to match anything. Returned data
181 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000182\end{methoddesc}
183
184\begin{methoddesc}{login}{user, password}
185 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000186 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000187\end{methoddesc}
188
189\begin{methoddesc}{logout}{}
190 Shutdown connection to server. Returns server \samp{BYE} response.
191\end{methoddesc}
192
Fred Drakee5cf53a1998-04-11 05:02:45 +0000193\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
194 List subscribed mailbox names in directory matching pattern.
195 \var{directory} defaults to the top level directory and
196 \var{pattern} defaults to match any mailbox.
197 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000198\end{methoddesc}
199
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000200\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000201 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000202\end{methoddesc}
203
204\begin{methoddesc}{open}{host, port}
205 Opens socket to \var{port} at \var{host}.
Piers Laudera3a16682001-07-20 11:04:19 +0000206 The connection objects established by this method
207 will be used in the \code{read}, \code{readline}, and \code{shutdown} methods.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000208 You may override this method.
209\end{methoddesc}
210
211\begin{methoddesc}{partial}{message_num, message_part, start, length}
212 Fetch truncated part of a message.
213 Returned data is a tuple of message part envelope and data.
214\end{methoddesc}
215
Piers Laudera3a16682001-07-20 11:04:19 +0000216\begin{methoddesc}{read}{size}
217 Reads \var{size} bytes from the remote server.
218 You may override this method.
219\end{methoddesc}
220
221\begin{methoddesc}{readline}{}
222 Reads one line from the remote server.
223 You may override this method.
224\end{methoddesc}
225
Fred Drake89de3141998-04-11 04:19:04 +0000226\begin{methoddesc}{recent}{}
227 Prompt server for an update. Returned data is \code{None} if no new
228 messages, else value of \samp{RECENT} response.
229\end{methoddesc}
230
231\begin{methoddesc}{rename}{oldmailbox, newmailbox}
232 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
233\end{methoddesc}
234
235\begin{methoddesc}{response}{code}
236 Return data for response \var{code} if received, or
237 \code{None}. Returns the given code, instead of the usual type.
238\end{methoddesc}
239
Fred Drake99d707a2000-05-26 04:08:37 +0000240\begin{methoddesc}{search}{charset, criterium\optional{, ...}}
241 Search mailbox for matching messages. Returned data contains a space
242 separated list of matching message numbers. \var{charset} may be
243 \code{None}, in which case no \samp{CHARSET} will be specified in the
244 request to the server. The IMAP protocol requires that at least one
245 criterium be specified; an exception will be raised when the server
246 returns an error.
247
248 Example:
249
250\begin{verbatim}
251# M is a connected IMAP4 instance...
252msgnums = M.search(None, 'FROM', '"LDJ"')
253
254# or:
255msgnums = M.search(None, '(FROM "LDJ")')
256\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000257\end{methoddesc}
258
259\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
260 Select a mailbox. Returned data is the count of messages in
261 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
262 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
263 to the mailbox are not allowed.
264\end{methoddesc}
265
Piers Laudera3a16682001-07-20 11:04:19 +0000266\begin{methoddesc}{setacl}{mailbox, who, what}
267 Set an \samp{ACL} for \var{mailbox}.
268 The method is non-standard, but is supported by the \samp{Cyrus} server.
269\end{methoddesc}
270
271\begin{methoddesc}{shutdown}{}
272 Close connection established in \code{open}.
273 You may override this method.
274\end{methoddesc}
275
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000276\begin{methoddesc}{socket}{}
Piers Laudera3a16682001-07-20 11:04:19 +0000277 Returns socket instance used to connect to server.
278\end{methoddesc}
279
280\begin{methoddesc}{sort}{sort_criteria, charset, search_criterium\optional{, ...}}
281 The \code{sort} command is a variant of \code{search} with sorting semantics for
282 the results. Returned data contains a space
283 separated list of matching message numbers.
284
285 Sort has two arguments before the \var{search_criterium}
286 argument(s); a parenthesized list of \var{sort_criteria}, and the searching \var{charset}.
287 Note that unlike \code{search}, the searching \var{charset} argument is mandatory.
288 There is also a \code{uid sort} command which corresponds to \code{sort} the way
289 that \code{uid search} corresponds to \code{search}.
290 The \code{sort} command first searches the mailbox for messages that
291 match the given searching criteria using the charset argument for
292 the interpretation of strings in the searching criteria. It then
293 returns the numbers of matching messages.
294
295 This is an \samp{IMAP4rev1} extension command.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000296\end{methoddesc}
297
Fred Drake89de3141998-04-11 04:19:04 +0000298\begin{methoddesc}{status}{mailbox, names}
299 Request named status conditions for \var{mailbox}.
300\end{methoddesc}
301
302\begin{methoddesc}{store}{message_set, command, flag_list}
303 Alters flag dispositions for messages in mailbox.
304\end{methoddesc}
305
306\begin{methoddesc}{subscribe}{mailbox}
307 Subscribe to new mailbox.
308\end{methoddesc}
309
Fred Drake99d707a2000-05-26 04:08:37 +0000310\begin{methoddesc}{uid}{command, arg\optional{, ...}}
Fred Drake89de3141998-04-11 04:19:04 +0000311 Execute command args with messages identified by UID, rather than
Fred Drake99d707a2000-05-26 04:08:37 +0000312 message number. Returns response appropriate to command. At least
313 one argument must be supplied; if none are provided, the server will
314 return an error and an exception will be raised.
Fred Drake89de3141998-04-11 04:19:04 +0000315\end{methoddesc}
316
317\begin{methoddesc}{unsubscribe}{mailbox}
318 Unsubscribe from old mailbox.
319\end{methoddesc}
320
Fred Drake99d707a2000-05-26 04:08:37 +0000321\begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}}
Fred Drake89de3141998-04-11 04:19:04 +0000322 Allow simple extension commands notified by server in
323 \samp{CAPABILITY} response.
324\end{methoddesc}
325
326
Piers Laudera4f83132002-03-08 01:53:24 +0000327Instances of \class{IMAP4_SSL} have just one additional method:
328
329\begin{methoddesc}{ssl}{}
330 Returns SSLObject instance used for the secure connection with the server.
331\end{methoddesc}
332
333
Fred Drake8f6b9581998-04-11 15:11:55 +0000334The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000335
Fred Drake8f6b9581998-04-11 15:11:55 +0000336
337\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000338The most recent supported protocol in the
339\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000340\end{memberdesc}
341
342\begin{memberdesc}{debug}
343Integer value to control debugging output. The initialize value is
344taken from the module variable \code{Debug}. Values greater than
345three trace each command.
346\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000347
348
Fred Drake363d67c1999-07-07 13:42:56 +0000349\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000350
351Here is a minimal example (without error checking) that opens a
352mailbox and retrieves and prints all messages:
353
354\begin{verbatim}
Piers Laudera3a16682001-07-20 11:04:19 +0000355import getpass, imaplib
Fred Drake363d67c1999-07-07 13:42:56 +0000356
Fred Drake89de3141998-04-11 04:19:04 +0000357M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000358M.login(getpass.getuser(), getpass.getpass())
359M.select()
360typ, data = M.search(None, 'ALL')
Piers Laudera3a16682001-07-20 11:04:19 +0000361for num in data[0].split():
Fred Drake363d67c1999-07-07 13:42:56 +0000362 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000363 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000364M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000365\end{verbatim}