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),

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.