imaplib - IMAP4 client.
| Use Case | Command | Description |
|---|---|---|
| ๐ Connect to IMAP server | IMAP4(host, port) | Instantiate an IMAP4 client object |
| ๐ Login | M.login(user, password) | Authenticate with plaintext password |
| ๐ List mailboxes | M.list(directory, pattern) | List mailbox names matching pattern |
| ๐ Select mailbox | M.select(mailbox, readonly) | Select a mailbox (default INBOX) |
| ๐ Search messages | M.search(charset, *criteria) | Search for messages matching criteria |
| ๐ฅ Fetch messages | M.fetch(message_set, parts) | Fetch parts of messages |
| ๐ช Logout | M.logout() | Shutdown connection to server |
https://docs.python.org/3.10/library/imaplib.html
The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.
Based on RFC 2060.
IMAP4DebugInternaldate2tuple, Int2AP, ParseFlags, Time2Internaldatebuiltins.object
IMAP4
IMAP4_SSL
IMAP4_stream
IMAP4(host='', port=143, timeout=None)
๐ IMAP4 client class.
Instantiate with: IMAP4([host[, port[, timeout=None]]])
All IMAP4rev1 commands are supported by methods of the same name (in lower-case).
Each command returns a tuple: (type, [data, ...]) where 'type' is usually 'OK' or 'NO', and 'data' is either the text from the tagged response, or untagged results from command. Each 'data' is either a string, or a tuple. If a tuple, then the first part is the header of the response, and the second part contains the data (i.e., 'literal' value).
Errors raise the exception class .error(""). IMAP4 server errors raise .abort(""), which is a sub-class of 'error'. Mailbox status changes from READ-WRITE to READ-ONLY raise the exception class .readonly(""), which is a sub-class of 'abort'.
Note: to use this module, you must read the RFCs pertaining to the IMAP4 protocol, as the semantics of the arguments to each IMAP4 command are left to the invoker, not to mention the results. Also, most IMAP servers implement a sub-set of the commands available here.
__enter__(self)__exit__(self, *args)__getattr__(self, attr)__init__(self, host='', port=143, timeout=None) โ Initialize self.append(self, mailbox, flags, date_time, message) โ ๐ Append message to named mailbox. (typ, [data]) = .append(mailbox, flags, date_time, message). All args except message can be None.authenticate(self, mechanism, authobject) โ ๐ Authenticate command - requires response processing. 'mechanism' must appear in .capabilities as AUTH=. 'authobject' must be a callable: data = authobject(response).capability(self) โ ๐ Fetch capabilities list from server. (typ, [data]) = .capability()check(self) โ โ
Checkpoint mailbox on server. (typ, [data]) = .check()close(self) โ โ Close currently selected mailbox. Deleted messages are removed from writable mailbox. This is the recommended command before 'LOGOUT'. (typ, [data]) = .close()copy(self, message_set, new_mailbox) โ ๐ Copy 'message_set' messages onto end of 'new_mailbox'. (typ, [data]) = .copy(message_set, new_mailbox)create(self, mailbox) โ ๐ Create new mailbox. (typ, [data]) = .create(mailbox)delete(self, mailbox) โ ๐๏ธ Delete old mailbox. (typ, [data]) = .delete(mailbox)deleteacl(self, mailbox, who) โ ๐ซ Delete the ACLs (remove any rights) set for who on mailbox. (typ, [data]) = .deleteacl(mailbox, who)enable(self, capability) โ ๐ก Send an RFC5161 enable string to the server. (typ, [data]) = .enable(capability)expunge(self) โ ๐งน Permanently remove deleted items from selected mailbox. Generates 'EXPUNGE' response for each deleted message. (typ, [data]) = .expunge(). 'data' is list of message numbers.fetch(self, message_set, message_parts) โ ๐ฅ Fetch (parts of) messages. (typ, [data, ...]) = .fetch(message_set, message_parts). 'message_parts' should be a string like "(UID BODY[TEXT])".getacl(self, mailbox) โ ๐ Get the ACLs for a mailbox. (typ, [data]) = .getacl(mailbox)getannotation(self, mailbox, entry, attribute) โ ๐ท๏ธ Retrieve ANNOTATIONs. (typ, [data]) = .getannotation(mailbox, entry, attribute)getquota(self, root) โ ๐ Get the quota root's resource usage and limits (RFC2087). (typ, [data]) = .getquota(root)getquotaroot(self, mailbox) โ ๐ Get the list of quota roots for the named mailbox. (typ, [[QUOTAROOT responses...], [QUOTA responses]]) = .getquotaroot(mailbox)list(self, directory='""', pattern='*') โ ๐ List mailbox names in directory matching pattern. (typ, [data]) = .list(directory='""', pattern='*'). 'data' is list of LIST responses.login(self, user, password) โ ๐ Identify client using plaintext password. (typ, [data]) = .login(user, password). NB: 'password' will be quoted.login_cram_md5(self, user, password) โ ๐ Force use of CRAM-MD5 authentication. (typ, [data]) = .login_cram_md5(user, password)logout(self) โ ๐ช Shutdown connection to server. Returns server 'BYE' response. (typ, [data]) = .logout()lsub(self, directory='""', pattern='*') โ ๐ List 'subscribed' mailbox names in directory matching pattern. (typ, [data, ...]) = .lsub(directory='""', pattern='*')myrights(self, mailbox) โ ๐ Show my ACLs for a mailbox. (typ, [data]) = .myrights(mailbox)namespace(self) โ ๐ Returns IMAP namespaces (RFC2342). (typ, [data, ...]) = .namespace()noop(self) โ ๐ Send NOOP command. (typ, [data]) = .noop()open(self, host='', port=143, timeout=None) โ ๐ Setup connection to remote server. Used by: read, readline, send, shutdown.partial(self, message_num, message_part, start, length) โ โ๏ธ Fetch truncated part of a message. (typ, [data, ...]) = .partial(message_num, message_part, start, length)print_log(self)proxyauth(self, user) โ ๐ค Assume authentication as "user". Allows an authorised administrator to proxy into any user's mailbox. (typ, [data]) = .proxyauth(user)read(self, size) โ ๐ Read 'size' bytes from remote.readline(self) โ ๐ Read line from remote.recent(self) โ ๐ Return most recent 'RECENT' responses if any exist, else prompt server using 'NOOP'. (typ, [data]) = .recent()rename(self, oldmailbox, newmailbox) โ โ๏ธ Rename old mailbox name to new. (typ, [data]) = .rename(oldmailbox, newmailbox)response(self, code) โ ๐จ Return data for response 'code' if received, or None. Old value cleared. (code, [data]) = .response(code)search(self, charset, *criteria) โ ๐ Search mailbox for matching messages. (typ, [data]) = .search(charset, criterion, ...). 'data' is space separated list of matching message numbers. If UTF8 is enabled, charset MUST be None.select(self, mailbox='INBOX', readonly=False) โ ๐ Select a mailbox. Flush all untagged responses. (typ, [data]) = .select(mailbox='INBOX', readonly=False). 'data' is count of messages in mailbox ('EXISTS' response). Mandated responses: ('FLAGS', 'EXISTS', 'RECENT', 'UIDVALIDITY').send(self, data) โ ๐ค Send data to remote.setacl(self, mailbox, who, what) โ ๐ Set a mailbox ACL. (typ, [data]) = .setacl(mailbox, who, what)setannotation(self, *args) โ ๐ท๏ธ Set ANNOTATIONs. (typ, [data]) = .setannotation(mailbox[, entry, attribute]+)setquota(self, root, limits) โ ๐ Set the quota root's resource limits. (typ, [data]) = .setquota(root, limits)shutdown(self) โ ๐ Close I/O established in "open".socket(self) โ ๐ Return socket instance used to connect. socket = .socket()sort(self, sort_criteria, charset, *search_criteria) โ ๐ข IMAP4rev1 extension SORT command. (typ, [data]) = .sort(sort_criteria, charset, search_criteria, ...)starttls(self, ssl_context=None) โ ๐status(self, mailbox, names) โ ๐ Request named status conditions for mailbox. (typ, [data]) = .status(mailbox, names)store(self, message_set, command, flags) โ ๐ท๏ธ Alters flag dispositions for messages. (typ, [data]) = .store(message_set, command, flags)subscribe(self, mailbox) โ ๐ Subscribe to new mailbox. (typ, [data]) = .subscribe(mailbox)thread(self, threading_algorithm, charset, *search_criteria) โ ๐งต IMAPrev1 extension THREAD command. (type, [data]) = .thread(threading_algorithm, charset, search_criteria, ...)uid(self, command, *args) โ ๐ Execute command with messages identified by UID. (typ, [data]) = .uid(command, arg1, arg2, ...)unselect(self) โ ๐ Free server's resources associated with the selected mailbox and return to authenticated state. No messages removed. (typ, [data]) = .unselect()unsubscribe(self, mailbox) โ ๐ซ Unsubscribe from old mailbox. (typ, [data]) = .unsubscribe(mailbox)xatom(self, name, *args) โ ๐ง Allow simple extension commands. (typ, [data]) = .xatom(name, arg, ...)__dict__ โ dictionary for instance variables__weakref__ โ list of weak referencesabort = <class 'imaplib.IMAP4.abort'>error = <class 'imaplib.IMAP4.error'>readonly = <class 'imaplib.IMAP4.readonly'>IMAP4_SSL(host='', port=993, keyfile=None, certfile=None, ssl_context=None, timeout=None)
๐ IMAP4 client class over SSL connection.
Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile[, ssl_context[, timeout=None]]]]]])
For more documentation see the docstring of the parent class IMAP4.
__init__(self, host='', port=993, keyfile=None, certfile=None, ssl_context=None, timeout=None)open(self, host='', port=993, timeout=None) โ ๐ Setup connection to remote server on "host:port" (default: localhost:standard IMAP4 SSL port). Used by: read, readline, send, shutdown.All other methods are inherited from IMAP4 (see above).
IMAP4_stream(command)
๐ก IMAP4 client class over a stream.
Instantiate with: IMAP4_stream(command) where "command" is a string that can be passed to subprocess.Popen().
For more documentation see the docstring of the parent class IMAP4.
__init__(self, command)open(self, host=None, port=None, timeout=None) โ ๐ Setup a stream connection. Used by: read, readline, send, shutdown.read(self, size) โ ๐ Read 'size' bytes from remote.readline(self) โ ๐ Read line from remote.send(self, data) โ ๐ค Send data to remote.shutdown(self) โ ๐ Close I/O established in "open".All other methods are inherited from IMAP4 (see above).
Int2AP(num) โ ๐ข Convert integer to A-P string representation.Internaldate2tuple(resp) โ ๐ Parse an IMAP4 INTERNALDATE string. Return corresponding local time as a time.struct_time tuple or None if the string has wrong format.ParseFlags(resp) โ ๐ท๏ธ Convert IMAP4 flags response to python tuple.Time2Internaldate(date_time) โ ๐ Convert date_time to IMAP4 INTERNALDATE representation. Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"'. The date_time argument can be a number (int or float) representing seconds since epoch, a 9-tuple representing local time, an instance of time.struct_time, an aware datetime instance, or a double-quoted string (already in correct format).__all__ = ['IMAP4', 'IMAP4_stream', 'Internaldate2tuple', 'Int2AP', 'P...
2.58
/usr/lib/python3.10/imaplib.py
Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-01 16:57 @216.73.216.239
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)