Email Triage
Scan your IMAP inbox, classify emails into priority categories, and surface the ones that need attention. Uses a local LLM (Ollama) for intelligent classification with a rule-based heuristic fallback when Ollama is unavailable.
Prerequisites
- Python 3.10+
- IMAP-accessible email account (Gmail, Fastmail, self-hosted, etc.)
- Ollama (optional) — for AI-powered classification. Without it, the script uses keyword-based heuristics that still work well for common patterns.
Categories
| Icon |
Category |
Description |
| 🔴 |
urgent |
Outages, security alerts, legal, payment failures, time-critical |
| 🟡 |
needs-response |
Business inquiries, questions, action items requiring a reply |
| 🔵 |
informational |
Receipts, confirmations, newsletters, automated notifications |
| ⚫ |
spam |
Marketing, promotions, unsolicited junk |
Configuration
All configuration is via environment variables:
| Variable |
Required |
Default |
Description |
IMAP_HOST |
✅ |
— |
IMAP server hostname |
IMAP_PORT |
— |
993 |
IMAP port (SSL) |
IMAP_USER |
✅ |
— |
IMAP username / email address |
IMAP_PASS |
✅ |
— |
IMAP password or app-specific password |
EMAIL_TRIAGE_STATE |
— |
./data/email-triage.json |
Path to the JSON state file |
OLLAMA_URL |
— |
http://127.0.0.1:11434 |
Ollama API endpoint |
OLLAMA_MODEL |
— |
qwen2.5:7b |
Ollama model for classification |
Directories Written
EMAIL_TRIAGE_STATE (default: ./data/email-triage.json) — Persistent state file tracking classified emails and surfacing status
Commands
# Scan inbox and classify new unread emails
python3 scripts/email/email-triage.py scan
# Scan with verbose output (shows each classification)
python3 scripts/email/email-triage.py scan --verbose
# Dry run — scan and classify but don't save state
python3 scripts/email/email-triage.py scan --dry-run
# Show unsurfaced important emails (urgent + needs-response)
python3 scripts/email/email-triage.py report
# Same as report but JSON output (for programmatic use)
python3 scripts/email/email-triage.py report --json
# Mark reported emails as surfaced (so they don't appear again)
python3 scripts/email/email-triage.py mark-surfaced
# Show triage statistics
python3 scripts/email/email-triage.py stats
How It Works
- Connects to IMAP over SSL and fetches unread messages (up to 20 per scan).
- Deduplicates by Message-ID (or a hash of subject + sender as fallback) so emails are never classified twice.
- Classifies each email using Ollama if available, otherwise falls back to keyword heuristics.
- Stores state in a local JSON file — tracks category, reason, and whether the email has been surfaced.
report surfaces only unsurfaced urgent and needs-response emails, sorted by priority.
mark-surfaced flags reported emails so they won't appear in future reports.
- Auto-prunes state to the most recent 200 entries to prevent unbounded growth.
Integration Tips
- Heartbeat / cron: Run
scan periodically, then report --json to check for items needing attention.
- Agent workflow:
scan → report --json → act on results → mark-surfaced.
- Without Ollama: The heuristic classifier handles common patterns (automated notifications, marketing, urgent keywords) well. Ollama adds nuance for ambiguous emails.
- App passwords: If your provider uses 2FA, generate an app-specific password for IMAP access.
1---
2name: email-triage
3version: 1.0.1
4description: IMAP email scanning and triage with AI classification via a local Ollama LLM. Scans unread emails, categorizes them as urgent, needs-response, informational, or spam, and surfaces important messages for agent consumption. Works standalone with heuristic fallback — Ollama optional but recommended.
5metadata:
6 source: https://skillmd.com/skills/modbender/email-triage
7 openclaw:
8 requires:
9 bins: ["python3"]
10---
11
12# Email Triage
13
14Scan your IMAP inbox, classify emails into priority categories, and surface the ones that need attention. Uses a local LLM (Ollama) for intelligent classification with a rule-based heuristic fallback when Ollama is unavailable.
15
16## Prerequisites
17
18- **Python 3.10+**
19- **IMAP-accessible email account** (Gmail, Fastmail, self-hosted, etc.)
20- **Ollama** *(optional)* — for AI-powered classification. Without it, the script uses keyword-based heuristics that still work well for common patterns.
21
22## Categories
23
24| Icon | Category | Description |
25|------|----------|-------------|
26| 🔴 | `urgent` | Outages, security alerts, legal, payment failures, time-critical |
27| 🟡 | `needs-response` | Business inquiries, questions, action items requiring a reply |
28| 🔵 | `informational` | Receipts, confirmations, newsletters, automated notifications |
29| ⚫ | `spam` | Marketing, promotions, unsolicited junk |
30
31## Configuration
32
33All configuration is via environment variables:
34
35| Variable | Required | Default | Description |
36|----------|----------|---------|-------------|
37| `IMAP_HOST` | ✅ | — | IMAP server hostname |
38| `IMAP_PORT` | — | `993` | IMAP port (SSL) |
39| `IMAP_USER` | ✅ | — | IMAP username / email address |
40| `IMAP_PASS` | ✅ | — | IMAP password or app-specific password |
41| `EMAIL_TRIAGE_STATE` | — | `./data/email-triage.json` | Path to the JSON state file |
42| `OLLAMA_URL` | — | `http://127.0.0.1:11434` | Ollama API endpoint |
43| `OLLAMA_MODEL` | — | `qwen2.5:7b` | Ollama model for classification |
44
45## Directories Written
46
47- **`EMAIL_TRIAGE_STATE`** (default: `./data/email-triage.json`) — Persistent state file tracking classified emails and surfacing status
48
49## Commands
50
51```bash
52# Scan inbox and classify new unread emails
53python3 scripts/email/email-triage.py scan
54
55# Scan with verbose output (shows each classification)
56python3 scripts/email/email-triage.py scan --verbose
57
58# Dry run — scan and classify but don't save state
59python3 scripts/email/email-triage.py scan --dry-run
60
61# Show unsurfaced important emails (urgent + needs-response)
62python3 scripts/email/email-triage.py report
63
64# Same as report but JSON output (for programmatic use)
65python3 scripts/email/email-triage.py report --json
66
67# Mark reported emails as surfaced (so they don't appear again)
68python3 scripts/email/email-triage.py mark-surfaced
69
70# Show triage statistics
71python3 scripts/email/email-triage.py stats
72```
73
74## How It Works
75
761. **Connects to IMAP** over SSL and fetches unread messages (up to 20 per scan).
772. **Deduplicates** by Message-ID (or a hash of subject + sender as fallback) so emails are never classified twice.
783. **Classifies** each email using Ollama if available, otherwise falls back to keyword heuristics.
794. **Stores state** in a local JSON file — tracks category, reason, and whether the email has been surfaced.
805. **`report`** surfaces only unsurfaced urgent and needs-response emails, sorted by priority.
816. **`mark-surfaced`** flags reported emails so they won't appear in future reports.
827. **Auto-prunes** state to the most recent 200 entries to prevent unbounded growth.
83
84## Integration Tips
85
86- **Heartbeat / cron:** Run `scan` periodically, then `report --json` to check for items needing attention.
87- **Agent workflow:** `scan` → `report --json` → act on results → `mark-surfaced`.
88- **Without Ollama:** The heuristic classifier handles common patterns (automated notifications, marketing, urgent keywords) well. Ollama adds nuance for ambiguous emails.
89- **App passwords:** If your provider uses 2FA, generate an app-specific password for IMAP access.