blob: 5725fd3779680f38719b6f3b3671c00107953714 [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>.
11
Fred Drakee5cf53a1998-04-11 05:02:45 +000012\indexii{IMAP4}{protocol}
Fred Drake89de3141998-04-11 04:19:04 +000013
14This module defines a class, \class{IMAP4}, which encapsulates a
15connection to an IMAP4 server and implements the IMAP4rev1 client
16protocol as defined in \rfc{2060}. It is backward compatible with
17IMAP4 (\rfc{1730}) servers, but note that the \samp{STATUS} command is
18not supported in IMAP4.
19
Fred Drakeb7745501999-04-22 16:46:18 +000020A single class is provided by the \module{imaplib} module:
Fred Drake89de3141998-04-11 04:19:04 +000021
22\begin{classdesc}{IMAP4}{\optional{host\optional{, port}}}
23This class implements the actual IMAP4 protocol. The connection is
24created and protocol version (IMAP4 or IMAP4rev1) is determined when
25the instance is initialized.
26If \var{host} is not specified, \code{''} (the local host) is used.
27If \var{port} is omitted, the standard IMAP4 port (143) is used.
28\end{classdesc}
29
30Two exceptions are defined as attributes of the \class{IMAP4} class:
31
32\begin{excdesc}{IMAP4.error}
33Exception raised on any errors. The reason for the exception is
34passed to the constructor as a string.
35\end{excdesc}
36
37\begin{excdesc}{IMAP4.abort}
38IMAP4 server errors cause this exception to be raised. This is a
39sub-class of \exception{IMAP4.error}. Note that closing the instance
40and instantiating a new one will usually allow recovery from this
41exception.
42\end{excdesc}
43
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000044\begin{excdesc}{IMAP4.readonly}
45This exception is raised when a writeable mailbox has its status changed by the server. This is a
46sub-class of \exception{IMAP4.error}. Some other client now has write permission,
47and the mailbox will need to be re-opened to re-obtain write permission.
48\end{excdesc}
49
Fred Drake89de3141998-04-11 04:19:04 +000050The following utility functions are defined:
51
52\begin{funcdesc}{Internaldate2tuple}{datestr}
53 Converts an IMAP4 INTERNALDATE string to Coordinated Universal
Fred Drakeb7745501999-04-22 16:46:18 +000054 Time. Returns a \refmodule{time} module tuple.
Fred Drake89de3141998-04-11 04:19:04 +000055\end{funcdesc}
56
57\begin{funcdesc}{Int2AP}{num}
58 Converts an integer into a string representation using characters
59 from the set [\code{A} .. \code{P}].
60\end{funcdesc}
61
62\begin{funcdesc}{ParseFlags}{flagstr}
63 Converts an IMAP4 \samp{FLAGS} response to a tuple of individual
64 flags.
65\end{funcdesc}
66
67\begin{funcdesc}{Time2Internaldate}{date_time}
Fred Drakeb7745501999-04-22 16:46:18 +000068 Converts a \refmodule{time} module tuple to an IMAP4
Fred Drake89de3141998-04-11 04:19:04 +000069 \samp{INTERNALDATE} representation. Returns a string in the form:
70 \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes).
71\end{funcdesc}
72
73
Fred Drake363d67c1999-07-07 13:42:56 +000074Note that IMAP4 message numbers change as the mailbox changes, so it
75is highly advisable to use UIDs instead, with the UID command.
76
77At the end of the module, there is a test section that contains a more
78extensive example of usage.
79
80\begin{seealso}
Fred Drake37f15741999-11-10 16:21:37 +000081 \seetext{Documents describing the protocol, and sources and binaries
82 for servers implementing it, can all be found at the
83 University of Washington's \emph{IMAP Information Center}
84 (\url{http://www.cac.washington.edu/imap/}).}
Fred Drake363d67c1999-07-07 13:42:56 +000085\end{seealso}
86
87
88\subsection{IMAP4 Objects \label{imap4-objects}}
Fred Drake89de3141998-04-11 04:19:04 +000089
90All IMAP4rev1 commands are represented by methods of the same name,
91either upper-case or lower-case.
92
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000093All arguments to commands are converted to strings, except for
Fred Draked16b5ab1999-12-13 23:34:42 +000094\samp{AUTHENTICATE}, and the last argument to \samp{APPEND} which is
95passed as an IMAP4 literal. If necessary (the string contains IMAP4
96protocol-sensitive characters and isn't enclosed with either
97parentheses or double quotes) each string is quoted. However, the
98\var{password} argument to the \samp{LOGIN} command is always quoted.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +000099
Fred Drakeb7745501999-04-22 16:46:18 +0000100Each command returns a tuple: \code{(\var{type}, [\var{data},
101...])} where \var{type} is usually \code{'OK'} or \code{'NO'},
Fred Drake89de3141998-04-11 04:19:04 +0000102and \var{data} is either the text from the command response, or
103mandated results from the command.
104
105An \class{IMAP4} instance has the following methods:
106
107
108\begin{methoddesc}{append}{mailbox, flags, date_time, message}
109 Append message to named mailbox.
110\end{methoddesc}
111
112\begin{methoddesc}{authenticate}{func}
113 Authenticate command --- requires response processing. This is
114 currently unimplemented, and raises an exception.
115\end{methoddesc}
116
117\begin{methoddesc}{check}{}
118 Checkpoint mailbox on server.
119\end{methoddesc}
120
121\begin{methoddesc}{close}{}
122 Close currently selected mailbox. Deleted messages are removed from
123 writable mailbox. This is the recommended command before
124 \samp{LOGOUT}.
125\end{methoddesc}
126
127\begin{methoddesc}{copy}{message_set, new_mailbox}
128 Copy \var{message_set} messages onto end of \var{new_mailbox}.
129\end{methoddesc}
130
131\begin{methoddesc}{create}{mailbox}
132 Create new mailbox named \var{mailbox}.
133\end{methoddesc}
134
135\begin{methoddesc}{delete}{mailbox}
136 Delete old mailbox named \var{mailbox}.
137\end{methoddesc}
138
139\begin{methoddesc}{expunge}{}
140 Permanently remove deleted items from selected mailbox. Generates an
141 \samp{EXPUNGE} response for each deleted message. Returned data
142 contains a list of \samp{EXPUNGE} message numbers in order
143 received.
144\end{methoddesc}
145
146\begin{methoddesc}{fetch}{message_set, message_parts}
147 Fetch (parts of) messages. Returned data are tuples of message part
148 envelope and data.
149\end{methoddesc}
150
Fred Drakee5cf53a1998-04-11 05:02:45 +0000151\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
152 List mailbox names in \var{directory} matching
153 \var{pattern}. \var{directory} defaults to the top-level mail
154 folder, and \var{pattern} defaults to match anything. Returned data
155 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000156\end{methoddesc}
157
158\begin{methoddesc}{login}{user, password}
159 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000160 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000161\end{methoddesc}
162
163\begin{methoddesc}{logout}{}
164 Shutdown connection to server. Returns server \samp{BYE} response.
165\end{methoddesc}
166
Fred Drakee5cf53a1998-04-11 05:02:45 +0000167\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
168 List subscribed mailbox names in directory matching pattern.
169 \var{directory} defaults to the top level directory and
170 \var{pattern} defaults to match any mailbox.
171 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000172\end{methoddesc}
173
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000174\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000175 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000176\end{methoddesc}
177
178\begin{methoddesc}{open}{host, port}
179 Opens socket to \var{port} at \var{host}.
180 You may override this method.
181\end{methoddesc}
182
183\begin{methoddesc}{partial}{message_num, message_part, start, length}
184 Fetch truncated part of a message.
185 Returned data is a tuple of message part envelope and data.
186\end{methoddesc}
187
Fred Drake89de3141998-04-11 04:19:04 +0000188\begin{methoddesc}{recent}{}
189 Prompt server for an update. Returned data is \code{None} if no new
190 messages, else value of \samp{RECENT} response.
191\end{methoddesc}
192
193\begin{methoddesc}{rename}{oldmailbox, newmailbox}
194 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
195\end{methoddesc}
196
197\begin{methoddesc}{response}{code}
198 Return data for response \var{code} if received, or
199 \code{None}. Returns the given code, instead of the usual type.
200\end{methoddesc}
201
202\begin{methoddesc}{search}{charset, criteria}
203 Search mailbox for matching messages. Returned data contains a space
204 separated list of matching message numbers.
205\end{methoddesc}
206
207\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
208 Select a mailbox. Returned data is the count of messages in
209 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
210 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
211 to the mailbox are not allowed.
212\end{methoddesc}
213
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000214\begin{methoddesc}{socket}{}
215 Returns socket instance used to connect to server.
216\end{methoddesc}
217
Fred Drake89de3141998-04-11 04:19:04 +0000218\begin{methoddesc}{status}{mailbox, names}
219 Request named status conditions for \var{mailbox}.
220\end{methoddesc}
221
222\begin{methoddesc}{store}{message_set, command, flag_list}
223 Alters flag dispositions for messages in mailbox.
224\end{methoddesc}
225
226\begin{methoddesc}{subscribe}{mailbox}
227 Subscribe to new mailbox.
228\end{methoddesc}
229
230\begin{methoddesc}{uid}{command, args}
231 Execute command args with messages identified by UID, rather than
232 message number. Returns response appropriate to command.
233\end{methoddesc}
234
235\begin{methoddesc}{unsubscribe}{mailbox}
236 Unsubscribe from old mailbox.
237\end{methoddesc}
238
239\begin{methoddesc}{xatom}{name\optional{, arg1\optional{, arg2}}}
240 Allow simple extension commands notified by server in
241 \samp{CAPABILITY} response.
242\end{methoddesc}
243
244
Fred Drake8f6b9581998-04-11 15:11:55 +0000245The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000246
Fred Drake8f6b9581998-04-11 15:11:55 +0000247
248\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000249The most recent supported protocol in the
250\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000251\end{memberdesc}
252
253\begin{memberdesc}{debug}
254Integer value to control debugging output. The initialize value is
255taken from the module variable \code{Debug}. Values greater than
256three trace each command.
257\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000258
259
Fred Drake363d67c1999-07-07 13:42:56 +0000260\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000261
262Here is a minimal example (without error checking) that opens a
263mailbox and retrieves and prints all messages:
264
265\begin{verbatim}
266import getpass, imaplib, string
Fred Drake363d67c1999-07-07 13:42:56 +0000267
Fred Drake89de3141998-04-11 04:19:04 +0000268M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000269M.login(getpass.getuser(), getpass.getpass())
270M.select()
271typ, data = M.search(None, 'ALL')
Fred Drake89de3141998-04-11 04:19:04 +0000272for num in string.split(data[0]):
Fred Drake363d67c1999-07-07 13:42:56 +0000273 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000274 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000275M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000276\end{verbatim}