Email Automation: Dual Mailbox Integration and AI Triage System
Email remains an irreplaceable information gateway in the AI Agent era. Here's how I implemented dual mailbox auto-reading and intelligent triage.
Dual Mailbox Architecture
- Hotmail — via Microsoft Graph API
- Gmail — via Gmail API
- P0 Urgent: Immediate notification (keyword + important contact matching)
- P1 Important: Same-day processing (work emails, bills) → daily report
- P2 Normal: Weekly summary (newsletters, social notifications)
- P3 Low Priority: Auto-archive (marketing, promotions)
Two APIs, two auth methods, two completely different ecosystems.
MS Graph API: Personal Account Pitfall
Personal Microsoft accounts require the /consumers/ endpoint. /common/ won't work. A vague "invalid_grant" error cost me half a day.
Device Code Flow
Headless server, no browser. Device Code Flow saves the day: program gets URL and code → Agent sends to me via Telegram → I authorize on my phone → Agent starts reading email.
Gmail Reader
Four functions: list, unread, search, and code (auto-extract verification codes). Automatically extracts 6-digit codes from verification emails — no more manual copying.
Email Triage: 4-Level Auto-Classification
Rule-based classification (sender whitelist + keywords + header analysis). Fast, stable, predictable.
Results
Every morning: auto-check both mailboxes → triage → P0 instant alerts → P1 daily digest. From 50-80 daily emails, only 3-5 actually need attention. Saving not just time, but attention.
Authentication is the most time-consuming part. The actual email reading and classification logic is surprisingly the simplest.