blob: 14daf41cfff5c6588a2b67bd0d56367f04c52de9 [file] [log] [blame]
Fred Drake2a374551998-04-11 13:48:26 +00001% Based on HTML documentation by Piers Lauder <piers@staff.cs.usyd.edu.au>;
Fred Drake89de3141998-04-11 04:19:04 +00002% converted by Fred L. Drake, Jr. <fdrake@acm.org>.
Fred Drake89de3141998-04-11 04:19:04 +00003
Fred Drake295da241998-08-10 19:42:37 +00004\section{\module{imaplib} ---
5 IMAP4 protocol client.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006\declaremodule{standard}{imaplib}
Fred Drake295da241998-08-10 19:42:37 +00007\moduleauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au}
8\sectionauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au}
Fred Drakeb91e9341998-07-23 17:59:49 +00009
10\modulesynopsis{IMAP4 protocol client (requires sockets).}
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
20A single class is provided by the \code{imaplib} module:
21
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
44The following utility functions are defined:
45
46\begin{funcdesc}{Internaldate2tuple}{datestr}
47 Converts an IMAP4 INTERNALDATE string to Coordinated Universal
48 Time. Returns a \module{time} module tuple.
49\end{funcdesc}
50
51\begin{funcdesc}{Int2AP}{num}
52 Converts an integer into a string representation using characters
53 from the set [\code{A} .. \code{P}].
54\end{funcdesc}
55
56\begin{funcdesc}{ParseFlags}{flagstr}
57 Converts an IMAP4 \samp{FLAGS} response to a tuple of individual
58 flags.
59\end{funcdesc}
60
61\begin{funcdesc}{Time2Internaldate}{date_time}
62 Converts a \module{time} module tuple to an IMAP4
63 \samp{INTERNALDATE} representation. Returns a string in the form:
64 \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes).
65\end{funcdesc}
66
67
68\subsection{IMAP4 Objects}
Fred Drake7dba8e21998-04-11 13:51:05 +000069\label{imap4-objects}
Fred Drake89de3141998-04-11 04:19:04 +000070
71All IMAP4rev1 commands are represented by methods of the same name,
72either upper-case or lower-case.
73
74Each command returns a tuple: \code{(}\var{type}, \code{[}\var{data},
75...\code{])} where \var{type} is usually \code{'OK'} or \code{'NO'},
76and \var{data} is either the text from the command response, or
77mandated results from the command.
78
79An \class{IMAP4} instance has the following methods:
80
81
82\begin{methoddesc}{append}{mailbox, flags, date_time, message}
83 Append message to named mailbox.
84\end{methoddesc}
85
86\begin{methoddesc}{authenticate}{func}
87 Authenticate command --- requires response processing. This is
88 currently unimplemented, and raises an exception.
89\end{methoddesc}
90
91\begin{methoddesc}{check}{}
92 Checkpoint mailbox on server.
93\end{methoddesc}
94
95\begin{methoddesc}{close}{}
96 Close currently selected mailbox. Deleted messages are removed from
97 writable mailbox. This is the recommended command before
98 \samp{LOGOUT}.
99\end{methoddesc}
100
101\begin{methoddesc}{copy}{message_set, new_mailbox}
102 Copy \var{message_set} messages onto end of \var{new_mailbox}.
103\end{methoddesc}
104
105\begin{methoddesc}{create}{mailbox}
106 Create new mailbox named \var{mailbox}.
107\end{methoddesc}
108
109\begin{methoddesc}{delete}{mailbox}
110 Delete old mailbox named \var{mailbox}.
111\end{methoddesc}
112
113\begin{methoddesc}{expunge}{}
114 Permanently remove deleted items from selected mailbox. Generates an
115 \samp{EXPUNGE} response for each deleted message. Returned data
116 contains a list of \samp{EXPUNGE} message numbers in order
117 received.
118\end{methoddesc}
119
120\begin{methoddesc}{fetch}{message_set, message_parts}
121 Fetch (parts of) messages. Returned data are tuples of message part
122 envelope and data.
123\end{methoddesc}
124
Fred Drakee5cf53a1998-04-11 05:02:45 +0000125\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
126 List mailbox names in \var{directory} matching
127 \var{pattern}. \var{directory} defaults to the top-level mail
128 folder, and \var{pattern} defaults to match anything. Returned data
129 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000130\end{methoddesc}
131
132\begin{methoddesc}{login}{user, password}
133 Identify the client using a plaintext password.
134\end{methoddesc}
135
136\begin{methoddesc}{logout}{}
137 Shutdown connection to server. Returns server \samp{BYE} response.
138\end{methoddesc}
139
Fred Drakee5cf53a1998-04-11 05:02:45 +0000140\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
141 List subscribed mailbox names in directory matching pattern.
142 \var{directory} defaults to the top level directory and
143 \var{pattern} defaults to match any mailbox.
144 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000145\end{methoddesc}
146
147\begin{methoddesc}{recent}{}
148 Prompt server for an update. Returned data is \code{None} if no new
149 messages, else value of \samp{RECENT} response.
150\end{methoddesc}
151
152\begin{methoddesc}{rename}{oldmailbox, newmailbox}
153 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
154\end{methoddesc}
155
156\begin{methoddesc}{response}{code}
157 Return data for response \var{code} if received, or
158 \code{None}. Returns the given code, instead of the usual type.
159\end{methoddesc}
160
161\begin{methoddesc}{search}{charset, criteria}
162 Search mailbox for matching messages. Returned data contains a space
163 separated list of matching message numbers.
164\end{methoddesc}
165
166\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
167 Select a mailbox. Returned data is the count of messages in
168 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
169 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
170 to the mailbox are not allowed.
171\end{methoddesc}
172
173\begin{methoddesc}{status}{mailbox, names}
174 Request named status conditions for \var{mailbox}.
175\end{methoddesc}
176
177\begin{methoddesc}{store}{message_set, command, flag_list}
178 Alters flag dispositions for messages in mailbox.
179\end{methoddesc}
180
181\begin{methoddesc}{subscribe}{mailbox}
182 Subscribe to new mailbox.
183\end{methoddesc}
184
185\begin{methoddesc}{uid}{command, args}
186 Execute command args with messages identified by UID, rather than
187 message number. Returns response appropriate to command.
188\end{methoddesc}
189
190\begin{methoddesc}{unsubscribe}{mailbox}
191 Unsubscribe from old mailbox.
192\end{methoddesc}
193
194\begin{methoddesc}{xatom}{name\optional{, arg1\optional{, arg2}}}
195 Allow simple extension commands notified by server in
196 \samp{CAPABILITY} response.
197\end{methoddesc}
198
199
Fred Drake8f6b9581998-04-11 15:11:55 +0000200The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000201
Fred Drake8f6b9581998-04-11 15:11:55 +0000202
203\begin{memberdesc}{PROTOCOL_VERSION}
204The most recent supported protocol in the \samp{CAPABILITY}
205response from the server.
206\end{memberdesc}
207
208\begin{memberdesc}{debug}
209Integer value to control debugging output. The initialize value is
210taken from the module variable \code{Debug}. Values greater than
211three trace each command.
212\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000213
214
215\subsection{IMAP4 Example}
Fred Drake7dba8e21998-04-11 13:51:05 +0000216\label{imap4-example}
Fred Drake89de3141998-04-11 04:19:04 +0000217
218Here is a minimal example (without error checking) that opens a
219mailbox and retrieves and prints all messages:
220
221\begin{verbatim}
222import getpass, imaplib, string
223M = imaplib.IMAP4()
224M.LOGIN(getpass.getuser(), getpass.getpass())
225M.SELECT()
226typ, data = M.SEARCH(None, 'ALL')
227for num in string.split(data[0]):
Fred Drakef7ffa921998-05-06 12:42:37 +0000228 typ, data = M.FETCH(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000229 print 'Message %s\n%s\n' % (num, data[0][1])
230M.LOGOUT()
231\end{verbatim}
232
233Note that IMAP4 message numbers change as the mailbox changes, so it
234is highly advisable to use UIDs instead, with the UID command.
235
236At the end of the module, there is a test section that contains a more
237extensive example of usage.
238
239\begin{seealso}
240\seetext{Documents describing the protocol, and sources and binaries
241for servers implementing it, can all be found at the University of
242Washington's \emph{IMAP Information Center}
243(\url{http://www.cac.washington.edu/imap/}).}
244\end{seealso}