Erle Robotics Python Networking Gitbook Free

Understanding IMAP in Python

The Python Standard Library contains an IMAP client interface named imaplib, which does offer rudimentary access to the protocol. Unfortunately, it limits itself to knowing how to send requests and deliver their responses back to your code. It makes no attempt to actually implement the detailed rules in the IMAP specification for parsing the returned data.

As an example of how values returned from imaplib are usually too raw to be usefully used in a program, take a look at open_imaplib.py. It is a simple script that uses imaplib to connect to an IMAP account, list the “capabilities” that the server advertises, and then display the status code and data returned by the LIST command.

import getpass, sys
from imapclient import IMAPClient

try:
    hostname, username = sys.argv[1:]
except ValueError:
    print 'usage: %s hostname username' % sys.argv[0]
    sys.exit(2)

c = IMAPClient(hostname, ssl=True)
try:
    c.login(username, getpass.getpass())
except c.Error, e:
    print 'Could not log in:', e
    sys.exit(1)

print 'Capabilities:', c.capabilities()
print 'Listing mailboxes:'
data = c.list_folders()
for flags, delimiter, folder_name in data:
    print '  %-30s%s %s' % (' '.join(flags), delimiter, folder_name)
c.logout()

If you run this script with appropriate arguments, it will start by asking for your password—IMAP authentication is almost always accomplished through a username and password:

[email protected]:~/Python_files# python open_imaplib.py imap.example.com [email protected]
Password:

If your password is correct, it will then print out a response that looks something like the result shown below:

Capabilities: ('IMAP4REV1', 'UNSELECT', 'IDLE', 'NAMESPACE', 'QUOTA',
'XLIST', 'CHILDREN', 'XYZZY', 'SASL-IR', 'AUTH=XOAUTH')
Listing mailboxes
Status: 'OK'
Data:
'(\\HasNoChildren) "/" "INBOX"'
'(\\HasNoChildren) "/" "Personal"'
'(\\HasNoChildren) "/" "Receipts"'
'(\\HasNoChildren) "/" "Travel"'
'(\\HasNoChildren) "/" "Work"'
'(\\Noselect \\HasChildren) "/" "[Gmail]"'
'(\\HasChildren \\HasNoChildren) "/" "[Gmail]/All Mail"'
'(\\HasNoChildren) "/" "[Gmail]/Drafts"'
'(\\HasChildren \\HasNoChildren) "/" "[Gmail]/Sent Mail"'
'(\\HasNoChildren) "/" "[Gmail]/Spam"'
'(\\HasNoChildren) "/" "[Gmail]/Starred"'
'(\\HasChildren \\HasNoChildren) "/" "[Gmail]/Trash"'

There are two main problems: First, we have been returned its status code manually and second,imaplib gives us no help in interpreting the results.

So unless you want to implement several details of the protocol yourself, you will want a more capable IMAP client library.