Library for reading IMAP mailboxes and converting email content to machine readable data
Project description
Imbox - Python IMAP Library for Agentic Workflows
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file imbox-0.10.1.tar.gz.
File metadata
- Download URL: imbox-0.10.1.tar.gz
- Upload date:
- Size: 24.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00ae16a231c0703637bac58e0965cc9cea65351106b095531a81f1bc360d54fc
|
|
| MD5 |
d000a04411fee3e8be09241bb2a5fd03
|
|
| BLAKE2b-256 |
254f25b7cab0ceb9aa1180399c142e36b9d886ffbaedd634bd1cf1e35ac3f779
|
Provenance
The following attestation bundles were made for imbox-0.10.1.tar.gz:
Publisher:
python-app.yml on martinrusev/imbox
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
imbox-0.10.1.tar.gz -
Subject digest:
00ae16a231c0703637bac58e0965cc9cea65351106b095531a81f1bc360d54fc - Sigstore transparency entry: 1203495324
- Sigstore integration time:
-
Permalink:
martinrusev/imbox@d48b864b7fd1d057ac30f044ba30c8455c38dc12 -
Branch / Tag:
refs/tags/0.10.1 - Owner: https://github.com/martinrusev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-app.yml@d48b864b7fd1d057ac30f044ba30c8455c38dc12 -
Trigger Event:
push
-
Statement type:
File details
Details for the file imbox-0.10.1-py3-none-any.whl.
File metadata
- Download URL: imbox-0.10.1-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b32e24aa011942911b551ceeca7e1ccea25ee2925d7245ac4f5c0b8dd2db05c6
|
|
| MD5 |
80067681ac1b9094ef1def736936d9b4
|
|
| BLAKE2b-256 |
d8965d4f8822ad8d68df3d96fb460161e701ee41ac62724d619d05e8a1acf668
|
Provenance
The following attestation bundles were made for imbox-0.10.1-py3-none-any.whl:
Publisher:
python-app.yml on martinrusev/imbox
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
imbox-0.10.1-py3-none-any.whl -
Subject digest:
b32e24aa011942911b551ceeca7e1ccea25ee2925d7245ac4f5c0b8dd2db05c6 - Sigstore transparency entry: 1203495328
- Sigstore integration time:
-
Permalink:
martinrusev/imbox@d48b864b7fd1d057ac30f044ba30c8455c38dc12 -
Branch / Tag:
refs/tags/0.10.1 - Owner: https://github.com/martinrusev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-app.yml@d48b864b7fd1d057ac30f044ba30c8455c38dc12 -
Trigger Event:
push
-
Statement type: