blob: 8fdc35955cb18aad026b3ea524b6808823fc26ef [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}
Thomas Woutersf8316632000-07-16 19:01:10 +000045This 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 +000046sub-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.
Fred Drake99d707a2000-05-26 04:08:37 +000099If you want to avoid having an argument string quoted
100(eg: the \var{flags} argument to \samp{STORE}) then enclose the string in
101parentheses (eg: \code{r'(\e Deleted)'}).
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000102
Fred Drakeb7745501999-04-22 16:46:18 +0000103Each command returns a tuple: \code{(\var{type}, [\var{data},
104...])} where \var{type} is usually \code{'OK'} or \code{'NO'},
Fred Drake89de3141998-04-11 04:19:04 +0000105and \var{data} is either the text from the command response, or
106mandated results from the command.
107
108An \class{IMAP4} instance has the following methods:
109
110
111\begin{methoddesc}{append}{mailbox, flags, date_time, message}
112 Append message to named mailbox.
113\end{methoddesc}
114
115\begin{methoddesc}{authenticate}{func}
116 Authenticate command --- requires response processing. This is
117 currently unimplemented, and raises an exception.
118\end{methoddesc}
119
120\begin{methoddesc}{check}{}
121 Checkpoint mailbox on server.
122\end{methoddesc}
123
124\begin{methoddesc}{close}{}
125 Close currently selected mailbox. Deleted messages are removed from
126 writable mailbox. This is the recommended command before
127 \samp{LOGOUT}.
128\end{methoddesc}
129
130\begin{methoddesc}{copy}{message_set, new_mailbox}
131 Copy \var{message_set} messages onto end of \var{new_mailbox}.
132\end{methoddesc}
133
134\begin{methoddesc}{create}{mailbox}
135 Create new mailbox named \var{mailbox}.
136\end{methoddesc}
137
138\begin{methoddesc}{delete}{mailbox}
139 Delete old mailbox named \var{mailbox}.
140\end{methoddesc}
141
142\begin{methoddesc}{expunge}{}
143 Permanently remove deleted items from selected mailbox. Generates an
144 \samp{EXPUNGE} response for each deleted message. Returned data
145 contains a list of \samp{EXPUNGE} message numbers in order
146 received.
147\end{methoddesc}
148
149\begin{methoddesc}{fetch}{message_set, message_parts}
Fred Drake99d707a2000-05-26 04:08:37 +0000150 Fetch (parts of) messages. \var{message_parts} should be
151 a string of message part names enclosed within parentheses,
152 eg: \samp{"(UID BODY[TEXT])"}. Returned data are tuples
153 of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000154\end{methoddesc}
155
Fred Drakee5cf53a1998-04-11 05:02:45 +0000156\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
157 List mailbox names in \var{directory} matching
158 \var{pattern}. \var{directory} defaults to the top-level mail
159 folder, and \var{pattern} defaults to match anything. Returned data
160 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000161\end{methoddesc}
162
163\begin{methoddesc}{login}{user, password}
164 Identify the client using a plaintext password.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000165 The \var{password} will be quoted.
Fred Drake89de3141998-04-11 04:19:04 +0000166\end{methoddesc}
167
168\begin{methoddesc}{logout}{}
169 Shutdown connection to server. Returns server \samp{BYE} response.
170\end{methoddesc}
171
Fred Drakee5cf53a1998-04-11 05:02:45 +0000172\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
173 List subscribed mailbox names in directory matching pattern.
174 \var{directory} defaults to the top level directory and
175 \var{pattern} defaults to match any mailbox.
176 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000177\end{methoddesc}
178
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000179\begin{methoddesc}{noop}{}
Fred Draked16b5ab1999-12-13 23:34:42 +0000180 Send \samp{NOOP} to server.
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000181\end{methoddesc}
182
183\begin{methoddesc}{open}{host, port}
184 Opens socket to \var{port} at \var{host}.
185 You may override this method.
186\end{methoddesc}
187
188\begin{methoddesc}{partial}{message_num, message_part, start, length}
189 Fetch truncated part of a message.
190 Returned data is a tuple of message part envelope and data.
191\end{methoddesc}
192
Fred Drake89de3141998-04-11 04:19:04 +0000193\begin{methoddesc}{recent}{}
194 Prompt server for an update. Returned data is \code{None} if no new
195 messages, else value of \samp{RECENT} response.
196\end{methoddesc}
197
198\begin{methoddesc}{rename}{oldmailbox, newmailbox}
199 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
200\end{methoddesc}
201
202\begin{methoddesc}{response}{code}
203 Return data for response \var{code} if received, or
204 \code{None}. Returns the given code, instead of the usual type.
205\end{methoddesc}
206
Fred Drake99d707a2000-05-26 04:08:37 +0000207\begin{methoddesc}{search}{charset, criterium\optional{, ...}}
208 Search mailbox for matching messages. Returned data contains a space
209 separated list of matching message numbers. \var{charset} may be
210 \code{None}, in which case no \samp{CHARSET} will be specified in the
211 request to the server. The IMAP protocol requires that at least one
212 criterium be specified; an exception will be raised when the server
213 returns an error.
214
215 Example:
216
217\begin{verbatim}
218# M is a connected IMAP4 instance...
219msgnums = M.search(None, 'FROM', '"LDJ"')
220
221# or:
222msgnums = M.search(None, '(FROM "LDJ")')
223\end{verbatim}
Fred Drake89de3141998-04-11 04:19:04 +0000224\end{methoddesc}
225
226\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
227 Select a mailbox. Returned data is the count of messages in
228 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
229 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
230 to the mailbox are not allowed.
231\end{methoddesc}
232
Guido van Rossum5f7a28c1999-12-13 23:29:39 +0000233\begin{methoddesc}{socket}{}
234 Returns socket instance used to connect to server.
235\end{methoddesc}
236
Fred Drake89de3141998-04-11 04:19:04 +0000237\begin{methoddesc}{status}{mailbox, names}
238 Request named status conditions for \var{mailbox}.
239\end{methoddesc}
240
241\begin{methoddesc}{store}{message_set, command, flag_list}
242 Alters flag dispositions for messages in mailbox.
243\end{methoddesc}
244
245\begin{methoddesc}{subscribe}{mailbox}
246 Subscribe to new mailbox.
247\end{methoddesc}
248
Fred Drake99d707a2000-05-26 04:08:37 +0000249\begin{methoddesc}{uid}{command, arg\optional{, ...}}
Fred Drake89de3141998-04-11 04:19:04 +0000250 Execute command args with messages identified by UID, rather than
Fred Drake99d707a2000-05-26 04:08:37 +0000251 message number. Returns response appropriate to command. At least
252 one argument must be supplied; if none are provided, the server will
253 return an error and an exception will be raised.
Fred Drake89de3141998-04-11 04:19:04 +0000254\end{methoddesc}
255
256\begin{methoddesc}{unsubscribe}{mailbox}
257 Unsubscribe from old mailbox.
258\end{methoddesc}
259
Fred Drake99d707a2000-05-26 04:08:37 +0000260\begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}}
Fred Drake89de3141998-04-11 04:19:04 +0000261 Allow simple extension commands notified by server in
262 \samp{CAPABILITY} response.
263\end{methoddesc}
264
265
Fred Drake8f6b9581998-04-11 15:11:55 +0000266The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000267
Fred Drake8f6b9581998-04-11 15:11:55 +0000268
269\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000270The most recent supported protocol in the
271\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000272\end{memberdesc}
273
274\begin{memberdesc}{debug}
275Integer value to control debugging output. The initialize value is
276taken from the module variable \code{Debug}. Values greater than
277three trace each command.
278\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000279
280
Fred Drake363d67c1999-07-07 13:42:56 +0000281\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000282
283Here is a minimal example (without error checking) that opens a
284mailbox and retrieves and prints all messages:
285
286\begin{verbatim}
287import getpass, imaplib, string
Fred Drake363d67c1999-07-07 13:42:56 +0000288
Fred Drake89de3141998-04-11 04:19:04 +0000289M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000290M.login(getpass.getuser(), getpass.getpass())
291M.select()
292typ, data = M.search(None, 'ALL')
Fred Drake89de3141998-04-11 04:19:04 +0000293for num in string.split(data[0]):
Fred Drake363d67c1999-07-07 13:42:56 +0000294 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000295 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000296M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000297\end{verbatim}