Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Email Agent

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.

Features

  • 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

Requirements

  • 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

Python packages

anthropic
python-dotenv
pandas
ipywidgets

Install all at once:

pip install -r requirements.txt

Setup

  1. Copy sample.env to .env in the same directory:

    cp sample.env .env
  2. Fill in your credentials in .env:

    [email protected]
    EMAIL_PASSWORD=xxxx xxxx xxxx xxxx   # 16-char App Password
    ANTHROPIC_API_KEY=sk-ant-...
    
  3. 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)
  4. Launch Jupyter and open the notebook:

    jupyter lab email_agent.ipynb

How It Works

The notebook is structured as 8 sequential cells. Run them top to bottom.

Cell 1 — Setup & imports

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)

Cell 2 — IMAP connection helpers

Defines and tests three helper functions:

  • connect_imap() — opens an authenticated IMAP4_SSL connection
  • decode_str() — decodes RFC 2047-encoded email headers to plain strings
  • get_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.

Cell 3 — Fetch today's emails

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

Cell 4 — Sender summary table

Builds two pandas DataFrames:

  1. All emails — one row per message with Name, Email, Subject, Date
  2. Sender summary — grouped by sender, with email count and concatenated subject list, sorted by frequency

Cell 5 — AI batch analysis

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

Cell 6 — Colour-coded results table

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)

Cell 7 — Draft AI replies

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.

Cell 8 — Interactive send UI

For each draft, renders an ipywidgets panel containing:

  1. Header — recipient name, address, and subject
  2. Textarea — editable draft reply (pre-filled by Claude)
  3. Checkbox — "I reviewed this reply" (must be checked before sending)
  4. 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.

Security Notes

  • Never commit .env — it is already listed in .gitignore
  • Use an App Password, not your real Gmail password
  • The sample.env file contains only placeholder values and is safe to commit

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages