Skip to main content

Imbox - Python IMAP Library for Agentic Workflows

workflow

Python library for reading IMAP mailboxes and converting email content to machine readable data

Table of Contents

Requirements

Python (3.11, 3.12, 3.13)

Installation

pip install imbox

CLI Usage

Imbox includes a command-line interface that can be used to fetch emails directly from the terminal and saving the results to a JSON file. This is particularly useful for agentic workflows.

Running with uv

The recommended way to run the CLI is with uv:

uv run imbox messages

Configuration

The CLI reads configuration from environment variables:

Variable Description Default
IMBOX_IMAP_URL IMAP server URL imap.gmail.com
IMBOX_USERNAME IMAP username -
IMBOX_PASSWORD IMAP password or app password -
IMBOX_SSL Enable SSL true
IMBOX_PORT IMAP port 993
IMBOX_STARTTLS Enable STARTTLS false
DEBUG Enable debug logging false
LOG_LEVEL Logging level INFO
LOG_OUTPUT_TYPE Output format. Clean displays only email fields. Default includes imbox metadata (default or clean) default
OUTPUT Enable file output false
OUTPUT_FOLDER Output folder for downloaded messages output
OUTPUT_FILENAME Output filename for results imbox_results.json

Messages Command

Fetch messages with optional filters:

# Basic usage (requires IMBOX_USERNAME and IMBOX_PASSWORD env vars)
uv run imbox messages

# Fetch from specific folder
uv run imbox messages --folder "Social"

# Fetch unread messages only
uv run imbox messages --unread

# Fetch flagged messages
uv run imbox messages --flagged

# Filter by sender
uv run imbox messages --sent-from "sender@example.com"

# Filter by recipient
uv run imbox messages --sent-to "recipient@example.com"

# Filter by subject
uv run imbox messages --subject "Newsletter"

# Filter by date range
uv run imbox messages --date-gt 2026-01-01 --date-lt 2026-12-31

# Filter by specific date
uv run imbox messages --date-on 2026-03-15

# Filter by UID range (e.g., messages with UID 1050 and above)
uv run imbox messages --uid-range "1050:*"

# Gmail-specific raw search (requires Gmail)
uv run imbox messages --raw "from:user has:attachment"

# Gmail-specific label filter (Gmail only)
uv run imbox messages --folder all --label "finance"

# Combine multiple filters
uv run imbox messages --folder "INBOX" --unread --sent-from "newsletter@example.com"

Folders Command

List all folders on the IMAP server:

uv run imbox folders

Debugging

Enable debug output for troubleshooting:

DEBUG=true LOG_LEVEL=DEBUG uv run imbox messages --unread

Usage (Python Library)

from imbox import Imbox

# SSL Context docs https://docs.python.org/3/library/ssl.html#ssl.create_default_context

with Imbox('imap.gmail.com',
        username='username',
        password='password',
        ssl=True,
        ssl_context=None,
        starttls=False) as imbox:

    # Get all folders
    status, folders_with_additional_info = imbox.folders()

    # Gets all messages from the inbox
    all_inbox_messages = imbox.messages()

    # Unread messages
    unread_messages = imbox.messages(unread=True)

    # Flagged messages
    flagged_messages = imbox.messages(flagged=True)

    # Un-flagged messages
    unflagged_messages = imbox.messages(unflagged=True)

    # Messages sent FROM
    inbox_messages_from = imbox.messages(sent_from='sender@example.org')

    # Messages sent TO
    inbox_messages_to = imbox.messages(sent_to='receiver@example.org')

    # Messages received before specific date
    inbox_messages_received_before = imbox.messages(date__lt=datetime.date(2026, 7, 31))

    # Messages received after specific date
    inbox_messages_received_after = imbox.messages(date__gt=datetime.date(2026, 7, 30))

    # Messages received on a specific date
    inbox_messages_received_on_date = imbox.messages(date__on=datetime.date(2026, 7, 30))

    # Messages whose subjects contain a string
    inbox_messages_subject_christmas = imbox.messages(subject='Christmas')

    # Messages whose UID is greater than 1050
    inbox_messages_uids_greater_than_1050 = imbox.messages(uid__range='1050:*')

    # Messages from a specific folder
    messages_in_folder_social = imbox.messages(folder='Social')

    # Some of Gmail's IMAP Extensions are supported (label and raw):
    all_messages_with_an_attachment_from_martin = imbox.messages(folder='all', raw='from:martin@amon.cx has:attachment')
    all_messages_labeled_finance = imbox.messages(folder='all', label='finance')

    for uid, message in all_inbox_messages:
    # Every message is an object with the following keys

        message.sent_from
        message.sent_to
        message.subject
        message.headers
        message.message_id
        message.date
        message.body.plain

Release files for imbox 0.10.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for imbox 0.10.1
File Size Uploaded
imbox-0.10.1.tar.gz 24.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for imbox 0.10.1
File Interpreter ABI Platform
imbox-0.10.1-py3-none-any.whl Python 3 none any Details

Total release size: 50.5 kB

Release files / imbox-0.10.1.tar.gz

Download URL imbox-0.10.1.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
00ae16a231c0703637bac58e0965cc9cea65351106b095531a81f1bc360d54fc
BLAKE2b-256 checksum
How to use checksums
254f25b7cab0ceb9aa1180399c142e36b9d886ffbaedd634bd1cf1e35ac3f779
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 31, 2026.

Transparency log

Release files / imbox-0.10.1-py3-none-any.whl

Download URL imbox-0.10.1-py3-none-any.whl
Size 26.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b32e24aa011942911b551ceeca7e1ccea25ee2925d7245ac4f5c0b8dd2db05c6
BLAKE2b-256 checksum
How to use checksums
d8965d4f8822ad8d68df3d96fb460161e701ee41ac62724d619d05e8a1acf668
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 31, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.10.1 This release

2 release files

0.10.0

2 release files

0.9.8

1 release file

0.9.7

2 release files

0.9.6

1 release file

0.9.5

1 release file

0.9

1 release file

0.8.5

1 release file

0.8

1 release file

0.7

1 release file

0.6

1 release file

0.5.5

1 release file

0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page