imaplib.IMAP4¶
-
class
imaplib.IMAP4(host='', port=143)[source]¶ IMAP4 client class.
Instantiate with: IMAP4([host[, port]])
host - host’s name (default: localhost); port - port number (default: standard IMAP4 port).All IMAP4rev1 commands are supported by methods of the same name (in lower-case).
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.
Methods¶
_CRAM_MD5_AUTH(challenge) |
Authobject to use with CRAM-MD5 authentication. |
__getattr__(attr) |
|
__init__([host, port]) |
|
_append_untagged(typ, dat) |
|
_check_bye() |
|
_checkquote(arg) |
|
_command(name, *args) |
|
_command_complete(name, tag) |
|
_dump_ur(dict) |
|
_get_line() |
|
_get_response() |
|
_get_tagged_response(tag) |
|
_log(line) |
|
_match(cre, s) |
|
_mesg(s[, secs]) |
|
_new_tag() |
|
_quote(arg) |
|
_simple_command(name, *args) |
|
_untagged_response(typ, dat, name) |
|
append(mailbox, flags, date_time, message) |
Append message to named mailbox. |
authenticate(mechanism, authobject) |
Authenticate command - requires response processing. |
capability() |
(typ, [data]) = <instance>.capability() |
check() |
Checkpoint mailbox on server. |
close() |
Close currently selected mailbox. |
copy(message_set, new_mailbox) |
Copy ‘message_set’ messages onto end of ‘new_mailbox’. |
create(mailbox) |
Create new mailbox. |
delete(mailbox) |
Delete old mailbox. |
deleteacl(mailbox, who) |
Delete the ACLs (remove any rights) set for who on mailbox. |
expunge() |
Permanently remove deleted items from selected mailbox. |
fetch(message_set, message_parts) |
Fetch (parts of) messages. |
getacl(mailbox) |
Get the ACLs for a mailbox. |
getannotation(mailbox, entry, attribute) |
(typ, [data]) = <instance>.getannotation(mailbox, entry, attribute) |
getquota(root) |
Get the quota root’s resource usage and limits. |
getquotaroot(mailbox) |
Get the list of quota roots for the named mailbox. |
list([directory, pattern]) |
List mailbox names in directory matching pattern. |
login(user, password) |
Identify client using plaintext password. |
login_cram_md5(user, password) |
Force use of CRAM-MD5 authentication. |
logout() |
Shutdown connection to server. |
lsub([directory, pattern]) |
List ‘subscribed’ mailbox names in directory matching pattern. |
myrights(mailbox) |
Show my ACLs for a mailbox (i.e. |
namespace() |
Returns IMAP namespaces ala rfc2342 |
noop() |
Send NOOP command. |
open([host, port]) |
Setup connection to remote server on “host:port” (default: localhost:standard IMAP4 port). |
partial(message_num, message_part, start, length) |
Fetch truncated part of a message. |
print_log() |
|
proxyauth(user) |
Assume authentication as “user”. |
read(size) |
Read ‘size’ bytes from remote. |
readline() |
Read line from remote. |
recent() |
Return most recent ‘RECENT’ responses if any exist, else prompt server for an update using the ‘NOOP’ command. |
rename(oldmailbox, newmailbox) |
Rename old mailbox name to new. |
response(code) |
Return data for response ‘code’ if received, or None. |
search(charset, *criteria) |
Search mailbox for matching messages. |
select([mailbox, readonly]) |
Select a mailbox. |
send(data) |
Send data to remote. |
setacl(mailbox, who, what) |
Set a mailbox acl. |
setannotation(*args) |
(typ, [data]) = <instance>.setannotation(mailbox[, entry, attribute]+) |
setquota(root, limits) |
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(sort_criteria, charset, *search_criteria) |
IMAP4rev1 extension SORT command. |
status(mailbox, names) |
Request named status conditions for mailbox. |
store(message_set, command, flags) |
Alters flag dispositions for messages in mailbox. |
subscribe(mailbox) |
Subscribe to new mailbox. |
thread(threading_algorithm, charset, ...) |
IMAPrev1 extension THREAD command. |
uid(command, *args) |
Execute “command arg ...” with messages identified by UID, rather than message number. |
unsubscribe(mailbox) |
Unsubscribe from old mailbox. |
xatom(name, *args) |
Allow simple extension commands notified by server in CAPABILITY response. |