Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{imaplib} --- |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 2 | IMAP4 protocol client} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{imaplib} |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 5 | \modulesynopsis{IMAP4 protocol client (requires sockets).} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 6 | \moduleauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au} |
| 7 | \sectionauthor{Piers Lauder}{piers@staff.cs.usyd.edu.au} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 8 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 9 | % Based on HTML documentation by Piers Lauder <piers@staff.cs.usyd.edu.au>; |
| 10 | % converted by Fred L. Drake, Jr. <fdrake@acm.org>. |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 11 | % Revised by ESR, January 2000. |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame^] | 12 | % Changes for IMAP4_SSL by Tino Lange <Tino.Lange@isg.de>, March 2002 |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 13 | |
Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 14 | \indexii{IMAP4}{protocol} |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame^] | 15 | \indexii{IMAP4_SSL}{protocol} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 16 | |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame^] | 17 | This module defines two classes, \class{IMAP4} and \class{IMAP4_SSL}, which encapsulate a |
| 18 | connection to an IMAP4 server and implement a large subset of the |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 19 | IMAP4rev1 client protocol as defined in \rfc{2060}. It is backward |
| 20 | compatible with IMAP4 (\rfc{1730}) servers, but note that the |
| 21 | \samp{STATUS} command is not supported in IMAP4. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 22 | |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame^] | 23 | Two classes are provided by the \module{imaplib} module, \class{IMAP4} is the base class: |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 24 | |
| 25 | \begin{classdesc}{IMAP4}{\optional{host\optional{, port}}} |
| 26 | This class implements the actual IMAP4 protocol. The connection is |
| 27 | created and protocol version (IMAP4 or IMAP4rev1) is determined when |
| 28 | the instance is initialized. |
| 29 | If \var{host} is not specified, \code{''} (the local host) is used. |
| 30 | If \var{port} is omitted, the standard IMAP4 port (143) is used. |
| 31 | \end{classdesc} |
| 32 | |
| 33 | Two exceptions are defined as attributes of the \class{IMAP4} class: |
| 34 | |
| 35 | \begin{excdesc}{IMAP4.error} |
| 36 | Exception raised on any errors. The reason for the exception is |
| 37 | passed to the constructor as a string. |
| 38 | \end{excdesc} |
| 39 | |
| 40 | \begin{excdesc}{IMAP4.abort} |
| 41 | IMAP4 server errors cause this exception to be raised. This is a |
| 42 | sub-class of \exception{IMAP4.error}. Note that closing the instance |
| 43 | and instantiating a new one will usually allow recovery from this |
| 44 | exception. |
| 45 | \end{excdesc} |
| 46 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 47 | \begin{excdesc}{IMAP4.readonly} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 48 | This exception is raised when a writable mailbox has its status changed by the server. This is a |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 49 | sub-class of \exception{IMAP4.error}. Some other client now has write permission, |
| 50 | and the mailbox will need to be re-opened to re-obtain write permission. |
| 51 | \end{excdesc} |
| 52 | |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame^] | 53 | There's also a subclass for secure connections: |
| 54 | |
| 55 | \begin{classdesc}{IMAP4_SSL}{\optional{host\optional{, port\optional{, keyfile\optional{, certfile}}}}} |
| 56 | This is a subclass derived from \class{IMAP4} that connects over an SSL encrypted socket |
| 57 | (to use this class you need a socket module that was compiled with SSL support). |
| 58 | If \var{host} is not specified, \code{''} (the local host) is used. |
| 59 | If \var{port} is omitted, the standard IMAP4-over-SSL port (993) is used. |
| 60 | \var{keyfile} and \var{certfile} are also optional - they can contain a PEM formatted |
| 61 | private key and certificate chain file for the SSL connection. |
| 62 | \end{classdesc} |
| 63 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 64 | The following utility functions are defined: |
| 65 | |
| 66 | \begin{funcdesc}{Internaldate2tuple}{datestr} |
| 67 | Converts an IMAP4 INTERNALDATE string to Coordinated Universal |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 68 | Time. Returns a \refmodule{time} module tuple. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 69 | \end{funcdesc} |
| 70 | |
| 71 | \begin{funcdesc}{Int2AP}{num} |
| 72 | Converts an integer into a string representation using characters |
| 73 | from the set [\code{A} .. \code{P}]. |
| 74 | \end{funcdesc} |
| 75 | |
| 76 | \begin{funcdesc}{ParseFlags}{flagstr} |
| 77 | Converts an IMAP4 \samp{FLAGS} response to a tuple of individual |
| 78 | flags. |
| 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{funcdesc}{Time2Internaldate}{date_time} |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 82 | Converts a \refmodule{time} module tuple to an IMAP4 |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 83 | \samp{INTERNALDATE} representation. Returns a string in the form: |
| 84 | \code{"DD-Mmm-YYYY HH:MM:SS +HHMM"} (including double-quotes). |
| 85 | \end{funcdesc} |
| 86 | |
| 87 | |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 88 | Note that IMAP4 message numbers change as the mailbox changes; in |
| 89 | particular, after an \samp{EXPUNGE} command performs deletions the |
| 90 | remaining messages are renumbered. So it is highly advisable to use |
| 91 | UIDs instead, with the UID command. |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 92 | |
| 93 | At the end of the module, there is a test section that contains a more |
| 94 | extensive example of usage. |
| 95 | |
| 96 | \begin{seealso} |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 97 | \seetext{Documents describing the protocol, and sources and binaries |
| 98 | for servers implementing it, can all be found at the |
| 99 | University of Washington's \emph{IMAP Information Center} |
| 100 | (\url{http://www.cac.washington.edu/imap/}).} |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 101 | \end{seealso} |
| 102 | |
| 103 | |
| 104 | \subsection{IMAP4 Objects \label{imap4-objects}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 105 | |
| 106 | All IMAP4rev1 commands are represented by methods of the same name, |
| 107 | either upper-case or lower-case. |
| 108 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 109 | All arguments to commands are converted to strings, except for |
Fred Drake | d16b5ab | 1999-12-13 23:34:42 +0000 | [diff] [blame] | 110 | \samp{AUTHENTICATE}, and the last argument to \samp{APPEND} which is |
| 111 | passed as an IMAP4 literal. If necessary (the string contains IMAP4 |
| 112 | protocol-sensitive characters and isn't enclosed with either |
| 113 | parentheses or double quotes) each string is quoted. However, the |
| 114 | \var{password} argument to the \samp{LOGIN} command is always quoted. |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 115 | If you want to avoid having an argument string quoted |
| 116 | (eg: the \var{flags} argument to \samp{STORE}) then enclose the string in |
| 117 | parentheses (eg: \code{r'(\e Deleted)'}). |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 118 | |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 119 | Each command returns a tuple: \code{(\var{type}, [\var{data}, |
| 120 | ...])} where \var{type} is usually \code{'OK'} or \code{'NO'}, |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 121 | and \var{data} is either the text from the command response, or |
| 122 | mandated results from the command. |
| 123 | |
| 124 | An \class{IMAP4} instance has the following methods: |
| 125 | |
| 126 | |
| 127 | \begin{methoddesc}{append}{mailbox, flags, date_time, message} |
| 128 | Append message to named mailbox. |
| 129 | \end{methoddesc} |
| 130 | |
| 131 | \begin{methoddesc}{authenticate}{func} |
| 132 | Authenticate command --- requires response processing. This is |
| 133 | currently unimplemented, and raises an exception. |
| 134 | \end{methoddesc} |
| 135 | |
| 136 | \begin{methoddesc}{check}{} |
| 137 | Checkpoint mailbox on server. |
| 138 | \end{methoddesc} |
| 139 | |
| 140 | \begin{methoddesc}{close}{} |
| 141 | Close currently selected mailbox. Deleted messages are removed from |
| 142 | writable mailbox. This is the recommended command before |
| 143 | \samp{LOGOUT}. |
| 144 | \end{methoddesc} |
| 145 | |
| 146 | \begin{methoddesc}{copy}{message_set, new_mailbox} |
| 147 | Copy \var{message_set} messages onto end of \var{new_mailbox}. |
| 148 | \end{methoddesc} |
| 149 | |
| 150 | \begin{methoddesc}{create}{mailbox} |
| 151 | Create new mailbox named \var{mailbox}. |
| 152 | \end{methoddesc} |
| 153 | |
| 154 | \begin{methoddesc}{delete}{mailbox} |
| 155 | Delete old mailbox named \var{mailbox}. |
| 156 | \end{methoddesc} |
| 157 | |
| 158 | \begin{methoddesc}{expunge}{} |
| 159 | Permanently remove deleted items from selected mailbox. Generates an |
| 160 | \samp{EXPUNGE} response for each deleted message. Returned data |
| 161 | contains a list of \samp{EXPUNGE} message numbers in order |
| 162 | received. |
| 163 | \end{methoddesc} |
| 164 | |
| 165 | \begin{methoddesc}{fetch}{message_set, message_parts} |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 166 | Fetch (parts of) messages. \var{message_parts} should be |
| 167 | a string of message part names enclosed within parentheses, |
| 168 | eg: \samp{"(UID BODY[TEXT])"}. Returned data are tuples |
| 169 | of message part envelope and data. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 170 | \end{methoddesc} |
| 171 | |
Piers Lauder | a3a1668 | 2001-07-20 11:04:19 +0000 | [diff] [blame] | 172 | \begin{methoddesc}{getacl}{mailbox} |
| 173 | Get the \samp{ACL}s for \var{mailbox}. |
| 174 | The method is non-standard, but is supported by the \samp{Cyrus} server. |
| 175 | \end{methoddesc} |
| 176 | |
Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 177 | \begin{methoddesc}{list}{\optional{directory\optional{, pattern}}} |
| 178 | List mailbox names in \var{directory} matching |
| 179 | \var{pattern}. \var{directory} defaults to the top-level mail |
| 180 | folder, and \var{pattern} defaults to match anything. Returned data |
| 181 | contains a list of \samp{LIST} responses. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 182 | \end{methoddesc} |
| 183 | |
| 184 | \begin{methoddesc}{login}{user, password} |
| 185 | Identify the client using a plaintext password. |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 186 | The \var{password} will be quoted. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 187 | \end{methoddesc} |
| 188 | |
| 189 | \begin{methoddesc}{logout}{} |
| 190 | Shutdown connection to server. Returns server \samp{BYE} response. |
| 191 | \end{methoddesc} |
| 192 | |
Fred Drake | e5cf53a | 1998-04-11 05:02:45 +0000 | [diff] [blame] | 193 | \begin{methoddesc}{lsub}{\optional{directory\optional{, pattern}}} |
| 194 | List subscribed mailbox names in directory matching pattern. |
| 195 | \var{directory} defaults to the top level directory and |
| 196 | \var{pattern} defaults to match any mailbox. |
| 197 | Returned data are tuples of message part envelope and data. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 198 | \end{methoddesc} |
| 199 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 200 | \begin{methoddesc}{noop}{} |
Fred Drake | d16b5ab | 1999-12-13 23:34:42 +0000 | [diff] [blame] | 201 | Send \samp{NOOP} to server. |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 202 | \end{methoddesc} |
| 203 | |
| 204 | \begin{methoddesc}{open}{host, port} |
| 205 | Opens socket to \var{port} at \var{host}. |
Piers Lauder | a3a1668 | 2001-07-20 11:04:19 +0000 | [diff] [blame] | 206 | The connection objects established by this method |
| 207 | will be used in the \code{read}, \code{readline}, and \code{shutdown} methods. |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 208 | You may override this method. |
| 209 | \end{methoddesc} |
| 210 | |
| 211 | \begin{methoddesc}{partial}{message_num, message_part, start, length} |
| 212 | Fetch truncated part of a message. |
| 213 | Returned data is a tuple of message part envelope and data. |
| 214 | \end{methoddesc} |
| 215 | |
Piers Lauder | a3a1668 | 2001-07-20 11:04:19 +0000 | [diff] [blame] | 216 | \begin{methoddesc}{read}{size} |
| 217 | Reads \var{size} bytes from the remote server. |
| 218 | You may override this method. |
| 219 | \end{methoddesc} |
| 220 | |
| 221 | \begin{methoddesc}{readline}{} |
| 222 | Reads one line from the remote server. |
| 223 | You may override this method. |
| 224 | \end{methoddesc} |
| 225 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 226 | \begin{methoddesc}{recent}{} |
| 227 | Prompt server for an update. Returned data is \code{None} if no new |
| 228 | messages, else value of \samp{RECENT} response. |
| 229 | \end{methoddesc} |
| 230 | |
| 231 | \begin{methoddesc}{rename}{oldmailbox, newmailbox} |
| 232 | Rename mailbox named \var{oldmailbox} to \var{newmailbox}. |
| 233 | \end{methoddesc} |
| 234 | |
| 235 | \begin{methoddesc}{response}{code} |
| 236 | Return data for response \var{code} if received, or |
| 237 | \code{None}. Returns the given code, instead of the usual type. |
| 238 | \end{methoddesc} |
| 239 | |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 240 | \begin{methoddesc}{search}{charset, criterium\optional{, ...}} |
| 241 | Search mailbox for matching messages. Returned data contains a space |
| 242 | separated list of matching message numbers. \var{charset} may be |
| 243 | \code{None}, in which case no \samp{CHARSET} will be specified in the |
| 244 | request to the server. The IMAP protocol requires that at least one |
| 245 | criterium be specified; an exception will be raised when the server |
| 246 | returns an error. |
| 247 | |
| 248 | Example: |
| 249 | |
| 250 | \begin{verbatim} |
| 251 | # M is a connected IMAP4 instance... |
| 252 | msgnums = M.search(None, 'FROM', '"LDJ"') |
| 253 | |
| 254 | # or: |
| 255 | msgnums = M.search(None, '(FROM "LDJ")') |
| 256 | \end{verbatim} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 257 | \end{methoddesc} |
| 258 | |
| 259 | \begin{methoddesc}{select}{\optional{mailbox\optional{, readonly}}} |
| 260 | Select a mailbox. Returned data is the count of messages in |
| 261 | \var{mailbox} (\samp{EXISTS} response). The default \var{mailbox} |
| 262 | is \code{'INBOX'}. If the \var{readonly} flag is set, modifications |
| 263 | to the mailbox are not allowed. |
| 264 | \end{methoddesc} |
| 265 | |
Piers Lauder | a3a1668 | 2001-07-20 11:04:19 +0000 | [diff] [blame] | 266 | \begin{methoddesc}{setacl}{mailbox, who, what} |
| 267 | Set an \samp{ACL} for \var{mailbox}. |
| 268 | The method is non-standard, but is supported by the \samp{Cyrus} server. |
| 269 | \end{methoddesc} |
| 270 | |
| 271 | \begin{methoddesc}{shutdown}{} |
| 272 | Close connection established in \code{open}. |
| 273 | You may override this method. |
| 274 | \end{methoddesc} |
| 275 | |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 276 | \begin{methoddesc}{socket}{} |
Piers Lauder | a3a1668 | 2001-07-20 11:04:19 +0000 | [diff] [blame] | 277 | Returns socket instance used to connect to server. |
| 278 | \end{methoddesc} |
| 279 | |
| 280 | \begin{methoddesc}{sort}{sort_criteria, charset, search_criterium\optional{, ...}} |
| 281 | The \code{sort} command is a variant of \code{search} with sorting semantics for |
| 282 | the results. Returned data contains a space |
| 283 | separated list of matching message numbers. |
| 284 | |
| 285 | Sort has two arguments before the \var{search_criterium} |
| 286 | argument(s); a parenthesized list of \var{sort_criteria}, and the searching \var{charset}. |
| 287 | Note that unlike \code{search}, the searching \var{charset} argument is mandatory. |
| 288 | There is also a \code{uid sort} command which corresponds to \code{sort} the way |
| 289 | that \code{uid search} corresponds to \code{search}. |
| 290 | The \code{sort} command first searches the mailbox for messages that |
| 291 | match the given searching criteria using the charset argument for |
| 292 | the interpretation of strings in the searching criteria. It then |
| 293 | returns the numbers of matching messages. |
| 294 | |
| 295 | This is an \samp{IMAP4rev1} extension command. |
Guido van Rossum | 5f7a28c | 1999-12-13 23:29:39 +0000 | [diff] [blame] | 296 | \end{methoddesc} |
| 297 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 298 | \begin{methoddesc}{status}{mailbox, names} |
| 299 | Request named status conditions for \var{mailbox}. |
| 300 | \end{methoddesc} |
| 301 | |
| 302 | \begin{methoddesc}{store}{message_set, command, flag_list} |
| 303 | Alters flag dispositions for messages in mailbox. |
| 304 | \end{methoddesc} |
| 305 | |
| 306 | \begin{methoddesc}{subscribe}{mailbox} |
| 307 | Subscribe to new mailbox. |
| 308 | \end{methoddesc} |
| 309 | |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 310 | \begin{methoddesc}{uid}{command, arg\optional{, ...}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 311 | Execute command args with messages identified by UID, rather than |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 312 | message number. Returns response appropriate to command. At least |
| 313 | one argument must be supplied; if none are provided, the server will |
| 314 | return an error and an exception will be raised. |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 315 | \end{methoddesc} |
| 316 | |
| 317 | \begin{methoddesc}{unsubscribe}{mailbox} |
| 318 | Unsubscribe from old mailbox. |
| 319 | \end{methoddesc} |
| 320 | |
Fred Drake | 99d707a | 2000-05-26 04:08:37 +0000 | [diff] [blame] | 321 | \begin{methoddesc}{xatom}{name\optional{, arg\optional{, ...}}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 322 | Allow simple extension commands notified by server in |
| 323 | \samp{CAPABILITY} response. |
| 324 | \end{methoddesc} |
| 325 | |
| 326 | |
Piers Lauder | a4f8313 | 2002-03-08 01:53:24 +0000 | [diff] [blame^] | 327 | Instances of \class{IMAP4_SSL} have just one additional method: |
| 328 | |
| 329 | \begin{methoddesc}{ssl}{} |
| 330 | Returns SSLObject instance used for the secure connection with the server. |
| 331 | \end{methoddesc} |
| 332 | |
| 333 | |
Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 334 | The following attributes are defined on instances of \class{IMAP4}: |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 335 | |
Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 336 | |
| 337 | \begin{memberdesc}{PROTOCOL_VERSION} |
Fred Drake | b774550 | 1999-04-22 16:46:18 +0000 | [diff] [blame] | 338 | The most recent supported protocol in the |
| 339 | \samp{CAPABILITY} response from the server. |
Fred Drake | 8f6b958 | 1998-04-11 15:11:55 +0000 | [diff] [blame] | 340 | \end{memberdesc} |
| 341 | |
| 342 | \begin{memberdesc}{debug} |
| 343 | Integer value to control debugging output. The initialize value is |
| 344 | taken from the module variable \code{Debug}. Values greater than |
| 345 | three trace each command. |
| 346 | \end{memberdesc} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 347 | |
| 348 | |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 349 | \subsection{IMAP4 Example \label{imap4-example}} |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 350 | |
| 351 | Here is a minimal example (without error checking) that opens a |
| 352 | mailbox and retrieves and prints all messages: |
| 353 | |
| 354 | \begin{verbatim} |
Piers Lauder | a3a1668 | 2001-07-20 11:04:19 +0000 | [diff] [blame] | 355 | import getpass, imaplib |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 356 | |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 357 | M = imaplib.IMAP4() |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 358 | M.login(getpass.getuser(), getpass.getpass()) |
| 359 | M.select() |
| 360 | typ, data = M.search(None, 'ALL') |
Piers Lauder | a3a1668 | 2001-07-20 11:04:19 +0000 | [diff] [blame] | 361 | for num in data[0].split(): |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 362 | typ, data = M.fetch(num, '(RFC822)') |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 363 | print 'Message %s\n%s\n' % (num, data[0][1]) |
Fred Drake | 363d67c | 1999-07-07 13:42:56 +0000 | [diff] [blame] | 364 | M.logout() |
Fred Drake | 89de314 | 1998-04-11 04:19:04 +0000 | [diff] [blame] | 365 | \end{verbatim} |