PyDocs

Nav

Internet Protocols and Support

IMAP4

imaplib

class (source) not WASI
  • IMAP4(host='', port=IMAP4_PORT, 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)
                  If timeout is not given or is None,
                  the global default socket timeout is used

All IMAP4rev1 commands are supported by methods of the same
name (in lowercase).

All arguments to commands are converted to strings, except for
AUTHENTICATE, and the last argument to APPEND which is passed as
an IMAP4 literal.  If necessary (the string contains any
non-printing characters or white-space and isn't enclosed with
either parentheses or double quotes) each string is quoted.
However, the 'password' argument to the LOGIN command is always
quoted.  If you want to avoid having an argument string quoted
(eg: the 'flags' argument to STORE) then enclose the string in
parentheses (eg: "(\Deleted)").

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 (ie: 'literal' value).

Errors raise the exception class <instance>.error("<reason>").
IMAP4 server errors raise <instance>.abort("<reason>"),
which is a sub-class of 'error'. Mailbox status changes
from READ-WRITE to READ-ONLY raise the exception class
<instance>.readonly("<reason>"), which is a sub-class of 'abort'.

"error" exceptions imply a program error.
"abort" exceptions imply the connection should be reset, and
        the command re-tried.
"readonly" exceptions imply the command should be re-tried.

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.
Changed in 3.5, 3.9
  • 3.5: Support for the with statement was added.
  • 3.9: The optional timeout parameter was added.
Members of IMAP4

a

abort
IMAP4 server errors cause this exception to be raised.
append
Append message to named mailbox.
authenticate
Authenticate command - requires response processing.

c

capabilities
A tuple of the capabilities advertised by the server, in upper case.
check
Checkpoint mailbox on server.
close
Close currently selected mailbox.
copy
Copy 'message_set' messages onto end of 'new_mailbox'.
create
Create new mailbox.

d

debug
Integer value to control debugging output.
delete
Delete old mailbox.
deleteacl
Delete the ACLs (remove any rights) set for who on mailbox.

e

enable
Send an RFC5161 enable string to the server.
error
Exception raised on any errors.
expunge
Permanently remove deleted items from selected mailbox.

f

fetch
Fetch (parts of) messages.
file
Internal BufferedReader associated with the underlying socket.

g

getacl
Get the ACLs for a mailbox.
getannotation
(typ, [data]) = <instance>.getannotation(mailbox, entry, attribute) Retrieve ANNOTATIONs.
getquota
Get the quota root's resource usage and limits.
getquotaroot
Get the list of quota roots for the named mailbox.

i

idle
Return an iterable IDLE context manager producing untagged responses.
Idler.burst
Yield a burst of responses no more than interval seconds apart (expressed as an int or float).

l

list
List mailbox names in directory matching pattern.
login
Identify client using plaintext password.
login_cram_md5
Force use of CRAM-MD5 authentication.
logout
Shutdown connection to server.
lsub
List 'subscribed' mailbox names in directory matching pattern.

m

myrights
Show my ACLs for a mailbox (i.e.

n

namespace
Returns IMAP namespaces ala rfc2342
noop
Send NOOP command.

o

open
Setup connection to remote server on "host:port" (default: localhost:standard IMAP4 port).

p

partial
Fetch truncated part of a message.
PROTOCOL_VERSION
The most recent supported protocol in the CAPABILITY response from the server.
proxyauth
Assume authentication as "user".

r

read
Read 'size' bytes from remote.
readline
Read line from remote.
readonly
This exception is raised when a writable mailbox has its status changed by the server.
recent
Return most recent 'RECENT' responses if any exist, else prompt server for an update using the 'NOOP' command.
rename
Rename old mailbox name to new.
response
Return data for response 'code' if received, or None.

s

search
Search mailbox for matching messages.
select
Select a mailbox.
send
Send data to remote.
setacl
Set a mailbox acl.
setannotation
(typ, [data]) = <instance>.setannotation(mailbox[, entry, attribute]+) Set ANNOTATIONs.
setquota
Set the quota root's resource limits.
shutdown
Close I/O established in "open".
socket
Return socket instance used to connect to IMAP4 server.
sort
IMAP4rev1 extension SORT command.
starttls
Send a STARTTLS command.
status
Request named status conditions for mailbox.
store
Alters flag dispositions for messages in mailbox.
subscribe
Subscribe to new mailbox.

t

thread
IMAPrev1 extension THREAD command.

u

uid
Execute "command arg ..." with messages identified by UID, rather than message number.
unselect
Free server's resources associated with the selected mailbox and returns the server to the authenticated state.
unsubscribe
Unsubscribe from old mailbox.
utf8_enabled
Boolean value that is normally False, but is set to True if an enable() command is successfully issued for the UTF8=ACCEPT capability.

x

xatom
Allow simple extension commands notified by server in CAPABILITY response.
1 Example
>>> from imaplib import IMAP4
>>> with IMAP4("domain.org") as M:
...     M.noop()
...
('OK', [b'Nothing Accomplished. d25if65hy903weo.87'])
from the Python docs
See Also
No see-alsos for imaplib/IMAP4
0 Notes
No notes for IMAP4