Launch-Free 3 months Builder plan-
Pixel art lobster working at a computer terminal with email — n8n ai agent email automation

how to build an n8n ai agent for email automation

Step-by-step guide to building an AI email agent in n8n with Gmail, OpenAI, and memory nodes, plus what to do when you hit Gmail's limits.

9 min read
Ian Bussières
Ian BussièresCTO & Co-founder

You can build an AI email agent in n8n in about 40 minutes. A Gmail trigger, an AI agent node, an OpenAI model, and a system prompt that tells the agent how to classify messages. Hit deploy, and your workflow starts reading, sorting, and replying to emails without you. Most tutorials cover exactly that much. This guide goes further: the build itself, then the OAuth friction, Gmail rate limits, and what to do when one Gmail account isn't enough.

How to build an n8n AI email agent (step-by-step)#

  1. Create a new n8n workflow and add a Gmail Trigger node configured to poll for new emails every minute.
  2. Connect an AI Agent node and attach an OpenAI Chat Model sub-node (GPT-4o-mini handles classification well at low cost).
  3. Write a system prompt instructing the agent to classify each email (support, sales, spam, newsletter, other) and draft a reply for support messages.
  4. Add a Gmail node after the agent to apply labels based on the classification and send drafted responses.
  5. Attach a Window Buffer Memory node to the agent so it retains context from previous messages in the same thread.
  6. Test the entire workflow with a sandboxed Gmail account before enabling live sends.

That's the skeleton. Let's look at each piece in detail.

Setting up Gmail OAuth2 authentication#

The Gmail nodes in n8n require OAuth2 credentials. You'll create a project in Google Cloud Console, enable the Gmail API, configure a consent screen, and generate a client ID and secret. Then you paste those into n8n's credential manager and complete the authorization flow.

For a single agent reading one inbox, this is manageable. The pain starts when you need multiple agents with separate email addresses. Each agent requires its own OAuth2 credential set, its own consent screen approval, and its own token refresh cycle. Google also requires a verification review for apps that access Gmail scopes outside your organization, which adds days or weeks of delay if you're building for other users.

One agent, one inbox? OAuth2 is fine. Ten agents that each need independent identities? It becomes the project's biggest maintenance burden.

Classifying emails with the AI agent node#

The AI Agent node is the brain of the workflow. It takes the email body as input, runs it through your LLM with a system prompt, and returns structured decisions.

A practical system prompt for email classification:

You are an email assistant. For each incoming email:
1. Classify it as: support, sales, newsletter, spam, or other
2. If it's a support email, draft a helpful reply
3. If it's spam or a newsletter, mark it for archiving
4. Return your classification and any draft reply as JSON

The difference between the AI Agent node and a simple LLM chain matters here. A basic chain processes text in and returns text out. The Agent node can reason about which tools to call: a vector store for context retrieval, a calculator, another API. For email workflows, this means the agent can look up relevant knowledge base articles before drafting a reply, rather than improvising from its training data alone.

This is where RAG (Retrieval-Augmented Generation) earns its keep. Connect a Vector Store Tool node backed by Pinecone or Qdrant, load it with your company's support docs, and the agent pulls relevant context before drafting each reply. Without RAG, replies are generic. With it, the agent references your actual policies, pricing, and product details. The quality jump is significant.

Adding conversation memory#

Without memory, your agent treats every email as a first contact. Someone replies to an ongoing thread and the agent has no idea what was discussed two messages ago.

The Window Buffer Memory node solves this by storing the last N interactions and passing them back into the agent's context window. Set the window size to 5-10 messages for email threads. This lets the agent reference earlier conversation without consuming your entire token budget on context. For long-term memory that persists across different threads and workflow executions, you'll need an external store (a database or vector DB that survives restarts).

Where Gmail starts breaking down#

Gmail is a great starting point. It's free, everyone has an account, and n8n's Gmail nodes handle the basics. But production agents hit real walls:

Rate limits. Gmail's API allows roughly 250 sends per day for regular accounts and around 2,000 for Google Workspace. If your agent processes high volumes, you'll hit these ceilings within the first week. Gmail returns 429 errors when you do, and your workflow stalls until the quota resets.

No per-agent identity. Every email your agent sends comes from a single Gmail address. Running separate agents for support, sales, and onboarding? They all share one sender. There's no way to give each agent its own address without creating and authenticating entirely separate Google accounts.

Deliverability at scale. Gmail was built for humans writing individual messages, not for agents sending dozens of replies per hour. Recipient spam filters start flagging programmatic-looking send patterns. And with a personal Gmail account, you don't control SPF records, DKIM signing, or sending IP reputation.

