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