blob: f4c3e19c0b994899f27c5273464b055476be735a [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.
Fred Drake38e5d272000-04-03 20:13:55 +000012
Fred Drakee5cf53a1998-04-11 05:02:45 +000013\indexii{IMAP4}{protocol}
Fred Drake89de3141998-04-11 04:19:04 +000014
15This module defines a class, \class{IMAP4}, which encapsulates a
Eric S. Raymond5ac97952001-01-11 04:19:52 +000016connection to an IMAP4 server and implements a large subset of the
17IMAP4rev1 client protocol as defined in \rfc{2060}. It is backward
18compatible with IMAP4 (\rfc{1730}) servers, but note that the
19\samp{STATUS} command is not supported in IMAP4.
Fred Drake89de3141998-04-11 04:19:04 +000020
Fred Drakeb7745501999-04-22 16:46:18 +000021A single class is provided by the \module{imaplib} module:
Fred Drake89de3141998-04-11 04:19:04 +000022
23\begin{classdesc}{IMAP4}{\optional{host\optional{, port}}}
24This class implements the actual IMAP4 protocol. The connection is
25created and protocol version (IMAP4 or IMAP4rev1) is determined when
26the instance is initialized.
27If \var{host} is not specified, \code{''} (the local host) is used.
28If \var{port} is omitted, the standard IMAP4 port (143) is used.
29\end{classdesc}
30
31Two exceptions are defined as attributes of the \class{IMAP4} class:
32
33\begin{excdesc}{IMAP4.error}
34Exception raised on any errors. The reason for the exception is
35passed to the constructor as a string.
36\end{excdesc}
37
38\begin{excdesc}{IMAP4.abort}
39IMAP4 server errors cause this exception to be raised. This is a
40sub-class of \exception{IMAP4.error}. Note that closing the instance
41and instantiating a new one will usually allow recovery from this
42exception.
43\end{excdesc}
44
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000045\begin{excdesc}{IMAP4.readonly}
Thomas Woutersf8316632000-07-16 19:01:10 +000046This 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 +000047sub-class of \exception{IMAP4.error}. Some other client now has write permission,
48and the mailbox will need to be re-opened to re-obtain write permission.
49\end{excdesc}
50
Fred Drake89de3141998-04-11 04:19:04 +000051The following utility functions are defined:
52
53\begin{funcdesc}{Internaldate2tuple}{datestr}
54 Converts an IMAP4 INTERNALDATE string to Coordinated Universal
Fred Drakeb7745501999-04-22 16:46:18 +000055 Time. Returns a \refmodule{time} module tuple.
Fred Drake89de3141998-04-11 04:19:04 +000056\end{funcdesc}
57
58\begin{funcdesc}{Int2AP}{num}
59 Converts an integer into a string representation using characters
60 from the set [\code{A} .. \code{P}].
61\end{funcdesc}
62
63\begin{funcdesc}{ParseFlags}{flagstr}
64 Converts an IMAP4 \samp{FLAGS} response to a tuple of individual
65 flags.
66\end{funcdesc}
67
68\begin{funcdesc}{Time2Internaldate}{date_time}
Fred Drakeb7745501999-04-22 16:46:18 +000069 Converts a \refmodule{time} module tuple to an IMAP4
Fred Drake89de3141998-04-11 04:19:04 +000070 \samp{INTERNALDATE} representation. Returns a string in the form:
71 \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes).
72\end{funcdesc}
73
74
Eric S. Raymond5ac97952001-01-11 04:19:52 +000075Note that IMAP4 message numbers change as the mailbox changes; in
76particular, after an \samp{EXPUNGE} command performs deletions the
77remaining messages are renumbered. So it is highly advisable to use
78UIDs instead, with the UID command.
Fred Drake363d67c1999-07-07 13:42:56 +000079
80At the end of the module, there is a test section that contains a more
81extensive example of usage.
82
83\begin{seealso}
Fred Drake37f15741999-11-10 16:21:37 +000084 \seetext{Documents describing the protocol, and sources and binaries
85 for servers implementing it, can all be found at the
86 University of Washington's \emph{IMAP Information Center}
87 (\url{http://www.cac.washington.edu/imap/}).}
Fred Drake363d67c1999-07-07 13:42:56 +000088\end{seealso}
89
90
91\subsection{IMAP4 Objects \label{imap4-objects}}
Fred Drake89de3141998-04-11 04:19:04 +000092
93All IMAP4rev1 commands are represented by methods of the same name,
94either upper-case or lower-case.
95
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000096All arguments to commands are converted to strings, except for
Fred Draked16b5ab1999-12-13 23:34:42 +000097\samp{AUTHENTICATE}, and the last argument to \samp{APPEND} which is
98passed as an IMAP4 literal. If necessary (the string contains IMAP4
99protocol-sensitive characters and isn't enclosed with either
100parentheses or double quotes) each string is quoted. However, the
101\var{password} argument to the \samp{LOGIN} command is always quoted.
Fred Drake99d707a2000-05-26 04:08:37 +0000102If you want to avoid having an argument string quoted
103(eg: the \var{flags} argument to \samp{STORE}) then enclose the string in
104parentheses (eg: \code{r'(\e Deleted)'}).
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000105
Fred Drakeb7745501999-04-22 16:46:18 +0000106Each command returns a tuple: \code{(\var{type}, [\var{data},
107...])} where \var{type} is usually \code{'OK'} or \code{'NO'},
Fred Drake89de3141998-04-11 04:19:04 +0000108and \var{data} is either the text from the command response, or
109mandated results from the command.
110
111An \class{IMAP4} instance has the following methods:
112
113
114\begin{methoddesc}{append}{mailbox, flags, date_time, message}
115 Append message to named mailbox.
116\end{methoddesc}
117
118\begin{methoddesc}{authenticate}{func}
119 Authenticate command --- requires response processing. This is
120 currently unimplemented, and raises an exception.
121\end{methoddesc}
122
123\begin{methoddesc}{check}{}
124 Checkpoint mailbox on server.
125\end{methoddesc}
126
127\begin{methoddesc}{close}{}
128 Close currently selected mailbox. Deleted messages are removed from
129 writable mailbox. This is the recommended command before
130 \samp{LOGOUT}.
131\end{methoddesc}
132
133\begin{methoddesc}{copy}{message_set, new_mailbox}
134 Copy \var{message_set} messages onto end of \var{new_mailbox}.
135\end{methoddesc}
136
137\begin{methoddesc}{create}{mailbox}
138 Create new mailbox named \var{mailbox}.
139\end{methoddesc}
140
141\begin{methoddesc}{delete}{mailbox}
142 Delete old mailbox named \var{mailbox}.
143\end{methoddesc}
144
145\begin{methoddesc}{expunge}{}
146 Permanently remove deleted items from selected mailbox. Generates an
147 \samp{EXPUNGE} response for each deleted message. Returned data
148 contains a list of \samp{EXPUNGE} message numbers in order
149 received.
150\end{methoddesc}
151
152\begin{methoddesc}{fetch}{message_set, message_parts}
Fred Drake99d707a2000-05-26 04:08:37 +0000153 Fetch (parts of) messages. \var{message_parts} should be
154 a string of message part names enclosed within parentheses,
155 eg: \samp{"(UID BODY[TEXT])"}. Returned data are tuples
156 of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000157\end{methoddesc}
158
Piers Laudera3a16682001-07-20 11:04:19 +0000159\begin{methoddesc}{getacl}{mailbox}
160 Get the \samp{ACL}s for \var{mailbox}.
161 The method is non-standard, but is supported by the \samp{Cyrus} server.
162\end{methoddesc}
163
Fred Drakee5cf53a1998-04-11 05:02:45 +0000164\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
165 List mailbox names in \var{directory} matching
166 \var{pattern}. \var{directory} defaults to the top-level mail
167 folder, and \var{pattern} defaults to match anything. Returned data
168 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000169\end{methoddesc}
170
171\begin{methoddesc}{login}{user, password}
172 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000173 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000174\end{methoddesc}
175
176\begin{methoddesc}{logout}{}
177 Shutdown connection to server. Returns server \samp{BYE} response.
178\end{methoddesc}
179
Fred Drakee5cf53a1998-04-11 05:02:45 +0000180\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
181 List subscribed mailbox names in directory matching pattern.
182 \var{directory} defaults to the top level directory and
183 \var{pattern} defaults to match any mailbox.
184 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000185\end{methoddesc}
186
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000187\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000188 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000189\end{methoddesc}
190
191\begin{methoddesc}{open}{host, port}
192 Opens socket to \var{port} at \var{host}.
Piers Laudera3a16682001-07-20 11:04:19 +0000193 The connection objects established by this method
194 will be used in the \code{read}, \code{readline}, and \code{shutdown} methods.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000195 You may override this method.
196\end{methoddesc}
197
198\begin{methoddesc}{partial}{message_num, message_part, start, length}
199 Fetch truncated part of a message.
200 Returned data is a tuple of message part envelope and data.
201\end{methoddesc}
202
Piers Laudera3a16682001-07-20 11:04:19 +0000203\begin{methoddesc}{read}{size}
204 Reads \var{size} bytes from the remote server.
205 You may override this method.
206\end{methoddesc}
207
208\begin{methoddesc}{readline}{}
209 Reads one line from the remote server.
210 You may override this method.
211\end{methoddesc}
212
Fred Drake89de3141998-04-11 04:19:04 +0000213\begin{methoddesc}{recent}{}
214 Prompt server for an update. Returned data is \code{None} if no new
215 messages, else value of \samp{RECENT} response.
216\end{methoddesc}
217
218\begin{methoddesc}{rename}{oldmailbox, newmailbox}
219 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
220\end{methoddesc}
221
222\begin{methoddesc}{response}{code}
223 Return data for response \var{code} if received, or
224 \code{None}. Returns the given code, instead of the usual type.
225\end{methoddesc}
226
Fred Drake99d707a2000-05-26 04:08:37 +0000227\begin{methoddesc}{search}{charset, criterium\optional{, ...}}
228 Search mailbox for matching messages. Returned data contains a space
229 separated list of matching message numbers. \var{charset} may be
230 \code{None}, in which case no \samp{CHARSET} will be specified in the
231 request to the server. The IMAP protocol requires that at least one
232 criterium be specified; an exception will be raised when the server
233 returns an error.
234
235 Example:
236
237\begin{verbatim}
238# M is a connected IMAP4 instance...
239msgnums = M.search(None, 'FROM', '"LDJ"')
240
241# or:
242msgnums = M.search(None, '(FROM "LDJ")')
243\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000244\end{methoddesc}
245
246\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
247 Select a mailbox. Returned data is the count of messages in
248 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
249 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
250 to the mailbox are not allowed.
251\end{methoddesc}
252
Piers Laudera3a16682001-07-20 11:04:19 +0000253\begin{methoddesc}{setacl}{mailbox, who, what}
254 Set an \samp{ACL} for \var{mailbox}.
255 The method is non-standard, but is supported by the \samp{Cyrus} server.
256\end{methoddesc}
257
258\begin{methoddesc}{shutdown}{}
259 Close connection established in \code{open}.
260 You may override this method.
261\end{methoddesc}
262
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000263\begin{methoddesc}{socket}{}
Piers Laudera3a16682001-07-20 11:04:19 +0000264 Returns socket instance used to connect to server.
265\end{methoddesc}
266
267\begin{methoddesc}{sort}{sort_criteria, charset, search_criterium\optional{, ...}}
268 The \code{sort} command is a variant of \code{search} with sorting semantics for
269 the results. Returned data contains a space
270 separated list of matching message numbers.
271
272 Sort has two arguments before the \var{search_criterium}
273 argument(s); a parenthesized list of \var{sort_criteria}, and the searching \var{charset}.
274 Note that unlike \code{search}, the searching \var{charset} argument is mandatory.
275 There is also a \code{uid sort} command which corresponds to \code{sort} the way
276 that \code{uid search} corresponds to \code{search}.
277 The \code{sort} command first searches the mailbox for messages that
278 match the given searching criteria using the charset argument for
279 the interpretation of strings in the searching criteria. It then
280 returns the numbers of matching messages.
281
282 This is an \samp{IMAP4rev1} extension command.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000283\end{methoddesc}
284
Fred Drake89de3141998-04-11 04:19:04 +0000285\begin{methoddesc}{status}{mailbox, names}
286 Request named status conditions for \var{mailbox}.
287\end{methoddesc}
288
289\begin{methoddesc}{store}{message_set, command, flag_list}
290 Alters flag dispositions for messages in mailbox.
291\end{methoddesc}
292
293\begin{methoddesc}{subscribe}{mailbox}
294 Subscribe to new mailbox.
295\end{methoddesc}
296
Fred Drake99d707a2000-05-26 04:08:37 +0000297\begin{methoddesc}{uid}{command, arg\optional{, ...}}
Fred Drake89de3141998-04-11 04:19:04 +0000298 Execute command args with messages identified by UID, rather than
Fred Drake99d707a2000-05-26 04:08:37 +0000299 message number. Returns response appropriate to command. At least
300 one argument must be supplied; if none are provided, the server will
301 return an error and an exception will be raised.
Fred Drake89de3141998-04-11 04:19:04 +0000302\end{methoddesc}
303
304\begin{methoddesc}{unsubscribe}{mailbox}
305 Unsubscribe from old mailbox.
306\end{methoddesc}
307
Fred Drake99d707a2000-05-26 04:08:37 +0000308\begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}}
Fred Drake89de3141998-04-11 04:19:04 +0000309 Allow simple extension commands notified by server in
310 \samp{CAPABILITY} response.
311\end{methoddesc}
312
313
Fred Drake8f6b9581998-04-11 15:11:55 +0000314The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000315
Fred Drake8f6b9581998-04-11 15:11:55 +0000316
317\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000318The most recent supported protocol in the
319\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000320\end{memberdesc}
321
322\begin{memberdesc}{debug}
323Integer value to control debugging output. The initialize value is
324taken from the module variable \code{Debug}. Values greater than
325three trace each command.
326\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000327
328
Fred Drake363d67c1999-07-07 13:42:56 +0000329\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000330
331Here is a minimal example (without error checking) that opens a
332mailbox and retrieves and prints all messages:
333
334\begin{verbatim}
Piers Laudera3a16682001-07-20 11:04:19 +0000335import getpass, imaplib
Fred Drake363d67c1999-07-07 13:42:56 +0000336
Fred Drake89de3141998-04-11 04:19:04 +0000337M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000338M.login(getpass.getuser(), getpass.getpass())
339M.select()
340typ, data = M.search(None, 'ALL')
Piers Laudera3a16682001-07-20 11:04:19 +0000341for num in data[0].split():
Fred Drake363d67c1999-07-07 13:42:56 +0000342 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000343 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000344M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000345\end{verbatim}