We dug into the tradeoffs between webhooks and polling for agent email delivery in a separate post. Short version: polling (which is what n8n's Gmail Trigger does) adds latency and wastes API calls checking for messages that haven't arrived. Webhooks push emails to your agent the instant they land.

When your agent needs its own inbox#

The natural progression after outgrowing Gmail is giving each agent a dedicated email address with its own authentication records, sending reputation, and delivery infrastructure.

This is where LobsterMail fits. Instead of provisioning OAuth2 credentials, DNS records, and rate limit workarounds per agent, the agent pinches its own inbox with a single call:

import { LobsterMail } from '@lobsterkit/lobstermail';

const lm = await LobsterMail.create();
const inbox = await lm.createSmartInbox({ name: 'Support Agent' });
console.log(inbox.address); // support-agent@lobstermail.ai

No OAuth flow. No Google Cloud Console. Each agent gets a clean address with proper email authentication from the start. If you're curious what agents actually do once they have their own inbox, we wrote up seven practical use cases.

You can still use n8n as your orchestration layer. Replace the Gmail nodes with HTTP Request nodes that call LobsterMail's API (or use the SDK in a Code node). Your AI classification logic, memory nodes, and workflow structure stay exactly the same. If you want to try it, and swap the Gmail nodes in your existing workflow.

Testing without accidents#

Before letting an email agent run on a live inbox, build a safety net:

  1. Create a dedicated test email account (or free LobsterMail inbox) that receives only test messages.
  2. Send yourself emails that cover each classification category: a support question, a sales inquiry, a newsletter, obvious spam.
  3. Set the Gmail send node to draft mode first. The agent creates drafts instead of sending replies directly, so you can review before anything goes out.
  4. Add a deduplication check: store processed message IDs in n8n's static data or an external database, and skip any email your agent has already handled.
  5. Monitor the first 50 live emails manually before trusting the agent to run unsupervised.

The biggest risk with email agents isn't bad classification. It's duplicate sends. If your workflow triggers twice for the same message (during n8n restarts or webhook retries), the recipient gets two identical replies. Always deduplicate by message ID.

Picking the right platform#

Is n8n better than Zapier for AI email automation? For this use case, yes. n8n's AI Agent node gives you real tool-using agents, not just simple text completion. The self-hosted Community Edition means your email data stays on your own infrastructure (useful for GDPR), and there's no per-execution cost ceiling. Zapier is simpler for basic triggers but gets expensive fast when an agent processes hundreds of emails daily, and its AI features are more constrained.

Start with the six-step workflow above. Get classification working reliably on a test inbox. Add RAG if your replies need company-specific context. And when Gmail's limits start costing you more time than they save, give each agent its own address and stop fighting OAuth.

Frequently asked questions

What n8n nodes do I need to build an AI email agent?

At minimum: a Gmail Trigger (or IMAP Trigger), an AI Agent node, an OpenAI Chat Model sub-node, and a Gmail node for sending replies or applying labels. Add a Window Buffer Memory node if you need conversation context across a thread.

How do I authenticate Gmail with OAuth2 in n8n?

Create a Google Cloud project, enable the Gmail API, set up an OAuth consent screen, and generate OAuth2 client credentials. Paste the client ID and secret into n8n's Gmail credential configuration, then complete the browser-based authorization.

Which OpenAI model works best for email classification in n8n?

GPT-4o-mini offers the best balance of speed, accuracy, and cost for classification. GPT-4o produces higher-quality reply drafts when nuance matters. For simple label-and-archive workflows, GPT-3.5-turbo is fast and cheap.

How do I add persistent conversation memory to an n8n email agent?

Attach a Window Buffer Memory node to the AI Agent node and set the window size to 5-10 messages. For memory that persists across workflow executions and different threads, store conversation history in a database and retrieve it via a Code node at the start of each run.

What is RAG and why does it make n8n email replies more accurate?

RAG (Retrieval-Augmented Generation) pulls relevant documents from a vector store before the agent drafts a reply. Connect a Vector Store Tool node backed by Pinecone or Qdrant to your AI Agent, and it will ground responses in your actual company data rather than guessing.

Can an n8n AI email agent work with Outlook and Microsoft 365?

Yes. n8n has a Microsoft Outlook node that works similarly to the Gmail nodes. You'll register an app in Azure AD, configure mail permissions, and authorize via OAuth2. The AI Agent logic and workflow structure stay identical.

How do I prevent my n8n email agent from sending duplicate replies?

Store processed message IDs in n8n's static data, a Google Sheet, or a database. Add an IF node at the start of your workflow that skips any email with an ID already in your store. This prevents double-sends during workflow restarts or trigger retries.

What Gmail API rate limits should I plan around for n8n email automation?

Regular Gmail accounts allow roughly 250 sends per day. Google Workspace accounts support around 2,000. The API also enforces per-second quota limits that can throttle high-frequency polling. Plan your agent's send volume around these caps before going live.

How do I handle email attachments inside an n8n AI agent workflow?

The Gmail Trigger node can download attachments as binary data. Route them to a Read/Write File node or the Extract From File node for PDFs. You can pass extracted text to the AI Agent as part of the email context for classification or response drafting.

What's the difference between n8n's AI Agent node and a simple LLM chain?

An LLM chain processes text input and returns text output in a single pass. The AI Agent node reasons about which tools to call (vector stores, APIs, calculators) and can execute multi-step workflows. Use the Agent node when your email processing requires decisions, not just text generation.

Can I self-host n8n for GDPR-compliant AI email processing?

Yes. n8n Community Edition is open source and runs on your own server. Email data never passes through n8n's cloud infrastructure. You're responsible for securing the instance, managing updates, and ensuring your LLM provider's data handling also meets GDPR requirements.

How do I safely test an n8n email agent without accidentally sending live replies?

Set the Gmail send node to create drafts instead of sending. Use a separate test email account. Run at least 50 test emails across all classification categories before switching to live mode. Check drafts manually to verify classification accuracy and reply quality.

When should I replace Gmail with a dedicated email service for my n8n agent?

When you need per-agent sending identities, volume beyond Gmail's 250 daily sends, control over SPF/DKIM records, or webhook-based delivery instead of polling. LobsterMail lets each agent provision its own inbox without OAuth setup. See what agents do with email for common use cases.

How do I give each AI agent its own email address and track replies separately?

With Gmail, you need separate Google accounts and OAuth2 credentials for every agent. With LobsterMail, each agent calls createSmartInbox() and gets an address like support-agent@lobstermail.ai in one step. Replies route back to the specific agent's inbox automatically.

Is n8n better than Zapier for AI-powered email workflows?

For AI-heavy email automation, n8n has more flexibility: native AI Agent nodes with tool use, a self-hosted option for data control, and no per-task pricing on the Community Edition. Zapier is easier for simple automations but costs more at volume and offers fewer AI-native features.

Related posts