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