blob: 1edbff7ab939deeff9afbc9719c8f00c473f1eb2 [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).}
Piers Lauderd3c821e2002-11-22 05:47:39 +00006\moduleauthor{Piers Lauder}{piers@communitysolutions.com.au}
7\sectionauthor{Piers Lauder}{piers@communitysolutions.com.au}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Piers Lauderd3c821e2002-11-22 05:47:39 +00009% Based on HTML documentation by Piers Lauder <piers@communitysolutions.com.au>;
Fred Drake38e5d272000-04-03 20:13:55 +000010% 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
Piers Lauderd3c821e2002-11-22 05:47:39 +000013% Changes for IMAP4_stream by Piers Lauder <piers@communitysolutions.com.au>, November 2002
Fred Drake38e5d272000-04-03 20:13:55 +000014
Fred Drakee5cf53a1998-04-11 05:02:45 +000015\indexii{IMAP4}{protocol}
Piers Laudera4f83132002-03-08 01:53:24 +000016\indexii{IMAP4_SSL}{protocol}
Piers Lauderd3c821e2002-11-22 05:47:39 +000017\indexii{IMAP4_stream}{protocol}
Fred Drake89de3141998-04-11 04:19:04 +000018
Piers Lauderd3c821e2002-11-22 05:47:39 +000019This module defines three classes, \class{IMAP4}, \class{IMAP4_SSL} and \class{IMAP4_stream}, which encapsulate a
Piers Laudera4f83132002-03-08 01:53:24 +000020connection to an IMAP4 server and implement a large subset of the
Eric S. Raymond5ac97952001-01-11 04:19:52 +000021IMAP4rev1 client protocol as defined in \rfc{2060}. It is backward
22compatible with IMAP4 (\rfc{1730}) servers, but note that the
23\samp{STATUS} command is not supported in IMAP4.
Fred Drake89de3141998-04-11 04:19:04 +000024
Piers Lauderd3c821e2002-11-22 05:47:39 +000025Three classes are provided by the \module{imaplib} module, \class{IMAP4} is the base class:
Fred Drake89de3141998-04-11 04:19:04 +000026
27\begin{classdesc}{IMAP4}{\optional{host\optional{, port}}}
28This class implements the actual IMAP4 protocol. The connection is
29created and protocol version (IMAP4 or IMAP4rev1) is determined when
30the instance is initialized.
31If \var{host} is not specified, \code{''} (the local host) is used.
32If \var{port} is omitted, the standard IMAP4 port (143) is used.
33\end{classdesc}
34
Piers Lauderd3c821e2002-11-22 05:47:39 +000035Three exceptions are defined as attributes of the \class{IMAP4} class:
Fred Drake89de3141998-04-11 04:19:04 +000036
37\begin{excdesc}{IMAP4.error}
38Exception raised on any errors. The reason for the exception is
39passed to the constructor as a string.
40\end{excdesc}
41
42\begin{excdesc}{IMAP4.abort}
43IMAP4 server errors cause this exception to be raised. This is a
44sub-class of \exception{IMAP4.error}. Note that closing the instance
45and instantiating a new one will usually allow recovery from this
46exception.
47\end{excdesc}
48
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000049\begin{excdesc}{IMAP4.readonly}
Thomas Woutersf8316632000-07-16 19:01:10 +000050This 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 +000051sub-class of \exception{IMAP4.error}. Some other client now has write permission,
52and the mailbox will need to be re-opened to re-obtain write permission.
53\end{excdesc}
54
Piers Laudera4f83132002-03-08 01:53:24 +000055There's also a subclass for secure connections:
56
57\begin{classdesc}{IMAP4_SSL}{\optional{host\optional{, port\optional{, keyfile\optional{, certfile}}}}}
58This is a subclass derived from \class{IMAP4} that connects over an SSL encrypted socket
59(to use this class you need a socket module that was compiled with SSL support).
60If \var{host} is not specified, \code{''} (the local host) is used.
61If \var{port} is omitted, the standard IMAP4-over-SSL port (993) is used.
62\var{keyfile} and \var{certfile} are also optional - they can contain a PEM formatted
63private key and certificate chain file for the SSL connection.
64\end{classdesc}
65
Piers Lauderd3c821e2002-11-22 05:47:39 +000066The second subclass allows for connections created by a child process:
67
68\begin{classdesc}{IMAP4_stream}{command}
69This is a subclass derived from \class{IMAP4} that connects
70to the \code{stdin/stdout} file descriptors created by passing \var{command} to \code{os.popen2()}.
71\end{classdesc}
72
Fred Drake89de3141998-04-11 04:19:04 +000073The following utility functions are defined:
74
75\begin{funcdesc}{Internaldate2tuple}{datestr}
76 Converts an IMAP4 INTERNALDATE string to Coordinated Universal
Fred Drakeb7745501999-04-22 16:46:18 +000077 Time. Returns a \refmodule{time} module tuple.
Fred Drake89de3141998-04-11 04:19:04 +000078\end{funcdesc}
79
80\begin{funcdesc}{Int2AP}{num}
81 Converts an integer into a string representation using characters
82 from the set [\code{A} .. \code{P}].
83\end{funcdesc}
84
85\begin{funcdesc}{ParseFlags}{flagstr}
86 Converts an IMAP4 \samp{FLAGS} response to a tuple of individual
87 flags.
88\end{funcdesc}
89
90\begin{funcdesc}{Time2Internaldate}{date_time}
Fred Drakeb7745501999-04-22 16:46:18 +000091 Converts a \refmodule{time} module tuple to an IMAP4
Fred Drake89de3141998-04-11 04:19:04 +000092 \samp{INTERNALDATE} representation. Returns a string in the form:
93 \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes).
94\end{funcdesc}
95
96
Eric S. Raymond5ac97952001-01-11 04:19:52 +000097Note that IMAP4 message numbers change as the mailbox changes; in
98particular, after an \samp{EXPUNGE} command performs deletions the
99remaining messages are renumbered. So it is highly advisable to use
100UIDs instead, with the UID command.
Fred Drake363d67c1999-07-07 13:42:56 +0000101
102At the end of the module, there is a test section that contains a more
103extensive example of usage.
104
105\begin{seealso}
Fred Drake37f15741999-11-10 16:21:37 +0000106 \seetext{Documents describing the protocol, and sources and binaries
107 for servers implementing it, can all be found at the
108 University of Washington's \emph{IMAP Information Center}
109 (\url{http://www.cac.washington.edu/imap/}).}
Fred Drake363d67c1999-07-07 13:42:56 +0000110\end{seealso}
111
112
113\subsection{IMAP4 Objects \label{imap4-objects}}
Fred Drake89de3141998-04-11 04:19:04 +0000114
115All IMAP4rev1 commands are represented by methods of the same name,
116either upper-case or lower-case.
117
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000118All arguments to commands are converted to strings, except for
Fred Draked16b5ab1999-12-13 23:34:42 +0000119\samp{AUTHENTICATE}, and the last argument to \samp{APPEND} which is
120passed as an IMAP4 literal. If necessary (the string contains IMAP4
121protocol-sensitive characters and isn't enclosed with either
122parentheses or double quotes) each string is quoted. However, the
123\var{password} argument to the \samp{LOGIN} command is always quoted.
Fred Drake99d707a2000-05-26 04:08:37 +0000124If you want to avoid having an argument string quoted
125(eg: the \var{flags} argument to \samp{STORE}) then enclose the string in
126parentheses (eg: \code{r'(\e Deleted)'}).
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000127
Fred Drakeb7745501999-04-22 16:46:18 +0000128Each command returns a tuple: \code{(\var{type}, [\var{data},
129...])} where \var{type} is usually \code{'OK'} or \code{'NO'},
Fred Drake89de3141998-04-11 04:19:04 +0000130and \var{data} is either the text from the command response, or
Piers Lauderd3c821e2002-11-22 05:47:39 +0000131mandated results from the command. Each \var{data}
132is either a string, or a tuple. If a tuple, then the first part
133is the header of the response, and the second part contains
134the data (ie: 'literal' value).
Fred Drake89de3141998-04-11 04:19:04 +0000135
136An \class{IMAP4} instance has the following methods:
137
138
139\begin{methoddesc}{append}{mailbox, flags, date_time, message}
140 Append message to named mailbox.
141\end{methoddesc}
142
143\begin{methoddesc}{authenticate}{func}
144 Authenticate command --- requires response processing. This is
145 currently unimplemented, and raises an exception.
146\end{methoddesc}
147
148\begin{methoddesc}{check}{}
149 Checkpoint mailbox on server.
150\end{methoddesc}
151
152\begin{methoddesc}{close}{}
153 Close currently selected mailbox. Deleted messages are removed from
154 writable mailbox. This is the recommended command before
155 \samp{LOGOUT}.
156\end{methoddesc}
157
158\begin{methoddesc}{copy}{message_set, new_mailbox}
159 Copy \var{message_set} messages onto end of \var{new_mailbox}.
160\end{methoddesc}
161
162\begin{methoddesc}{create}{mailbox}
163 Create new mailbox named \var{mailbox}.
164\end{methoddesc}
165
166\begin{methoddesc}{delete}{mailbox}
167 Delete old mailbox named \var{mailbox}.
168\end{methoddesc}
169
170\begin{methoddesc}{expunge}{}
171 Permanently remove deleted items from selected mailbox. Generates an
172 \samp{EXPUNGE} response for each deleted message. Returned data
173 contains a list of \samp{EXPUNGE} message numbers in order
174 received.
175\end{methoddesc}
176
177\begin{methoddesc}{fetch}{message_set, message_parts}
Fred Drake99d707a2000-05-26 04:08:37 +0000178 Fetch (parts of) messages. \var{message_parts} should be
179 a string of message part names enclosed within parentheses,
180 eg: \samp{"(UID BODY[TEXT])"}. Returned data are tuples
181 of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000182\end{methoddesc}
183
Piers Laudera3a16682001-07-20 11:04:19 +0000184\begin{methoddesc}{getacl}{mailbox}
185 Get the \samp{ACL}s for \var{mailbox}.
186 The method is non-standard, but is supported by the \samp{Cyrus} server.
187\end{methoddesc}
188
Piers Lauder3fca2912002-06-17 07:07:20 +0000189\begin{methoddesc}{getquota}{root}
190 Get the \samp{quota} \var{root}'s resource usage and limits.
191 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000192\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000193\end{methoddesc}
194
195\begin{methoddesc}{getquotaroot}{mailbox}
196 Get the list of \samp{quota} \samp{roots} for the named \var{mailbox}.
197 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000198\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000199\end{methoddesc}
200
Fred Drakee5cf53a1998-04-11 05:02:45 +0000201\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
202 List mailbox names in \var{directory} matching
203 \var{pattern}. \var{directory} defaults to the top-level mail
204 folder, and \var{pattern} defaults to match anything. Returned data
205 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000206\end{methoddesc}
207
208\begin{methoddesc}{login}{user, password}
209 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000210 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000211\end{methoddesc}
212
Piers Lauderd3c821e2002-11-22 05:47:39 +0000213\begin{methoddesc}{login_cram_md5}{user, password}
214 Force use of \samp{CRAM-MD5} authentication when identifying the client to protect the password.
215 Will only work if the server \samp{CAPABILITY} response includes the phrase \samp{AUTH=CRAM-MD5}.
216\end{methoddesc}
217
Fred Drake89de3141998-04-11 04:19:04 +0000218\begin{methoddesc}{logout}{}
219 Shutdown connection to server. Returns server \samp{BYE} response.
220\end{methoddesc}
221
Fred Drakee5cf53a1998-04-11 05:02:45 +0000222\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
223 List subscribed mailbox names in directory matching pattern.
224 \var{directory} defaults to the top level directory and
225 \var{pattern} defaults to match any mailbox.
226 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000227\end{methoddesc}
228
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000229\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000230 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000231\end{methoddesc}
232
233\begin{methoddesc}{open}{host, port}
234 Opens socket to \var{port} at \var{host}.
Piers Laudera3a16682001-07-20 11:04:19 +0000235 The connection objects established by this method
Piers Lauder3fca2912002-06-17 07:07:20 +0000236 will be used in the \code{read}, \code{readline}, \code{send}, and \code{shutdown} methods.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000237 You may override this method.
238\end{methoddesc}
239
240\begin{methoddesc}{partial}{message_num, message_part, start, length}
241 Fetch truncated part of a message.
242 Returned data is a tuple of message part envelope and data.
243\end{methoddesc}
244
Piers Lauderd3c821e2002-11-22 05:47:39 +0000245\begin{methoddesc}{proxyauth}{user}
246 Assume authentication as \var{user}.
247 Allows an authorised administrator to proxy into any user's mailbox.
248\end{methoddesc}
249
Piers Laudera3a16682001-07-20 11:04:19 +0000250\begin{methoddesc}{read}{size}
251 Reads \var{size} bytes from the remote server.
252 You may override this method.
253\end{methoddesc}
254
255\begin{methoddesc}{readline}{}
256 Reads one line from the remote server.
257 You may override this method.
258\end{methoddesc}
259
Fred Drake89de3141998-04-11 04:19:04 +0000260\begin{methoddesc}{recent}{}
261 Prompt server for an update. Returned data is \code{None} if no new
262 messages, else value of \samp{RECENT} response.
263\end{methoddesc}
264
265\begin{methoddesc}{rename}{oldmailbox, newmailbox}
266 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
267\end{methoddesc}
268
269\begin{methoddesc}{response}{code}
270 Return data for response \var{code} if received, or
271 \code{None}. Returns the given code, instead of the usual type.
272\end{methoddesc}
273
Fred Drake99d707a2000-05-26 04:08:37 +0000274\begin{methoddesc}{search}{charset, criterium\optional{, ...}}
275 Search mailbox for matching messages. Returned data contains a space
276 separated list of matching message numbers. \var{charset} may be
277 \code{None}, in which case no \samp{CHARSET} will be specified in the
278 request to the server. The IMAP protocol requires that at least one
279 criterium be specified; an exception will be raised when the server
280 returns an error.
281
282 Example:
283
284\begin{verbatim}
285# M is a connected IMAP4 instance...
286msgnums = M.search(None, 'FROM', '"LDJ"')
287
288# or:
289msgnums = M.search(None, '(FROM "LDJ")')
290\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000291\end{methoddesc}
292
293\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
294 Select a mailbox. Returned data is the count of messages in
295 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
296 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
297 to the mailbox are not allowed.
298\end{methoddesc}
299
Piers Lauder3fca2912002-06-17 07:07:20 +0000300\begin{methoddesc}{send}{data}
301 Sends \code{data} to the remote server.
302 You may override this method.
303\end{methoddesc}
304
Piers Laudera3a16682001-07-20 11:04:19 +0000305\begin{methoddesc}{setacl}{mailbox, who, what}
306 Set an \samp{ACL} for \var{mailbox}.
307 The method is non-standard, but is supported by the \samp{Cyrus} server.
308\end{methoddesc}
309
Piers Lauder3fca2912002-06-17 07:07:20 +0000310\begin{methoddesc}{setquota}{root, limits}
311 Set the \samp{quota} \var{root}'s resource \var{limits}.
312 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000313\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000314\end{methoddesc}
315
Piers Laudera3a16682001-07-20 11:04:19 +0000316\begin{methoddesc}{shutdown}{}
317 Close connection established in \code{open}.
318 You may override this method.
319\end{methoddesc}
320
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000321\begin{methoddesc}{socket}{}
Piers Laudera3a16682001-07-20 11:04:19 +0000322 Returns socket instance used to connect to server.
323\end{methoddesc}
324
325\begin{methoddesc}{sort}{sort_criteria, charset, search_criterium\optional{, ...}}
326 The \code{sort} command is a variant of \code{search} with sorting semantics for
327 the results. Returned data contains a space
328 separated list of matching message numbers.
329
330 Sort has two arguments before the \var{search_criterium}
331 argument(s); a parenthesized list of \var{sort_criteria}, and the searching \var{charset}.
332 Note that unlike \code{search}, the searching \var{charset} argument is mandatory.
333 There is also a \code{uid sort} command which corresponds to \code{sort} the way
334 that \code{uid search} corresponds to \code{search}.
335 The \code{sort} command first searches the mailbox for messages that
336 match the given searching criteria using the charset argument for
337 the interpretation of strings in the searching criteria. It then
338 returns the numbers of matching messages.
339
340 This is an \samp{IMAP4rev1} extension command.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000341\end{methoddesc}
342
Fred Drake89de3141998-04-11 04:19:04 +0000343\begin{methoddesc}{status}{mailbox, names}
344 Request named status conditions for \var{mailbox}.
345\end{methoddesc}
346
347\begin{methoddesc}{store}{message_set, command, flag_list}
348 Alters flag dispositions for messages in mailbox.
349\end{methoddesc}
350
351\begin{methoddesc}{subscribe}{mailbox}
352 Subscribe to new mailbox.
353\end{methoddesc}
354
Fred Drake99d707a2000-05-26 04:08:37 +0000355\begin{methoddesc}{uid}{command, arg\optional{, ...}}
Fred Drake89de3141998-04-11 04:19:04 +0000356 Execute command args with messages identified by UID, rather than
Fred Drake99d707a2000-05-26 04:08:37 +0000357 message number. Returns response appropriate to command. At least
358 one argument must be supplied; if none are provided, the server will
359 return an error and an exception will be raised.
Fred Drake89de3141998-04-11 04:19:04 +0000360\end{methoddesc}
361
362\begin{methoddesc}{unsubscribe}{mailbox}
363 Unsubscribe from old mailbox.
364\end{methoddesc}
365
Fred Drake99d707a2000-05-26 04:08:37 +0000366\begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}}
Fred Drake89de3141998-04-11 04:19:04 +0000367 Allow simple extension commands notified by server in
368 \samp{CAPABILITY} response.
369\end{methoddesc}
370
371
Piers Laudera4f83132002-03-08 01:53:24 +0000372Instances of \class{IMAP4_SSL} have just one additional method:
373
374\begin{methoddesc}{ssl}{}
375 Returns SSLObject instance used for the secure connection with the server.
376\end{methoddesc}
377
378
Fred Drake8f6b9581998-04-11 15:11:55 +0000379The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000380
Fred Drake8f6b9581998-04-11 15:11:55 +0000381
382\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000383The most recent supported protocol in the
384\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000385\end{memberdesc}
386
387\begin{memberdesc}{debug}
388Integer value to control debugging output. The initialize value is
389taken from the module variable \code{Debug}. Values greater than
390three trace each command.
391\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000392
393
Fred Drake363d67c1999-07-07 13:42:56 +0000394\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000395
396Here is a minimal example (without error checking) that opens a
397mailbox and retrieves and prints all messages:
398
399\begin{verbatim}
Piers Laudera3a16682001-07-20 11:04:19 +0000400import getpass, imaplib
Fred Drake363d67c1999-07-07 13:42:56 +0000401
Fred Drake89de3141998-04-11 04:19:04 +0000402M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000403M.login(getpass.getuser(), getpass.getpass())
404M.select()
405typ, data = M.search(None, 'ALL')
Piers Laudera3a16682001-07-20 11:04:19 +0000406for num in data[0].split():
Fred Drake363d67c1999-07-07 13:42:56 +0000407 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000408 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000409M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000410\end{verbatim}