An n8n workflow for Gmail that classifies new unread mail with an OpenAI-powered agent, then either sends a short auto-reply (generic / bulk mail) or creates a Gmail draft (messages that need a real reply). You keep control: non-generic mail becomes an editable draft instead of sending automatically.
Repository: github.com/coder-msk/Automatic-Email-Response-and-Draft-Creation
| Behavior | When |
|---|---|
| Auto-reply in the thread | Email is classified as GENERIC (newsletters, promos, receipts, automated notices, etc.). Reply goes to the sender only. |
Gmail draft (Re: …) |
Email is classified as NON_GENERIC (personal or business mail that expects a thoughtful reply). |
The model returns structured JSON: classification, reasoning, and suggestedReply. An If node branches on classification.
flowchart LR
A[Gmail: new unread] --> B[Set fields]
B --> C[AI: classify + suggest reply]
C --> D{GENERIC?}
D -->|yes| E[Send auto reply]
D -->|no| F[Create draft]
- New Unread Email — Gmail trigger (e.g. poll every minute).
- Workflow Configuration — Maps subject, body, from,
messageId,threadId. - Classify Email — LangChain agent with a system prompt for GENERIC vs NON_GENERIC and reply tone.
- OpenAI Chat Model + Structured Output Parser — Keeps output in a fixed JSON shape (second chat model supports the parser).
- Is Generic Email? — Routes to Send Auto Reply or Create Draft.
- Send Auto Reply / Create Draft — Gmail nodes using thread metadata and
suggestedReply.
n8n view of the branch that sends an automatic reply for generic mail:
n8n view of the branch that creates a draft for non-generic mail:
- n8n with Gmail and LangChain nodes (compatible with the version used when the workflow was exported).
- Gmail OAuth2 — used by the trigger and Gmail send/draft nodes.
- OpenAI API — e.g.
gpt-4o-minifor the agent and parser chat models.
- In n8n: Workflows → Import from File → select
Automated Email Response & Draft Creation.json. - Open every node that uses credentials and attach your own Gmail and OpenAI credentials (exported workflows often still show IDs from the original instance).
- Adjust New Unread Email (poll interval and Gmail filters) to match your inbox.
- Activate the workflow when you are satisfied with tests.
Security: API keys and OAuth tokens live in n8n credentials, not inside the JSON file. Do not commit secrets; re-credential after import on a new instance.
| Path | Purpose |
|---|---|
Automated Email Response & Draft Creation.json |
Exported n8n workflow (import this). |
Send Auto Reply for Generic Emails/ |
Workflow screenshot for the generic / auto-reply path. |
Create Draft for Non-Generic Emails/ |
Workflow screenshot for the non-generic / draft path. |
email classification.docx |
Optional notes on classification (not required to run the workflow). |
Add a LICENSE file if you want to specify terms for reuse.