How it works...

Our class acts as a wrapper over the imaplib and email modules, providing an easier-to-use interface for the need of fetching mail from a folder.

There are actually two different objects that can be created from imaplib to connect to an IMAP server, one that uses SSL and one that doesn't. Depending on what's required by your server, you might have to turn it on or off (for example, Gmail requires SSL) and that's abstracted in __init__:

def __init__(self, host, username, password, ssl=True):
    if ssl:
        self._imap = imaplib.IMAP4_SSL(host)
    else:
        self._imap = imaplib.IMAP4(host)
    self._imap.login(username, password)

The __init__ method also takes care of logging you against the IMAP server, so that the once the reader is created, ...

Get Modern Python Standard Library Cookbook 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.