blob: c275a6ec29cf58d589f8a70d18768d3587d978be [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} ---
Fred Drakeb7745501999-04-22 16:46:18 +00005 IMAP4 protocol client}
6
Fred Drakeb91e9341998-07-23 17:59:49 +00007\declaremodule{standard}{imaplib}
Fred Drakeb7745501999-04-22 16:46:18 +00008\modulesynopsis{IMAP4 protocol client (requires sockets).}
Fred Drake295da241998-08-10 19:42:37 +00009\moduleauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au}
10\sectionauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au}
Fred Drakeb91e9341998-07-23 17:59:49 +000011
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
44The following utility functions are defined:
45
46\begin{funcdesc}{Internaldate2tuple}{datestr}
47 Converts an IMAP4 INTERNALDATE string to Coordinated Universal
Fred Drakeb7745501999-04-22 16:46:18 +000048 Time. Returns a \refmodule{time} module tuple.
Fred Drake89de3141998-04-11 04:19:04 +000049\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}
Fred Drakeb7745501999-04-22 16:46:18 +000062 Converts a \refmodule{time} module tuple to an IMAP4
Fred Drake89de3141998-04-11 04:19:04 +000063 \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
Fred Drake363d67c1999-07-07 13:42:56 +000068Note that IMAP4 message numbers change as the mailbox changes, so it
69is highly advisable to use UIDs instead, with the UID command.
70
71At the end of the module, there is a test section that contains a more
72extensive example of usage.
73
74\begin{seealso}
Fred Drake37f15741999-11-10 16:21:37 +000075 \seetext{Documents describing the protocol, and sources and binaries
76 for servers implementing it, can all be found at the
77 University of Washington's \emph{IMAP Information Center}
78 (\url{http://www.cac.washington.edu/imap/}).}
Fred Drake363d67c1999-07-07 13:42:56 +000079\end{seealso}
80
81
82\subsection{IMAP4 Objects \label{imap4-objects}}
Fred Drake89de3141998-04-11 04:19:04 +000083
84All IMAP4rev1 commands are represented by methods of the same name,
85either upper-case or lower-case.
86
Fred Drakeb7745501999-04-22 16:46:18 +000087Each command returns a tuple: \code{(\var{type}, [\var{data},
88...])} where \var{type} is usually \code{'OK'} or \code{'NO'},
Fred Drake89de3141998-04-11 04:19:04 +000089and \var{data} is either the text from the command response, or
90mandated results from the command.
91
92An \class{IMAP4} instance has the following methods:
93
94
95\begin{methoddesc}{append}{mailbox, flags, date_time, message}
96 Append message to named mailbox.
97\end{methoddesc}
98
99\begin{methoddesc}{authenticate}{func}
100 Authenticate command --- requires response processing. This is
101 currently unimplemented, and raises an exception.
102\end{methoddesc}
103
104\begin{methoddesc}{check}{}
105 Checkpoint mailbox on server.
106\end{methoddesc}
107
108\begin{methoddesc}{close}{}
109 Close currently selected mailbox. Deleted messages are removed from
110 writable mailbox. This is the recommended command before
111 \samp{LOGOUT}.
112\end{methoddesc}
113
114\begin{methoddesc}{copy}{message_set, new_mailbox}
115 Copy \var{message_set} messages onto end of \var{new_mailbox}.
116\end{methoddesc}
117
118\begin{methoddesc}{create}{mailbox}
119 Create new mailbox named \var{mailbox}.
120\end{methoddesc}
121
122\begin{methoddesc}{delete}{mailbox}
123 Delete old mailbox named \var{mailbox}.
124\end{methoddesc}
125
126\begin{methoddesc}{expunge}{}
127 Permanently remove deleted items from selected mailbox. Generates an
128 \samp{EXPUNGE} response for each deleted message. Returned data
129 contains a list of \samp{EXPUNGE} message numbers in order
130 received.
131\end{methoddesc}
132
133\begin{methoddesc}{fetch}{message_set, message_parts}
134 Fetch (parts of) messages. Returned data are tuples of message part
135 envelope and data.
136\end{methoddesc}
137
Fred Drakee5cf53a1998-04-11 05:02:45 +0000138\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
139 List mailbox names in \var{directory} matching
140 \var{pattern}. \var{directory} defaults to the top-level mail
141 folder, and \var{pattern} defaults to match anything. Returned data
142 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000143\end{methoddesc}
144
145\begin{methoddesc}{login}{user, password}
146 Identify the client using a plaintext password.
147\end{methoddesc}
148
149\begin{methoddesc}{logout}{}
150 Shutdown connection to server. Returns server \samp{BYE} response.
151\end{methoddesc}
152
Fred Drakee5cf53a1998-04-11 05:02:45 +0000153\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
154 List subscribed mailbox names in directory matching pattern.
155 \var{directory} defaults to the top level directory and
156 \var{pattern} defaults to match any mailbox.
157 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000158\end{methoddesc}
159
160\begin{methoddesc}{recent}{}
161 Prompt server for an update. Returned data is \code{None} if no new
162 messages, else value of \samp{RECENT} response.
163\end{methoddesc}
164
165\begin{methoddesc}{rename}{oldmailbox, newmailbox}
166 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
167\end{methoddesc}
168
169\begin{methoddesc}{response}{code}
170 Return data for response \var{code} if received, or
171 \code{None}. Returns the given code, instead of the usual type.
172\end{methoddesc}
173
174\begin{methoddesc}{search}{charset, criteria}
175 Search mailbox for matching messages. Returned data contains a space
176 separated list of matching message numbers.
177\end{methoddesc}
178
179\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
180 Select a mailbox. Returned data is the count of messages in
181 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
182 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
183 to the mailbox are not allowed.
184\end{methoddesc}
185
186\begin{methoddesc}{status}{mailbox, names}
187 Request named status conditions for \var{mailbox}.
188\end{methoddesc}
189
190\begin{methoddesc}{store}{message_set, command, flag_list}
191 Alters flag dispositions for messages in mailbox.
192\end{methoddesc}
193
194\begin{methoddesc}{subscribe}{mailbox}
195 Subscribe to new mailbox.
196\end{methoddesc}
197
198\begin{methoddesc}{uid}{command, args}
199 Execute command args with messages identified by UID, rather than
200 message number. Returns response appropriate to command.
201\end{methoddesc}
202
203\begin{methoddesc}{unsubscribe}{mailbox}
204 Unsubscribe from old mailbox.
205\end{methoddesc}
206
207\begin{methoddesc}{xatom}{name\optional{, arg1\optional{, arg2}}}
208 Allow simple extension commands notified by server in
209 \samp{CAPABILITY} response.
210\end{methoddesc}
211
212
Fred Drake8f6b9581998-04-11 15:11:55 +0000213The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000214
Fred Drake8f6b9581998-04-11 15:11:55 +0000215
216\begin{memberdesc}{PROTOCOL_VERSION}
Fred Drakeb7745501999-04-22 16:46:18 +0000217The most recent supported protocol in the
218\samp{CAPABILITY} response from the server.
Fred Drake8f6b9581998-04-11 15:11:55 +0000219\end{memberdesc}
220
221\begin{memberdesc}{debug}
222Integer value to control debugging output. The initialize value is
223taken from the module variable \code{Debug}. Values greater than
224three trace each command.
225\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000226
227
Fred Drake363d67c1999-07-07 13:42:56 +0000228\subsection{IMAP4 Example \label{imap4-example}}
Fred Drake89de3141998-04-11 04:19:04 +0000229
230Here is a minimal example (without error checking) that opens a
231mailbox and retrieves and prints all messages:
232
233\begin{verbatim}
234import getpass, imaplib, string
Fred Drake363d67c1999-07-07 13:42:56 +0000235
Fred Drake89de3141998-04-11 04:19:04 +0000236M = imaplib.IMAP4()
Fred Drake363d67c1999-07-07 13:42:56 +0000237M.login(getpass.getuser(), getpass.getpass())
238M.select()
239typ, data = M.search(None, 'ALL')
Fred Drake89de3141998-04-11 04:19:04 +0000240for num in string.split(data[0]):
Fred Drake363d67c1999-07-07 13:42:56 +0000241 typ, data = M.fetch(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000242 print 'Message %s\n%s\n' % (num, data[0][1])
Fred Drake363d67c1999-07-07 13:42:56 +0000243M.logout()
Fred Drake89de3141998-04-11 04:19:04 +0000244\end{verbatim}