blob: fa3ec68dacfcdd9df71898598eb775131cd02f02 [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}}
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}
Fred Drake7dba8e21998-04-11 13:51:05 +000066\label{imap4-objects}
Fred Drake89de3141998-04-11 04:19:04 +000067
68All IMAP4rev1 commands are represented by methods of the same name,
69either upper-case or lower-case.
70
71Each command returns a tuple: \code{(}\var{type}, \code{[}\var{data},
72...\code{])} where \var{type} is usually \code{'OK'} or \code{'NO'},
73and \var{data} is either the text from the command response, or
74mandated results from the command.
75
76An \class{IMAP4} instance has the following methods:
77
78
79\begin{methoddesc}{append}{mailbox, flags, date_time, message}
80 Append message to named mailbox.
81\end{methoddesc}
82
83\begin{methoddesc}{authenticate}{func}
84 Authenticate command --- requires response processing. This is
85 currently unimplemented, and raises an exception.
86\end{methoddesc}
87
88\begin{methoddesc}{check}{}
89 Checkpoint mailbox on server.
90\end{methoddesc}
91
92\begin{methoddesc}{close}{}
93 Close currently selected mailbox. Deleted messages are removed from
94 writable mailbox. This is the recommended command before
95 \samp{LOGOUT}.
96\end{methoddesc}
97
98\begin{methoddesc}{copy}{message_set, new_mailbox}
99 Copy \var{message_set} messages onto end of \var{new_mailbox}.
100\end{methoddesc}
101
102\begin{methoddesc}{create}{mailbox}
103 Create new mailbox named \var{mailbox}.
104\end{methoddesc}
105
106\begin{methoddesc}{delete}{mailbox}
107 Delete old mailbox named \var{mailbox}.
108\end{methoddesc}
109
110\begin{methoddesc}{expunge}{}
111 Permanently remove deleted items from selected mailbox. Generates an
112 \samp{EXPUNGE} response for each deleted message. Returned data
113 contains a list of \samp{EXPUNGE} message numbers in order
114 received.
115\end{methoddesc}
116
117\begin{methoddesc}{fetch}{message_set, message_parts}
118 Fetch (parts of) messages. Returned data are tuples of message part
119 envelope and data.
120\end{methoddesc}
121
Fred Drakee5cf53a1998-04-11 05:02:45 +0000122\begin{methoddesc}{list}{\optional{directory\optional{, pattern}}}
123 List mailbox names in \var{directory} matching
124 \var{pattern}. \var{directory} defaults to the top-level mail
125 folder, and \var{pattern} defaults to match anything. Returned data
126 contains a list of \samp{LIST} responses.
Fred Drake89de3141998-04-11 04:19:04 +0000127\end{methoddesc}
128
129\begin{methoddesc}{login}{user, password}
130 Identify the client using a plaintext password.
131\end{methoddesc}
132
133\begin{methoddesc}{logout}{}
134 Shutdown connection to server. Returns server \samp{BYE} response.
135\end{methoddesc}
136
Fred Drakee5cf53a1998-04-11 05:02:45 +0000137\begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}}
138 List subscribed mailbox names in directory matching pattern.
139 \var{directory} defaults to the top level directory and
140 \var{pattern} defaults to match any mailbox.
141 Returned data are tuples of message part envelope and data.
Fred Drake89de3141998-04-11 04:19:04 +0000142\end{methoddesc}
143
144\begin{methoddesc}{recent}{}
145 Prompt server for an update. Returned data is \code{None} if no new
146 messages, else value of \samp{RECENT} response.
147\end{methoddesc}
148
149\begin{methoddesc}{rename}{oldmailbox, newmailbox}
150 Rename mailbox named \var{oldmailbox} to \var{newmailbox}.
151\end{methoddesc}
152
153\begin{methoddesc}{response}{code}
154 Return data for response \var{code} if received, or
155 \code{None}. Returns the given code, instead of the usual type.
156\end{methoddesc}
157
158\begin{methoddesc}{search}{charset, criteria}
159 Search mailbox for matching messages. Returned data contains a space
160 separated list of matching message numbers.
161\end{methoddesc}
162
163\begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}}
164 Select a mailbox. Returned data is the count of messages in
165 \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox}
166 is \code{'INBOX'}. If the \var{readonly} flag is set, modifications
167 to the mailbox are not allowed.
168\end{methoddesc}
169
170\begin{methoddesc}{status}{mailbox, names}
171 Request named status conditions for \var{mailbox}.
172\end{methoddesc}
173
174\begin{methoddesc}{store}{message_set, command, flag_list}
175 Alters flag dispositions for messages in mailbox.
176\end{methoddesc}
177
178\begin{methoddesc}{subscribe}{mailbox}
179 Subscribe to new mailbox.
180\end{methoddesc}
181
182\begin{methoddesc}{uid}{command, args}
183 Execute command args with messages identified by UID, rather than
184 message number. Returns response appropriate to command.
185\end{methoddesc}
186
187\begin{methoddesc}{unsubscribe}{mailbox}
188 Unsubscribe from old mailbox.
189\end{methoddesc}
190
191\begin{methoddesc}{xatom}{name\optional{, arg1\optional{, arg2}}}
192 Allow simple extension commands notified by server in
193 \samp{CAPABILITY} response.
194\end{methoddesc}
195
196
Fred Drake8f6b9581998-04-11 15:11:55 +0000197The following attributes are defined on instances of \class{IMAP4}:
Fred Drake89de3141998-04-11 04:19:04 +0000198
Fred Drake8f6b9581998-04-11 15:11:55 +0000199
200\begin{memberdesc}{PROTOCOL_VERSION}
201The most recent supported protocol in the \samp{CAPABILITY}
202response from the server.
203\end{memberdesc}
204
205\begin{memberdesc}{debug}
206Integer value to control debugging output. The initialize value is
207taken from the module variable \code{Debug}. Values greater than
208three trace each command.
209\end{memberdesc}
Fred Drake89de3141998-04-11 04:19:04 +0000210
211
212\subsection{IMAP4 Example}
Fred Drake7dba8e21998-04-11 13:51:05 +0000213\label{imap4-example}
Fred Drake89de3141998-04-11 04:19:04 +0000214
215Here is a minimal example (without error checking) that opens a
216mailbox and retrieves and prints all messages:
217
218\begin{verbatim}
219import getpass, imaplib, string
220M = imaplib.IMAP4()
221M.LOGIN(getpass.getuser(), getpass.getpass())
222M.SELECT()
223typ, data = M.SEARCH(None, 'ALL')
224for num in string.split(data[0]):
Fred Drakef7ffa921998-05-06 12:42:37 +0000225 typ, data = M.FETCH(num, '(RFC822)')
Fred Drake89de3141998-04-11 04:19:04 +0000226 print 'Message %s\n%s\n' % (num, data[0][1])
227M.LOGOUT()
228\end{verbatim}
229
230Note that IMAP4 message numbers change as the mailbox changes, so it
231is highly advisable to use UIDs instead, with the UID command.
232
233At the end of the module, there is a test section that contains a more
234extensive example of usage.
235
236\begin{seealso}
237\seetext{Documents describing the protocol, and sources and binaries
238for servers implementing it, can all be found at the University of
239Washington's \emph{IMAP Information Center}
240(\url{http://www.cac.washington.edu/imap/}).}
241\end{seealso}