imaplib - IMAP4 client.
| Use Case | Command | Description |
|---|---|---|
| π‘ Connect to IMAP server | IMAP4(host='', port=143) | Create an IMAP4 client instance |
| π Login | login(user, password) | Authenticate with plaintext password |
| π Select mailbox | select(mailbox='INBOX', readonly=False) | Select a mailbox for operations |
| π Search messages | search(charset, *criteria) | Search mailbox for matching messages |
| π₯ Fetch messages | fetch(message_set, message_parts) | Fetch parts of specified messages |
| π€ Append message | append(mailbox, flags, date_time, message) | Append message to named mailbox |
| ποΈ Delete messages | store(message_set, '+FLAGS', '(\\Deleted)') | Mark messages for deletion |
| π Expunge | expunge() | Permanently remove deleted messages |
| πͺ Logout | logout() | Shutdown connection to server |
| π SSL connection | IMAP4_SSL(host='', port=993) | IMAP4 over SSL |
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, Time2InternaldateIMAP4(host='', port=143, timeout=None)
IMAP4 client class. Instantiate with: IMAP4([host[, port[, timeout=None]]])
host β hostβs name (default: localhost)port β port number (default: standard IMAP4 port)timeout β socket timeout (default: None, uses global default)All IMAP4rev1 commands are supported by lowerβcase methods. Arguments are converted to strings, except for AUTHENTICATE and the last argument to APPEND. Each command returns a tuple: (type, [data, ...]) where type is usually 'OK' or 'NO'. Errors raise <instance>.error("<reason>"); server errors raise <instance>.abort("<reason>"); mailbox status changes raise <instance>.readonly("<reason>").
__enter__(self)__exit__(self, *args)__getattr__(self, attr)__init__(self, host='', port=143, timeout=None)help(type(self)) for accurate signature.append(self, mailbox, flags, date_time, message)message can be None. Returns (typ, [data]).authenticate(self, mechanism, authobject)mechanism must appear in <instance>.capabilities as AUTH=<mechanism>. authobject is a callable: data = authobject(response) returning bytes or None.capability(self)(typ, [data]).check(self)(typ, [data]).close(self)LOGOUT. Returns (typ, [data]).copy(self, message_set, new_mailbox)message_set messages onto end of new_mailbox. Returns (typ, [data]).create(self, mailbox)(typ, [data]).delete(self, mailbox)(typ, [data]).deleteacl(self, mailbox, who)(typ, [data]).enable(self, capability)(typ, [data]).expunge(self)EXPUNGE response for each deleted message. Returns (typ, [data]) where data is list of message numbers.fetch(self, message_set, message_parts)message_parts should be a string of selected parts enclosed in parentheses, e.g., "(UID BODY[TEXT])". Returns (typ, [data, ...]) where data are tuples of message part envelope and data.getacl(self, mailbox)(typ, [data]).getannotation(self, mailbox, entry, attribute)(typ, [data]).getquota(self, root)(typ, [data]).getquotaroot(self, mailbox)(typ, [[QUOTAROOT responses...], [QUOTA responses]]).list(self, directory='""', pattern='*')(typ, [data]) where data is list of LIST responses.login(self, user, password)password will be quoted. Returns (typ, [data]).login_cram_md5(self, user, password)(typ, [data]).logout(self)BYE response. Returns (typ, [data]).lsub(self, directory='""', pattern='*')(typ, [data, ...]) where data are tuples of message part envelope and data.myrights(self, mailbox)(typ, [data]).namespace(self)(typ, [data, ...]).noop(self)(typ, [data]).open(self, host='', port=143, timeout=None)host:port (default: localhost:standard IMAP4 port). Used by read, readline, send, shutdown.partial(self, message_num, message_part, start, length)(typ, [data, ...]) where data is tuple of message part envelope and data.print_log(self)proxyauth(self, user)user. Allows authorised administrator to proxy into any userβs mailbox. Returns (typ, [data]).read(self, size)size bytes from remote.readline(self)recent(self)RECENT responses if any exist, else prompt server using NOOP. Returns (typ, [data]); data is None if no new messages, else list of RECENT responses.rename(self, oldmailbox, newmailbox)(typ, [data]).response(self, code)code if received, or None. Old value for response code is cleared. Returns (code, [data]).search(self, charset, *criteria)(typ, [data]) where data is spaceβseparated list of matching message numbers. If UTF8 is enabled, charset must be None.select(self, mailbox='INBOX', readonly=False)(typ, [data]) where data is count of messages in mailbox (EXISTS response). Mandated responses: FLAGS, EXISTS, RECENT, UIDVALIDITY.send(self, data)setacl(self, mailbox, who, what)(typ, [data]).setannotation(self, *args)(typ, [data]).setquota(self, root, limits)(typ, [data]).shutdown(self)open.socket(self)socket = <instance>.socket().sort(self, sort_criteria, charset, *search_criteria)(typ, [data]).starttls(self, ssl_context=None)status(self, mailbox, names)(typ, [data]).store(self, message_set, command, flags)(typ, [data]).subscribe(self, mailbox)(typ, [data]).thread(self, threading_algorithm, charset, *search_criteria)(type, [data]).uid(self, command, *args)command.unselect(self)CLOSE but no messages are removed. Returns (typ, [data]).unsubscribe(self, mailbox)(typ, [data]).xatom(self, name, *args)name.__dict__ β dictionary for instance variables (if defined)__weakref__ β list of weak references to the object (if defined)abort = <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. For more documentation see the docstring of the parent class IMAP4.
Method resolution order: IMAP4_SSL, IMAP4, builtins.object
__init__(self, host='', port=993, keyfile=None, certfile=None, ssl_context=None, timeout=None)help(type(self)) for accurate signature.open(self, host='', port=993, timeout=None)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.
Method resolution order: IMAP4_stream, IMAP4, builtins.object
__init__(self, command)help(type(self)) for accurate signature.open(self, host=None, port=None, timeout=None)read, readline, send, shutdown.read(self, size)size bytes from remote.readline(self)send(self, data)shutdown(self)open.All other methods are inherited from IMAP4 (see above).
Int2AP(num)Internaldate2tuple(resp)time.struct_time tuple or None if the string has wrong format.ParseFlags(resp)Time2Internaldate(date_time)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, a time.struct_time, an aware datetime instance, or a double-quoted string (assumed already correct format).__all__ = ['IMAP4', 'IMAP4_stream', 'Internaldate2tuple', 'Int2AP', 'P...]
2.58
/usr/lib/python3.10/imaplib.py
Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 11:13 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format