A Jupyter notebook that connects to a Gmail account via IMAP, fetches today's emails, analyses them with Claude AI, and lets you review and send AI-drafted replies — all from within the notebook.
- Fetch today's emails from Gmail via IMAP (SSL)
- Sender summary table — counts emails per sender and lists subjects
- AI batch analysis — Claude categorises, prioritises, summarises every email and flags which ones need a reply
- Colour-coded results — priority and category are highlighted in a styled pandas DataFrame
- AI-drafted replies — Claude writes a professional reply draft for each email that needs a response
- Interactive send UI — review and edit each draft, confirm with a checkbox, then click Send via SMTP
- Python 3.9+
- A Gmail account with IMAP enabled (Gmail Settings → See all settings → Forwarding and POP/IMAP → Enable IMAP)
- A Gmail App Password if 2-Step Verification is enabled (required — your normal password will not work)
- An Anthropic API key
anthropic
python-dotenv
pandas
ipywidgets
Install all at once:
pip install -r requirements.txt-
Copy
sample.envto.envin the same directory:cp sample.env .env
-
Fill in your credentials in
.env:[email protected] EMAIL_PASSWORD=xxxx xxxx xxxx xxxx # 16-char App Password ANTHROPIC_API_KEY=sk-ant-... -
Create an App Password in Google (if 2FA is on):
- Go to myaccount.google.com/apppasswords
- Select Mail / Other → generate → copy the 16-character password
- Paste it as
EMAIL_PASSWORD(not your real Gmail password)
-
Launch Jupyter and open the notebook:
jupyter lab email_agent.ipynb
The notebook is structured as 8 sequential cells. Run them top to bottom.
Installs dependencies, imports libraries, reads credentials from .env, and initialises the Anthropic client and server constants:
| Variable | Value |
|---|---|
IMAP_SERVER |
imap.gmail.com |
IMAP_PORT |
993 (SSL) |
SMTP_SERVER |
smtp.gmail.com |
SMTP_PORT |
587 (STARTTLS) |
Defines and tests three helper functions:
connect_imap()— opens an authenticatedIMAP4_SSLconnectiondecode_str()— decodes RFC 2047-encoded email headers to plain stringsget_body()— walks a MIME message to extract the plain-text body; falls back to HTML stripped of tags
Runs a quick login/logout to verify credentials before continuing.
fetch_todays_emails() searches the inbox using the IMAP SINCE criterion with today's date, then fetches each matching message in full (RFC822). Returns a list of dicts containing:
| Field | Description |
|---|---|
uid |
IMAP unique ID |
message_id |
RFC 2822 Message-ID (used for reply threading) |
subject |
Decoded subject line |
from_name / from_address |
Parsed sender name and email |
to |
Recipient |
date |
Date string from header |
body |
Full plain-text body |
bodyPreview |
First 300 characters of body |
Builds two pandas DataFrames:
- All emails — one row per message with Name, Email, Subject, Date
- Sender summary — grouped by sender, with email count and concatenated subject list, sorted by frequency
analyze_emails_with_claude() sends all emails in a single prompt to claude-sonnet-4-6 and receives a JSON array. Each item contains:
| Field | Type | Description |
|---|---|---|
index |
int | Matches the email's position in the list |
summary |
str | One-sentence summary |
category |
str | Work, Personal, Newsletter, Notification, or Urgent |
priority |
str | High, Medium, or Low |
needs_reply |
bool | Whether a human reply is expected |
Renders the analysis as a styled pandas DataFrame using pandas.io.formats.style:
- Priority column is coloured: red (High), orange (Medium), green (Low)
- Category column is coloured: red (Urgent), blue (Work), green (Personal), brown (Newsletter), grey (Notification)
For every email where needs_reply is true, draft_reply() sends the email body to Claude and asks it to write a professional, concise reply. Drafts are stored in a drafts dict keyed by email index.
For each draft, renders an ipywidgets panel containing:
- Header — recipient name, address, and subject
- Textarea — editable draft reply (pre-filled by Claude)
- Checkbox — "I reviewed this reply" (must be checked before sending)
- Send button — calls
send_reply()via SMTP STARTTLS once confirmed
send_reply() constructs a MIME message with In-Reply-To and References headers so the reply threads correctly in Gmail.
- Never commit
.env— it is already listed in.gitignore - Use an App Password, not your real Gmail password
- The
sample.envfile contains only placeholder values and is safe to commit