O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

imaplib

The imaplib module provides a low-level client-side interface for connecting to an IMAP4 mail server using the IMAP4rev1 protocol. Documents describing the protocol, as well as sources and binaries for servers implementing it, can be found at the University of Washington’s IMAP Information Center website at http://www.cac.washington.edu/imap.

The following example shows how the module is used by opening a mailbox and printing all messages:

import getpass, imaplib, string
m = imaplib.IMAP4()
m.login(getpass.getuser(), getpass.getpass())
m.select()
typ, data = m.search(None, 'ALL' )
for num in string.split(data[0]):
    typ, data = m.fetch(num,'(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][1])
m.logout()

See Also

poplib (p. 426),

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required