blob: 3448ba6925d1b0cbd334cd3f38a97ead3b247f7e [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
Fred Drake63a5d0b2004-07-30 19:12:38 +00009% Based on HTML documentation by Piers Lauder
10% <piers@communitysolutions.com.au>;
Fred Drake38e5d272000-04-03 20:13:55 +000011% converted by Fred L. Drake, Jr. <fdrake@acm.org>.
Eric S. Raymond5ac97952001-01-11 04:19:52 +000012% Revised by ESR, January 2000.
Piers Laudera4f83132002-03-08 01:53:24 +000013% Changes for IMAP4_SSL by Tino Lange <Tino.Lange@isg.de>, March 2002
Fred Drake63a5d0b2004-07-30 19:12:38 +000014% Changes for IMAP4_stream by Piers Lauder
15% <piers@communitysolutions.com.au>, November 2002
Fred Drake38e5d272000-04-03 20:13:55 +000016
Fred Drakee5cf53a1998-04-11 05:02:45 +000017\indexii{IMAP4}{protocol}
Piers Laudera4f83132002-03-08 01:53:24 +000018\indexii{IMAP4_SSL}{protocol}
Piers Lauderd3c821e2002-11-22 05:47:39 +000019\indexii{IMAP4_stream}{protocol}
Fred Drake89de3141998-04-11 04:19:04 +000020
Fred Drake63a5d0b2004-07-30 19:12:38 +000021This module defines three classes, \class{IMAP4}, \class{IMAP4_SSL}
22and \class{IMAP4_stream}, which encapsulate a
Piers Laudera4f83132002-03-08 01:53:24 +000023connection to an IMAP4 server and implement a large subset of the
Eric S. Raymond5ac97952001-01-11 04:19:52 +000024IMAP4rev1 client protocol as defined in \rfc{2060}. It is backward
25compatible with IMAP4 (\rfc{1730}) servers, but note that the
26\samp{STATUS} command is not supported in IMAP4.
Fred Drake89de3141998-04-11 04:19:04 +000027
Fred Drake63a5d0b2004-07-30 19:12:38 +000028Three classes are provided by the \module{imaplib} module,
29\class{IMAP4} is the base class:
Fred Drake89de3141998-04-11 04:19:04 +000030
31\begin{classdesc}{IMAP4}{\optional{host\optional{, port}}}
32This class implements the actual IMAP4 protocol. The connection is
33created and protocol version (IMAP4 or IMAP4rev1) is determined when
34the instance is initialized.
35If \var{host} is not specified, \code{''} (the local host) is used.
36If \var{port} is omitted, the standard IMAP4 port (143) is used.
37\end{classdesc}
38
Piers Lauderd3c821e2002-11-22 05:47:39 +000039Three exceptions are defined as attributes of the \class{IMAP4} class:
Fred Drake89de3141998-04-11 04:19:04 +000040
41\begin{excdesc}{IMAP4.error}
42Exception raised on any errors. The reason for the exception is
43passed to the constructor as a string.
44\end{excdesc}
45
46\begin{excdesc}{IMAP4.abort}
47IMAP4 server errors cause this exception to be raised. This is a
48sub-class of \exception{IMAP4.error}. Note that closing the instance
49and instantiating a new one will usually allow recovery from this
50exception.
51\end{excdesc}
52
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000053\begin{excdesc}{IMAP4.readonly}
Fred Drake63a5d0b2004-07-30 19:12:38 +000054This exception is raised when a writable mailbox has its status
55changed by the server. This is a sub-class of
56\exception{IMAP4.error}. Some other client now has write permission,
57and the mailbox will need to be re-opened to re-obtain write
58permission.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000059\end{excdesc}
60
Piers Laudera4f83132002-03-08 01:53:24 +000061There's also a subclass for secure connections:
62
Fred Drake63a5d0b2004-07-30 19:12:38 +000063\begin{classdesc}{IMAP4_SSL}{\optional{host\optional{, port\optional{,
64 keyfile\optional{, certfile}}}}}
65This is a subclass derived from \class{IMAP4} that connects over an
66SSL encrypted socket (to use this class you need a socket module that
67was compiled with SSL support). If \var{host} is not specified,
68\code{''} (the local host) is used. If \var{port} is omitted, the
69standard IMAP4-over-SSL port (993) is used. \var{keyfile} and
70\var{certfile} are also optional - they can contain a PEM formatted
71private key and certificate chain file for the SSL connection.
Piers Laudera4f83132002-03-08 01:53:24 +000072\end{classdesc}
73
Piers Lauderd3c821e2002-11-22 05:47:39 +000074The second subclass allows for connections created by a child process:
75
76\begin{classdesc}{IMAP4_stream}{command}
77This is a subclass derived from \class{IMAP4} that connects
Fred Drake63a5d0b2004-07-30 19:12:38 +000078to the \code{stdin/stdout} file descriptors created by passing
79\var{command} to \code{os.popen2()}.
Neal Norwitze1497982003-01-02 15:32:00 +000080\versionadded{2.3}
Piers Lauderd3c821e2002-11-22 05:47:39 +000081\end{classdesc}
82
Fred Drake89de3141998-04-11 04:19:04 +000083The following utility functions are defined:
84
85\begin{funcdesc}{Internaldate2tuple}{datestr}
86 Converts an IMAP4 INTERNALDATE string to Coordinated Universal
Fred Drakeb7745501999-04-22 16:46:18 +000087 Time. Returns a \refmodule{time} module tuple.
Fred Drake89de3141998-04-11 04:19:04 +000088\end{funcdesc}
89
90\begin{funcdesc}{Int2AP}{num}
91 Converts an integer into a string representation using characters
92 from the set [\code{A} .. \code{P}].
93\end{funcdesc}
94
95\begin{funcdesc}{ParseFlags}{flagstr}
96 Converts an IMAP4 \samp{FLAGS} response to a tuple of individual
97 flags.
98\end{funcdesc}
99
100\begin{funcdesc}{Time2Internaldate}{date_time}
Fred Drakeb7745501999-04-22 16:46:18 +0000101 Converts a \refmodule{time} module tuple to an IMAP4
Fred Drake89de3141998-04-11 04:19:04 +0000102 \samp{INTERNALDATE} representation. Returns a string in the form:
103 \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes).
104\end{funcdesc}
105
106
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000107Note that IMAP4 message numbers change as the mailbox changes; in
108particular, after an \samp{EXPUNGE} command performs deletions the
109remaining messages are renumbered. So it is highly advisable to use
110UIDs instead, with the UID command.
Fred Drake363d67c1999-07-07 13:42:56 +0000111
112At the end of the module, there is a test section that contains a more
113extensive example of usage.
114
115\begin{seealso}
Fred Drake37f15741999-11-10 16:21:37 +0000116 \seetext{Documents describing the protocol, and sources and binaries
117 for servers implementing it, can all be found at the
118 University of Washington's \emph{IMAP Information Center}
119 (\url{http://www.cac.washington.edu/imap/}).}
Fred Drake363d67c1999-07-07 13:42:56 +0000120\end{seealso}
121
122
123\subsection{IMAP4 Objects \label{imap4-objects}}
Fred Drake89de3141998-04-11 04:19:04 +0000124
125All IMAP4rev1 commands are represented by methods of the same name,
126either upper-case or lower-case.
127
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000128All arguments to commands are converted to strings, except for
Fred Draked16b5ab1999-12-13 23:34:42 +0000129\samp{AUTHENTICATE}, and the last argument to \samp{APPEND} which is
130passed as an IMAP4 literal. If necessary (the string contains IMAP4
131protocol-sensitive characters and isn't enclosed with either
132parentheses or double quotes) each string is quoted. However, the
133\var{password} argument to the \samp{LOGIN} command is always quoted.
Fred Drake99d707a2000-05-26 04:08:37 +0000134If you want to avoid having an argument string quoted
135(eg: the \var{flags} argument to \samp{STORE}) then enclose the string in
136parentheses (eg: \code{r'(\e Deleted)'}).
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000137
Fred Drakeb7745501999-04-22 16:46:18 +0000138Each command returns a tuple: \code{(\var{type}, [\var{data},
139...])} where \var{type} is usually \code{'OK'} or \code{'NO'},
Fred Drake89de3141998-04-11 04:19:04 +0000140and \var{data} is either the text from the command response, or
Piers Lauderd3c821e2002-11-22 05:47:39 +0000141mandated results from the command. Each \var{data}
142is either a string, or a tuple. If a tuple, then the first part
143is the header of the response, and the second part contains
144the data (ie: 'literal' value).
Fred Drake89de3141998-04-11 04:19:04 +0000145
Fred Drake5e37d792005-01-19 04:44:07 +0000146The \var{message_set} options to commands below is a string specifying one
147or more messages to be acted upon. It may be a simple message number
148(\code{'1'}), a range of message numbers (\code{'2:4'}), or a group of
149non-contiguous ranges separated by commas (\code{'1:3,6:9'}). A range
150can contain an asterisk to indicate an infinite upper bound
151(\code{'3:*'}).
152
Fred Drake89de3141998-04-11 04:19:04 +0000153An \class{IMAP4} instance has the following methods:
154
155
156\begin{methoddesc}{append}{mailbox, flags, date_time, message}
Piers Lauder8bc81fc2004-05-20 12:12:58 +0000157 Append \var{message} to named mailbox.
Fred Drake89de3141998-04-11 04:19:04 +0000158\end{methoddesc}
159
Piers Lauder8bc81fc2004-05-20 12:12:58 +0000160\begin{methoddesc}{authenticate}{mechanism, authobject}
161 Authenticate command --- requires response processing.
162
Fred Drake63a5d0b2004-07-30 19:12:38 +0000163 \var{mechanism} specifies which authentication mechanism is to be
164 used - it should appear in the instance variable \code{capabilities}
165 in the form \code{AUTH=mechanism}.
Piers Lauder8bc81fc2004-05-20 12:12:58 +0000166
167 \var{authobject} must be a callable object:
168
169\begin{verbatim}
170data = authobject(response)
171\end{verbatim}
172
173 It will be called to process server continuation responses.
174 It should return \code{data} that will be encoded and sent to server.
175 It should return \code{None} if the client abort response \samp{*} should
176 be sent instead.
Fred Drake89de3141998-04-11 04:19:04 +0000177\end{methoddesc}
178
179\begin{methoddesc}{check}{}
180 Checkpoint mailbox on server.
181\end{methoddesc}
182
183\begin{methoddesc}{close}{}
184 Close currently selected mailbox. Deleted messages are removed from
185 writable mailbox. This is the recommended command before
186 \samp{LOGOUT}.
187\end{methoddesc}
188
189\begin{methoddesc}{copy}{message_set, new_mailbox}
190 Copy \var{message_set} messages onto end of \var{new_mailbox}.
191\end{methoddesc}
192
193\begin{methoddesc}{create}{mailbox}
194 Create new mailbox named \var{mailbox}.
195\end{methoddesc}
196
197\begin{methoddesc}{delete}{mailbox}
198 Delete old mailbox named \var{mailbox}.
199\end{methoddesc}
200
Martin v. Löwis7b9190b2004-07-27 05:07:19 +0000201\begin{methoddesc}{deleteacl}{mailbox, who}
202 Delete the ACLs (remove any rights) set for who on mailbox.
Neal Norwitzbee41742004-07-28 02:34:12 +0000203\versionadded{2.4}
Martin v. Löwis7b9190b2004-07-27 05:07:19 +0000204\end{methoddesc}
205
Fred Drake89de3141998-04-11 04:19:04 +0000206\begin{methoddesc}{expunge}{}
207 Permanently remove deleted items from selected mailbox. Generates an
208 \samp{EXPUNGE} response for each deleted message. Returned data
209 contains a list of \samp{EXPUNGE} message numbers in order
210 received.
211\end{methoddesc}
212
213\begin{methoddesc}{fetch}{message_set, message_parts}
Fred Drake99d707a2000-05-26 04:08:37 +0000214 Fetch (parts of) messages. \var{message_parts} should be
215 a string of message part names enclosed within parentheses,
216 eg: \samp{"(UID BODY[TEXT])"}. Returned data are tuples
217 of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000218\end{methoddesc}
219
Piers Laudera3a16682001-07-20 11:04:19 +0000220\begin{methoddesc}{getacl}{mailbox}
221 Get the \samp{ACL}s for \var{mailbox}.
222 The method is non-standard, but is supported by the \samp{Cyrus} server.
223\end{methoddesc}
224
Piers Lauder3fca2912002-06-17 07:07:20 +0000225\begin{methoddesc}{getquota}{root}
226 Get the \samp{quota} \var{root}'s resource usage and limits.
227 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000228\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000229\end{methoddesc}
230
231\begin{methoddesc}{getquotaroot}{mailbox}
232 Get the list of \samp{quota} \samp{roots} for the named \var{mailbox}.
233 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000234\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000235\end{methoddesc}
236
Fred Drakee5cf53a1998-04-11 05:02:45 +0000237\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
238 List mailbox names in \var{directory} matching
239 \var{pattern}. \var{directory} defaults to the top-level mail
240 folder, and \var{pattern} defaults to match anything. Returned data
241 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000242\end{methoddesc}
243
244\begin{methoddesc}{login}{user, password}
245 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000246 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000247\end{methoddesc}
248
Piers Lauderd3c821e2002-11-22 05:47:39 +0000249\begin{methoddesc}{login_cram_md5}{user, password}
Fred Drake63a5d0b2004-07-30 19:12:38 +0000250 Force use of \samp{CRAM-MD5} authentication when identifying the
251 client to protect the password. Will only work if the server
252 \samp{CAPABILITY} response includes the phrase \samp{AUTH=CRAM-MD5}.
Neal Norwitze1497982003-01-02 15:32:00 +0000253\versionadded{2.3}
Piers Lauderd3c821e2002-11-22 05:47:39 +0000254\end{methoddesc}
255
Fred Drake89de3141998-04-11 04:19:04 +0000256\begin{methoddesc}{logout}{}
257 Shutdown connection to server. Returns server \samp{BYE} response.
258\end{methoddesc}
259
Fred Drakee5cf53a1998-04-11 05:02:45 +0000260\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
261 List subscribed mailbox names in directory matching pattern.
262 \var{directory} defaults to the top level directory and
263 \var{pattern} defaults to match any mailbox.
264 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000265\end{methoddesc}
266
Martin v. Löwis479b7a72004-07-30 16:09:19 +0000267\begin{methoddesc}{myrights}{mailbox}
Martin v. Löwis7b9190b2004-07-27 05:07:19 +0000268 Show my ACLs for a mailbox (i.e. the rights that I have on mailbox).
Neal Norwitzbee41742004-07-28 02:34:12 +0000269\versionadded{2.4}
Martin v. Löwis7b9190b2004-07-27 05:07:19 +0000270\end{methoddesc}
271
Piers Lauder8bc81fc2004-05-20 12:12:58 +0000272\begin{methoddesc}{namespace}{}
273 Returns IMAP namespaces as defined in RFC2342.
274\versionadded{2.3}
275\end{methoddesc}
276
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000277\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000278 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000279\end{methoddesc}
280
281\begin{methoddesc}{open}{host, port}
282 Opens socket to \var{port} at \var{host}.
Piers Laudera3a16682001-07-20 11:04:19 +0000283 The connection objects established by this method
Fred Drake63a5d0b2004-07-30 19:12:38 +0000284 will be used in the \code{read}, \code{readline}, \code{send}, and
285 \code{shutdown} methods.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000286 You may override this method.
287\end{methoddesc}
288
289\begin{methoddesc}{partial}{message_num, message_part, start, length}
290 Fetch truncated part of a message.
291 Returned data is a tuple of message part envelope and data.
292\end{methoddesc}
293
Piers Lauderd3c821e2002-11-22 05:47:39 +0000294\begin{methoddesc}{proxyauth}{user}
295 Assume authentication as \var{user}.
296 Allows an authorised administrator to proxy into any user's mailbox.
Neal Norwitze1497982003-01-02 15:32:00 +0000297\versionadded{2.3}
Piers Lauderd3c821e2002-11-22 05:47:39 +0000298\end{methoddesc}
299
Piers Laudera3a16682001-07-20 11:04:19 +0000300\begin{methoddesc}{read}{size}
301 Reads \var{size} bytes from the remote server.
302 You may override this method.
303\end{methoddesc}
304
305\begin{methoddesc}{readline}{}
306 Reads one line from the remote server.
307 You may override this method.
308\end{methoddesc}
309
Fred Drake89de3141998-04-11 04:19:04 +0000310\begin{methoddesc}{recent}{}
311 Prompt server for an update. Returned data is \code{None} if no new
312 messages, else value of \samp{RECENT} response.
313\end{methoddesc}
314
315\begin{methoddesc}{rename}{oldmailbox, newmailbox}
316 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
317\end{methoddesc}
318
319\begin{methoddesc}{response}{code}
320 Return data for response \var{code} if received, or
321 \code{None}. Returns the given code, instead of the usual type.
322\end{methoddesc}
323
Martin v. Löwis3ae0f7a2003-03-27 16:59:38 +0000324\begin{methoddesc}{search}{charset, criterion\optional{, ...}}
Fred Drake99d707a2000-05-26 04:08:37 +0000325 Search mailbox for matching messages. Returned data contains a space
326 separated list of matching message numbers. \var{charset} may be
327 \code{None}, in which case no \samp{CHARSET} will be specified in the
328 request to the server. The IMAP protocol requires that at least one
Martin v. Löwis3ae0f7a2003-03-27 16:59:38 +0000329 criterion be specified; an exception will be raised when the server
Fred Drake99d707a2000-05-26 04:08:37 +0000330 returns an error.
331
332 Example:
333
334\begin{verbatim}
335# M is a connected IMAP4 instance...
336msgnums = M.search(None, 'FROM', '"LDJ"')
337
338# or:
339msgnums = M.search(None, '(FROM "LDJ")')
340\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000341\end{methoddesc}
342
343\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
344 Select a mailbox. Returned data is the count of messages in
345 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
346 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
347 to the mailbox are not allowed.
348\end{methoddesc}
349
Piers Lauder3fca2912002-06-17 07:07:20 +0000350\begin{methoddesc}{send}{data}
351 Sends \code{data} to the remote server.
352 You may override this method.
353\end{methoddesc}
354
Piers Laudera3a16682001-07-20 11:04:19 +0000355\begin{methoddesc}{setacl}{mailbox, who, what}
356 Set an \samp{ACL} for \var{mailbox}.
357 The method is non-standard, but is supported by the \samp{Cyrus} server.
358\end{methoddesc}
359
Piers Lauder3fca2912002-06-17 07:07:20 +0000360\begin{methoddesc}{setquota}{root, limits}
361 Set the \samp{quota} \var{root}'s resource \var{limits}.
362 This method is part of the IMAP4 QUOTA extension defined in rfc2087.
Neal Norwitz1cfcafc2002-07-20 00:46:12 +0000363\versionadded{2.3}
Piers Lauder3fca2912002-06-17 07:07:20 +0000364\end{methoddesc}
365
Piers Laudera3a16682001-07-20 11:04:19 +0000366\begin{methoddesc}{shutdown}{}
367 Close connection established in \code{open}.
368 You may override this method.
369\end{methoddesc}
370
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000371\begin{methoddesc}{socket}{}
Piers Laudera3a16682001-07-20 11:04:19 +0000372 Returns socket instance used to connect to server.
373\end{methoddesc}
374
Martin v. Löwis3ae0f7a2003-03-27 16:59:38 +0000375\begin{methoddesc}{sort}{sort_criteria, charset, search_criterion\optional{, ...}}
Fred Drake63a5d0b2004-07-30 19:12:38 +0000376 The \code{sort} command is a variant of \code{search} with sorting
377 semantics for the results. Returned data contains a space separated
378 list of matching message numbers.
Piers Laudera3a16682001-07-20 11:04:19 +0000379
Martin v. Löwis3ae0f7a2003-03-27 16:59:38 +0000380 Sort has two arguments before the \var{search_criterion}
Fred Drake63a5d0b2004-07-30 19:12:38 +0000381 argument(s); a parenthesized list of \var{sort_criteria}, and the
382 searching \var{charset}. Note that unlike \code{search}, the
383 searching \var{charset} argument is mandatory. There is also a
384 \code{uid sort} command which corresponds to \code{sort} the way
385 that \code{uid search} corresponds to \code{search}. The
386 \code{sort} command first searches the mailbox for messages that
Piers Laudera3a16682001-07-20 11:04:19 +0000387 match the given searching criteria using the charset argument for
388 the interpretation of strings in the searching criteria. It then
389 returns the numbers of matching messages.
390
391 This is an \samp{IMAP4rev1} extension command.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000392\end{methoddesc}
393
Fred Drake89de3141998-04-11 04:19:04 +0000394\begin{methoddesc}{status}{mailbox, names}
395 Request named status conditions for \var{mailbox}.
396\end{methoddesc}
397
398\begin{methoddesc}{store}{message_set, command, flag_list}
Fred Drake5e37d792005-01-19 04:44:07 +0000399 Alters flag dispositions for messages in mailbox. \var{command} is
400 specified by section 6.4.6 of \rfc{2060} as being one of "FLAGS", "+FLAGS",
401 or "-FLAGS", optionally with a suffix of ".SILENT".
402
403 For example, to set the delete flag on all messages:
404
405\begin{verbatim}
406typ, data = M.search(None, 'ALL')
407for num in data[0].split():
408 M.store(num, '+FLAGS', '\\Deleted')
409M.expunge()
410\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000411\end{methoddesc}
412
413\begin{methoddesc}{subscribe}{mailbox}
414 Subscribe to new mailbox.
415\end{methoddesc}
416
Fred Drake63a5d0b2004-07-30 19:12:38 +0000417\begin{methoddesc}{thread}{threading_algorithm, charset,
418 search_criterion\optional{, ...}}
419 The \code{thread} command is a variant of \code{search} with
420 threading semantics for the results. Returned data contains a space
Martin v. Löwisd8921372003-11-10 06:44:44 +0000421 separated list of thread members.
422
Fred Drake63a5d0b2004-07-30 19:12:38 +0000423 Thread members consist of zero or more messages numbers, delimited
424 by spaces, indicating successive parent and child.
Martin v. Löwisd8921372003-11-10 06:44:44 +0000425
426 Thread has two arguments before the \var{search_criterion}
Fred Drake63a5d0b2004-07-30 19:12:38 +0000427 argument(s); a \var{threading_algorithm}, and the searching
428 \var{charset}. Note that unlike \code{search}, the searching
429 \var{charset} argument is mandatory. There is also a \code{uid
430 thread} command which corresponds to \code{thread} the way that
431 \code{uid search} corresponds to \code{search}. The \code{thread}
432 command first searches the mailbox for messages that match the given
433 searching criteria using the charset argument for the interpretation
Raymond Hettinger68804312005-01-01 00:28:46 +0000434 of strings in the searching criteria. It then returns the matching
Fred Drake63a5d0b2004-07-30 19:12:38 +0000435 messages threaded according to the specified threading algorithm.
Martin v. Löwisd8921372003-11-10 06:44:44 +0000436
437 This is an \samp{IMAP4rev1} extension command. \versionadded{2.4}
438\end{methoddesc}
439
Fred Drake99d707a2000-05-26 04:08:37 +0000440\begin{methoddesc}{uid}{command, arg\optional{, ...}}
Fred Drake89de3141998-04-11 04:19:04 +0000441 Execute command args with messages identified by UID, rather than
Fred Drake99d707a2000-05-26 04:08:37 +0000442 message number. Returns response appropriate to command. At least
443 one argument must be supplied; if none are provided, the server will
444 return an error and an exception will be raised.
Fred Drake89de3141998-04-11 04:19:04 +0000445\end{methoddesc}
446
447\begin{methoddesc}{unsubscribe}{mailbox}
448 Unsubscribe from old mailbox.
449\end{methoddesc}
450
Fred Drake99d707a2000-05-26 04:08:37 +0000451\begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}}
Fred Drake89de3141998-04-11 04:19:04 +0000452 Allow simple extension commands notified by server in
453 \samp{CAPABILITY} response.
454\end{methoddesc}
455
456
Piers Laudera4f83132002-03-08 01:53:24 +0000457Instances of \class{IMAP4_SSL} have just one additional method:
458
459\begin{methoddesc}{ssl}{}
460 Returns SSLObject instance used for the secure connection with the server.
461\end{methoddesc}
462
463
Fred Drake8f6b9581998-04-11 15:11:55 +0000464The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000465
Fred Drake8f6b9581998-04-11 15:11:55 +0000466
467\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000468The most recent supported protocol in the
469\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000470\end{memberdesc}
471
472\begin{memberdesc}{debug}
473Integer value to control debugging output. The initialize value is
474taken from the module variable \code{Debug}. Values greater than
475three trace each command.
476\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000477
478
Fred Drake363d67c1999-07-07 13:42:56 +0000479\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000480
481Here is a minimal example (without error checking) that opens a
482mailbox and retrieves and prints all messages:
483
484\begin{verbatim}
Piers Laudera3a16682001-07-20 11:04:19 +0000485import getpass, imaplib
Fred Drake363d67c1999-07-07 13:42:56 +0000486
Fred Drake89de3141998-04-11 04:19:04 +0000487M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000488M.login(getpass.getuser(), getpass.getpass())
489M.select()
490typ, data = M.search(None, 'ALL')
Piers Laudera3a16682001-07-20 11:04:19 +0000491for num in data[0].split():
Fred Drake363d67c1999-07-07 13:42:56 +0000492 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000493 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake5e37d792005-01-19 04:44:07 +0000494M.close()
Fred Drake363d67c1999-07-07 13:42:56 +0000495M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000496\end{verbatim}