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>. |
| 3 | % |
Fred Drake | 2a37455 | 1998-04-11 13:48:26 +0000 | [diff] [blame] | 4 | % The imaplib module was written by Piers Lauder. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 5 | |
| 6 | \section{Standard Module \module{imaplib}} |
| 7 | \stmodindex{imaplib} |
| 8 | \label{module-imaplib} |
Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 9 | \indexii{IMAP4}{protocol} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 10 | |
| 11 | This module defines a class, \class{IMAP4}, which encapsulates a |
| 12 | connection to an IMAP4 server and implements the IMAP4rev1 client |
| 13 | protocol as defined in \rfc{2060}. It is backward compatible with |
| 14 | IMAP4 (\rfc{1730}) servers, but note that the \samp{STATUS} command is |
| 15 | not supported in IMAP4. |
| 16 | |
| 17 | A single class is provided by the \code{imaplib} module: |
| 18 | |
| 19 | \begin{classdesc}{IMAP4}{\optional{host\optional{, port}}} |
| 20 | This class implements the actual IMAP4 protocol. The connection is |
| 21 | created and protocol version (IMAP4 or IMAP4rev1) is determined when |
| 22 | the instance is initialized. |
| 23 | If \var{host} is not specified, \code{''} (the local host) is used. |
| 24 | If \var{port} is omitted, the standard IMAP4 port (143) is used. |
| 25 | \end{classdesc} |
| 26 | |
| 27 | Two exceptions are defined as attributes of the \class{IMAP4} class: |
| 28 | |
| 29 | \begin{excdesc}{IMAP4.error} |
| 30 | Exception raised on any errors. The reason for the exception is |
| 31 | passed to the constructor as a string. |
| 32 | \end{excdesc} |
| 33 | |
| 34 | \begin{excdesc}{IMAP4.abort} |
| 35 | IMAP4 server errors cause this exception to be raised. This is a |
| 36 | sub-class of \exception{IMAP4.error}. Note that closing the instance |
| 37 | and instantiating a new one will usually allow recovery from this |
| 38 | exception. |
| 39 | \end{excdesc} |
| 40 | |
| 41 | The 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 Drake | 7dba8e2 | 1998-04-11 13:51:05 +0000 | [diff] [blame] | 66 | \label{imap4-objects} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 67 | |
| 68 | All IMAP4rev1 commands are represented by methods of the same name, |
| 69 | either upper-case or lower-case. |
| 70 | |
| 71 | Each command returns a tuple: \code{(}\var{type}, \code{[}\var{data}, |
| 72 | ...\code{])} where \var{type} is usually \code{'OK'} or \code{'NO'}, |
| 73 | and \var{data} is either the text from the command response, or |
| 74 | mandated results from the command. |
| 75 | |
| 76 | An \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 Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 122 | \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 Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 127 | \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 Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 137 | \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 Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 142 | \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 Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 197 | The following attributes are defined on instances of \class{IMAP4}: |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 198 | |
Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 199 | |
| 200 | \begin{memberdesc}{PROTOCOL_VERSION} |
| 201 | The most recent supported protocol in the \samp{CAPABILITY} |
| 202 | response from the server. |
| 203 | \end{memberdesc} |
| 204 | |
| 205 | \begin{memberdesc}{debug} |
| 206 | Integer value to control debugging output. The initialize value is |
| 207 | taken from the module variable \code{Debug}. Values greater than |
| 208 | three trace each command. |
| 209 | \end{memberdesc} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | \subsection{IMAP4 Example} |
Fred Drake | 7dba8e2 | 1998-04-11 13:51:05 +0000 | [diff] [blame] | 213 | \label{imap4-example} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 214 | |
| 215 | Here is a minimal example (without error checking) that opens a |
| 216 | mailbox and retrieves and prints all messages: |
| 217 | |
| 218 | \begin{verbatim} |
| 219 | import getpass, imaplib, string |
| 220 | M = imaplib.IMAP4() |
| 221 | M.LOGIN(getpass.getuser(), getpass.getpass()) |
| 222 | M.SELECT() |
| 223 | typ, data = M.SEARCH(None, 'ALL') |
| 224 | for num in string.split(data[0]): |
Fred Drake | f7ffa92 | 1998-05-06 12:42:37 +0000 | [diff] [blame^] | 225 | typ, data = M.FETCH(num, '(RFC822)') |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 226 | print 'Message %s\n%s\n' % (num, data[0][1]) |
| 227 | M.LOGOUT() |
| 228 | \end{verbatim} |
| 229 | |
| 230 | Note that IMAP4 message numbers change as the mailbox changes, so it |
| 231 | is highly advisable to use UIDs instead, with the UID command. |
| 232 | |
| 233 | At the end of the module, there is a test section that contains a more |
| 234 | extensive example of usage. |
| 235 | |
| 236 | \begin{seealso} |
| 237 | \seetext{Documents describing the protocol, and sources and binaries |
| 238 | for servers implementing it, can all be found at the University of |
| 239 | Washington's \emph{IMAP Information Center} |
| 240 | (\url{http://www.cac.washington.edu/imap/}).} |
| 241 | \end{seealso} |