| Fred Drake | 2a37455 | 1998-04-11 13:48:26 +0000 | [diff] [blame] | 1 | % Based on HTML documentation by Piers Lauder <piers@staff.cs.usyd.edu.au>; | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 2 | % converted by Fred L. Drake, Jr. <fdrake@acm.org>. | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 3 |  | 
| Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 4 | \section{\module{imaplib} --- | 
| Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 5 | IMAP4 protocol client} | 
|  | 6 |  | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | \declaremodule{standard}{imaplib} | 
| Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 8 | \modulesynopsis{IMAP4 protocol client (requires sockets).} | 
| Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 9 | \moduleauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au} | 
|  | 10 | \sectionauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au} | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 11 |  | 
| Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 12 | \indexii{IMAP4}{protocol} | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 13 |  | 
|  | 14 | This module defines a class, \class{IMAP4}, which encapsulates a | 
|  | 15 | connection to an IMAP4 server and implements the IMAP4rev1 client | 
|  | 16 | protocol as defined in \rfc{2060}. It is backward compatible with | 
|  | 17 | IMAP4 (\rfc{1730}) servers, but note that the \samp{STATUS} command is | 
|  | 18 | not supported in IMAP4. | 
|  | 19 |  | 
| Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 20 | A single class is provided by the \module{imaplib} module: | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | \begin{classdesc}{IMAP4}{\optional{host\optional{, port}}} | 
|  | 23 | This class implements the actual IMAP4 protocol.  The connection is | 
|  | 24 | created and protocol version (IMAP4 or IMAP4rev1) is determined when | 
|  | 25 | the instance is initialized. | 
|  | 26 | If \var{host} is not specified, \code{''} (the local host) is used. | 
|  | 27 | If \var{port} is omitted, the standard IMAP4 port (143) is used. | 
|  | 28 | \end{classdesc} | 
|  | 29 |  | 
|  | 30 | Two exceptions are defined as attributes of the \class{IMAP4} class: | 
|  | 31 |  | 
|  | 32 | \begin{excdesc}{IMAP4.error} | 
|  | 33 | Exception raised on any errors.  The reason for the exception is | 
|  | 34 | passed to the constructor as a string. | 
|  | 35 | \end{excdesc} | 
|  | 36 |  | 
|  | 37 | \begin{excdesc}{IMAP4.abort} | 
|  | 38 | IMAP4 server errors cause this exception to be raised.  This is a | 
|  | 39 | sub-class of \exception{IMAP4.error}.  Note that closing the instance | 
|  | 40 | and instantiating a new one will usually allow recovery from this | 
|  | 41 | exception. | 
|  | 42 | \end{excdesc} | 
|  | 43 |  | 
|  | 44 | The following utility functions are defined: | 
|  | 45 |  | 
|  | 46 | \begin{funcdesc}{Internaldate2tuple}{datestr} | 
|  | 47 | Converts an IMAP4 INTERNALDATE string to Coordinated Universal | 
| Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 48 | Time. Returns a \refmodule{time} module tuple. | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 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} | 
| Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 62 | Converts a \refmodule{time} module tuple to an IMAP4 | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 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 |  | 
| Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 68 | Note that IMAP4 message numbers change as the mailbox changes, so it | 
|  | 69 | is highly advisable to use UIDs instead, with the UID command. | 
|  | 70 |  | 
|  | 71 | At the end of the module, there is a test section that contains a more | 
|  | 72 | extensive example of usage. | 
|  | 73 |  | 
|  | 74 | \begin{seealso} | 
| Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame^] | 75 | \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 Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 79 | \end{seealso} | 
|  | 80 |  | 
|  | 81 |  | 
|  | 82 | \subsection{IMAP4 Objects \label{imap4-objects}} | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 83 |  | 
|  | 84 | All IMAP4rev1 commands are represented by methods of the same name, | 
|  | 85 | either upper-case or lower-case. | 
|  | 86 |  | 
| Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 87 | Each command returns a tuple: \code{(\var{type}, [\var{data}, | 
|  | 88 | ...])} where \var{type} is usually \code{'OK'} or \code{'NO'}, | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 89 | and \var{data} is either the text from the command response, or | 
|  | 90 | mandated results from the command. | 
|  | 91 |  | 
|  | 92 | An \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 Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 138 | \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 Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 143 | \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 Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 153 | \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 Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 158 | \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 Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 213 | The following attributes are defined on instances of \class{IMAP4}: | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 214 |  | 
| Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 215 |  | 
|  | 216 | \begin{memberdesc}{PROTOCOL_VERSION} | 
| Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 217 | The most recent supported protocol in the | 
|  | 218 | \samp{CAPABILITY} response from the server. | 
| Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 219 | \end{memberdesc} | 
|  | 220 |  | 
|  | 221 | \begin{memberdesc}{debug} | 
|  | 222 | Integer value to control debugging output.  The initialize value is | 
|  | 223 | taken from the module variable \code{Debug}.  Values greater than | 
|  | 224 | three trace each command. | 
|  | 225 | \end{memberdesc} | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 226 |  | 
|  | 227 |  | 
| Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 228 | \subsection{IMAP4 Example \label{imap4-example}} | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 229 |  | 
|  | 230 | Here is a minimal example (without error checking) that opens a | 
|  | 231 | mailbox and retrieves and prints all messages: | 
|  | 232 |  | 
|  | 233 | \begin{verbatim} | 
|  | 234 | import getpass, imaplib, string | 
| Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 235 |  | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 236 | M = imaplib.IMAP4() | 
| Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 237 | M.login(getpass.getuser(), getpass.getpass()) | 
|  | 238 | M.select() | 
|  | 239 | typ, data = M.search(None, 'ALL') | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 240 | for num in string.split(data[0]): | 
| Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 241 | typ, data = M.fetch(num, '(RFC822)') | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 242 | print 'Message %s\n%s\n' % (num, data[0][1]) | 
| Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 243 | M.logout() | 
| Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 244 | \end{verbatim} |