Free n8n template · Intermediate
AI Email Categorizer
Classify one test email at a time, apply a Gmail label and log the result. No replies, forwarding or deletion.
- 01Test-labelled email
- 02Read + classify
- 03Apply Gmail label
- 04Log to Sheets
What it does
- Who it is for
- Operations teams exploring inbox triage before letting automation touch a shared mailbox.
- Input
- One Gmail message carrying the AutomateHQ-Test label, excluding messages already carrying a category label.
- Process
- Read a plain-text message, classify it into sales, support, billing or other, validate the category, label the message and append a log row.
- Output
- One added Gmail category label and a sheet row with the message ID, category and short explanation.
Validation status: Export structure and Code node logic are checked with local fixtures. Live n8n import and authenticated provider runs have not been verified. Follow the setup guide with synthetic data before using it with real records.
Set up the workflow
Tools and prerequisites
- An n8n instance with Code, HTTP Request and Webhook nodes. The downloads use built-in nodes only; import compatibility still needs checking on your installed version.
- Your own OpenAI API account with billing enabled. Store the key in an n8n Header Auth credential named OpenAI: header Authorization, value Bearer followed by your key.
- A Google Sheets OAuth2 credential in n8n and a dedicated test spreadsheet. Enable the Sheets API and grant the connected account access to that spreadsheet.
- A Gmail OAuth2 credential in n8n with gmail.modify access and the Gmail API enabled. Use a test mailbox, not your primary inbox.
- Five Gmail labels: AutomateHQ-Test, AHQ-Sales, AHQ-Support, AHQ-Billing and AHQ-Other. Obtain category label IDs using Gmail users.labels.list or n8n’s Gmail label listing operation.
Installation steps
- Import the workflow JSON and create a sheet tab named EmailLog with the headers below.
- Create the five labels in Gmail. Configure the four actual category label IDs in Configure workflow.
- Connect the Gmail, OpenAI and Sheets credentials. Add AutomateHQ-Test to one synthetic plain-text test email.
- Choose Execute workflow. The manual trigger processes at most one eligible email per run. It does not enable background inbox polling.
- Check the new category label and log row. Execute again: an already-labelled message should be excluded. If nothing is eligible, execution ends without calling OpenAI.
- Test one email per category plus an ambiguous message. Only add a Schedule Trigger after resolving the concurrency and partial-failure considerations below.
Spreadsheet column order
Create these headers in row 1. The workflow appends values in this order using RAW input, which keeps text from becoming a spreadsheet formula.
message_id, category, reason, needs_reviewConfiguration points
- Configure workflow
- Set spreadsheetId and sheetName. Replace all four categoryLabelIds with actual Gmail label IDs (Label_...), not display names. Keep the starter search query for your first run.
- Find one test email / Read Gmail message / Apply category label
- Select the same Gmail OAuth2 credential on all three HTTP Request nodes.
- Classify with OpenAI
- Select the OpenAI Header Auth credential. Keep the four allowed categories unless you update validation and label mapping too.
- Append review row
- Select your Google Sheets OAuth2 credential.
Example input & output
Synthetic examples, not customer data. AI output may differ on each run. A well-formed response still needs review.
Input
{
"subject": "Help with my invoice",
"text": "Could you send a copy of invoice INV-1042? I cannot find it in the portal."
}Illustrative output
{
"message_id": "example-gmail-id",
"category": "billing",
"reason": "The sender asks for a copy of an invoice.",
"needs_review": true
}What can go wrong
No messages returned
Confirm the test label is applied and no AHQ category label is already present. Empty results stop cleanly.
No readable plain-text body
HTML-only emails and attachment-only messages are rejected. Use a text/plain MIME part or add a reviewed HTML-to-text conversion.
Label applied but sheet failed
The next run excludes the labelled message. Recover the classification from the execution and repair the log; do not remove the label and rerun without checking.
403 from Gmail
Check Gmail API enablement, gmail.modify consent and that each Gmail node uses the correct credential.
Failed nodes stop execution and remain visible in n8n’s Executions view. No write node retries automatically. A caller timeout does not prove that no write happened: inspect downstream systems before replaying. Add an error workflow if you need alerts.
Limitations
- A manual run processes one email. This is deliberately scoped inbox triage, not a background email agent.
- It does not read attachments, send replies, archive mail or remove existing labels. HTML-only bodies are not supported.
- An AI label can be wrong. The log marks every classification for review; no category triggers a business action.
Before your team depends on it
From demo to production
A successful test run proves one path works. Production engineering also accounts for repeated requests, unavailable services and people correcting the result.
- Persist message_id and processing status in a durable store before enabling multiple workers or scheduled execution. Search exclusions alone do not prevent concurrent runs from selecting the same message.
- Treat Gmail labelling and Sheets logging as separate steps with recoverable state. Record partial completion and reconcile failed log writes.
- Evaluate categories on representative mail. Route ambiguous messages to a person instead of making commitments to customers.
- Use a dedicated mailbox, least-privilege access and explicit retention rules. Monitor volume, retry rates and changes in category distribution.
Security & privacy
The subject and up to 12,000 characters of plain-text body are sent to OpenAI. The log stores a message ID and explanation, not the body, but explanations can still contain sensitive details. n8n execution history can contain the original email. Restrict access and review retention before using real mail.
Secrets belong in n8n’s credential store. Successful production payloads are not saved by this export; manual and failed executions are saved for debugging. Configure pruning and binary-data retention, restrict execution access and delete test runs when finished. Webhook headers can appear in execution inputs, so rotate test keys.
Estimated running costs
One Gmail search per run. A matching message adds one Gmail read, one model request, one label change and one Sheets append. Model retries can increase cost. Empty searches make no model call.
For 1,000 runs, estimate the model cost as (average input tokens × input price per million + average output tokens × output price per million) ÷ 1,000. For example, at hypothetical rates of $1 input and $4 output per million tokens, 1,500 input and 250 output tokens per run would cost $2.50 per 1,000 runs, before retries and hosting. These are illustrative rates, not a provider quote.
Use your execution usage and the current OpenAI pricing and n8n plan to budget. The template download is free; connected services may charge.
Make it your own
- Add a scheduled trigger after implementing durable deduplication.
- Use a human-reviewed draft step for support responses.
- Replace the four categories with a tested, documented team taxonomy.