Package: imapIO
Here are some convenience classes and methods for processing IMAP mailboxes.  Since the classes are derived from theimaplibclasses, all methods available in theimaplibclasses are directly usable.Installationeasy_install -U imapIOUsage# Connect to IMAP server
import imapIO
server = imapIO.connect(host, port, user, password)

# Select folder
import random
emailCount = server.cd(random.choice(server.folders))

# Walk emails in inbox sorted by arrival time
for email in server.walk('inbox', sortCriterion='ARRIVAL'):
    # Show information
    print
    print 'Date: %s' % email.whenUTC
    print 'Subject: %s' % email.subject.encode('utf-8')
    print 'From: %s' % email.fromWhom.encode('utf-8')
    print 'From (nickname): %s' % imapIO.clean_nickname(email.fromWhom)
    print 'To: %s' % email.toWhom.encode('utf-8')
    print 'CC: %s' % email.ccWhom.encode('utf-8')
    print 'BCC: %s' % email.bccWhom.encode('utf-8')
    # Set flags
    email.seen = False
    email.deleted = False

# Walk emails satisfying search criterion
emailCriterion = 'BEFORE 23-JAN-2005'
emailGenerator = server.walk(lambda folder: folder not in ['public', 'trash'], searchCriterion=emailCriterion)
for emailIndex, email in enumerate(emailGenerator):
    # Show flags
    print
    print email.flags
    # Save email in compressed format on hard drive
    emailPath = '%s.gz' % emailIndex
    partPacks = email.save(emailPath)
    # Extract attachments from email on hard drive
    for partIndex, filename, contentType, payload in imapIO.extract(emailPath):
        print len(payload), filename.encode('utf-8')

# Create an email in the inbox
import datetime
server.revive('inbox', imapIO.build_message(
    whenUTC=datetime.datetime(2005, 1, 23, 1, 0),
    subject='Subject',
    fromWhom='from@example.com',
    toWhom='to@example.com',
    ccWhom='cc@example.com',
    bccWhom='bcc@example.com',
    bodyText=u'text',
    bodyHTML=u'<html>text</html>',
    attachmentPaths=[
        'CHANGES.rst',
        'README.rst',
    ]))
# Load email
email = server.walk('inbox', searchCriterion='FROM from@example.com TO to@example.com').next()
# Browse attachments in email
partPacks = email.extract(
    include=lambda index, name, type: name.lower().endswith('.rst'),
    peek=True)
for partIndex, filename, contentType in partPacks:
    print filename
# Delete email
email.deleted = True
server.expunge()

# Duplicate an email from one server to another
server1 = imapIO.connect(host1, port1, user1, password1)
server2 = imapIO.connect(host2, port2, user2, password2)
server2.revive('inbox', server1.walk().next())0.9.5Added examples to _IMAPExtension.walk() docstringAdded Email.extract() for getting attachments directly from an email messageCached Email.as_string()Changed error formattingFixed bug in Email.flags() so it handles messages with no flagsFixed bug in extract() so that it does not try to decode non-text into unicodeModified extract() to filter attachments using a lambda function0.9.4Modified _IMAPExtension.walk() to accept a generic function to filter foldersModified Email.__init__() to apply _decode() to both parts of an email addressRemoved clean_tag(), parse_tags, format_tags()Added utf-7-imap4 codec to parse folder namesIncreased test coverage to 100%0.9.3Fixed revive() to handle messages that lack a dateModified Email so an email from _IMAPExtension.walk() can be sent to revive()Modified Email so we can access its parent folderModified flags.setter so that it does not try to set flag “Recent”0.9.2Reverted to set() for versions of Python < 2.7 that lack set literal syntaxRemoved keyword arguments from decode() to support versions of Python < 2.7Fixed tests for servers like Lotus Domino that do not update search indices0.9.1Changed _IMAPExtension.walk() to use UID directlyAdded support for sortCriterion using UID SORTImproved test coverage to 80%0.9.0Extracted code from imap-search-scoutMade API more user-friendlyImproved test coverage to 79%