An intelligent email agent powered by OpenAI GPT-4o and Gmail API. It understands your emails deeply, drafts smart replies, manages tasks, and summarizes your inbox — all through a natural language CLI.
| Feature | Description |
|---|---|
| 🧠 Deep Understanding | Classify emails, detect intent, extract dates/names/deadlines |
| ✉️ Smart Replies | Context-aware drafts with tone matching — requires your approval before sending |
| 🔎 Search & Summarize | Natural language inbox search, actionable summaries |
| 📌 Task Extraction | Auto-convert emails into to-dos with deadlines and priorities |
| 🔧 Function Calling | Uses OpenAI function calling for reliable, structured tool use |
cd email_agent
pip install -r requirements.txtCopy the env template and add your API key:
cp .env.example .env
# Edit .env and set OPENAI_API_KEY=sk-...- Go to Google Cloud Console
- Create a new project (or use existing)
- Enable the Gmail API: APIs & Services → Enable APIs → search "Gmail API"
- APIs & Services → Credentials → Create Credentials → OAuth client ID
- Application type: Desktop app
- Download the JSON file
- Save it as
credentials.jsonin theemail_agent/folder
- APIs & Services → OAuth consent screen
- User type: External (or Internal if using Google Workspace)
- Add your Gmail address as a test user
- Scopes needed: Gmail read, send, modify
python main.pyOn first run, a browser window will open for Gmail authorization. After approving, a token.json is saved for future runs.
You: Summarize my inbox
You: Show unread emails
You: Find emails from Sarah about the contract
You: Analyze email [id] and draft a formal reply
You: What tasks do I have?
You: Create a task: Review proposal — due Friday
You: Mark task abc123 as done
email_agent/
├── main.py # Entry point
├── .env # Your config (git-ignored)
├── requirements.txt
│
├── config/
│ └── settings.py # Loads .env settings
│
├── tools/
│ ├── gmail_tool.py # Gmail API wrapper (read/send/search)
│ └── task_manager.py # JSON-based task persistence
│
└── agent/
├── email_agent.py # Orchestrator — OpenAI function calling loop
├── email_analyzer.py # Deep analysis & reply drafting with GPT
└── tool_definitions.py # OpenAI tool/function schemas
- You type a natural language request
- The agent sends it to GPT-4o with a set of tool definitions
- GPT-4o decides which tools to call (e.g.
list_emails,analyze_email) - Tools execute against Gmail API / task store
- Results feed back to GPT-4o which synthesizes a final response
- For replies: a draft is shown for your approval before any send
| Variable | Default | Description |
|---|---|---|
OPENAI_API_KEY |
— | Required |
OPENAI_MODEL |
gpt-4o |
Model to use |
GMAIL_CREDENTIALS_FILE |
credentials.json |
OAuth credentials path |
GMAIL_TOKEN_FILE |
token.json |
Auth token (auto-created) |
USER_NAME |
User |
Your name for email sign-offs |
USER_EMAIL |
auto-detected | Your Gmail address |
MAX_EMAILS_FETCH |
20 |
Max emails per fetch |
TASKS_FILE |
tasks.json |
Task storage path |
AUTO_SEND |
false |
Skip send confirmation (not recommended) |
- Never auto-sends: All replies are drafted and shown for approval first
- No data leaves: Emails are sent to OpenAI API only for analysis — standard API privacy applies
- Local tasks: Task data stored in a local JSON file only
- Scoped permissions: Only requests Gmail read, send, and modify scopes
To add a new capability:
- Add a new tool definition in
agent/tool_definitions.py - Add the handler in
agent/email_agent.py→_dispatch() - Optionally add a helper class in
tools/
The OpenAI function-calling loop handles everything else automatically.