imaplib --- IMAP4 protocol client

synopsis: IMAP4 protocol client (requires sockets).

Source code: Lib/imaplib.py

pair: IMAP4; protocol pair: IMAP4_SSL; protocol pair: IMAP4_stream; protocol

This module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 client protocol as defined in 3501. It is backward compatible with IMAP4 (1730) servers, but note that the STATUS command is not supported in IMAP4.

Three classes are provided by the !imaplib module, IMAP4 is the base class

This class implements the actual IMAP4 protocol. The connection is created and protocol version (IMAP4 or IMAP4rev1) is determined when the instance is initialized. If host is not specified, '' (the local host) is used. If port is omitted, the standard IMAP4 port (143) is used. The optional timeout parameter specifies a timeout in seconds for the connection attempt. If timeout is not given or is None, the global default socket timeout is used.

The IMAP4 class supports the with statement. When used like this, the IMAP4 LOGOUT command is issued automatically when the !with statement exits. E.g.

Three exceptions are defined as attributes of the IMAP4 class

Exception raised on any errors. The reason for the exception is passed to the constructor as a string.

IMAP4 server errors cause this exception to be raised. This is a sub-class of IMAP4.error. Note that closing the instance and instantiating a new one will usually allow recovery from this exception.

This exception is raised when a writable mailbox has its status changed by the server. This is a sub-class of IMAP4.error. Some other client now has write permission, and the mailbox will need to be re-opened to re-obtain write permission.

There's also a subclass for secure connections

This is a subclass derived from IMAP4 that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified, '' (the local host) is used. If port is omitted, the standard IMAP4-over-SSL port (993) is used. ssl_context is a ssl.SSLContext object which allows bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure. Please read ssl-security for best practices. …