IMAP Tools

1.12.0 · active · verified Tue Apr 14

imap-tools is a high-level Python library designed for working with email via the IMAP protocol. It provides a user-friendly interface for common email operations such as fetching, parsing, searching, moving, and deleting messages, as well as managing folders. The library is actively maintained, with frequent minor releases, and is currently at version 1.12.0.

Warnings

Install

Imports

Quickstart

Connects to an IMAP server using environment variables for credentials, fetches the subjects and senders of the first 5 unseen emails in the INBOX, and prints them. It demonstrates the use of `MailBox` as a context manager and basic `fetch` with search criteria.

import os
from imap_tools import MailBox, AND

IMAP_HOST = os.environ.get('IMAP_HOST', 'imap.mail.com')
IMAP_USER = os.environ.get('IMAP_USER', 'test@mail.com')
IMAP_PASS = os.environ.get('IMAP_PASS', 'your-password')

try:
    with MailBox(IMAP_HOST).login(IMAP_USER, IMAP_PASS) as mailbox:
        # Fetch all unseen emails from the INBOX and print their subject and sender
        print(f"Connected to IMAP host: {IMAP_HOST}")
        print("Fetching unseen emails...")
        for msg in mailbox.fetch(criteria=AND(seen=False), mark_seen=False, limit=5):
            print(f"From: {msg.from_}, Subject: {msg.subject}")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →