In this tutorial, we'll walk through building an AI-powered email assistant using n8n, OpenAI, and Gmail. This agent will send, retrieve, and reply to emails—entirely driven by natural language.
⚙️ What You'll Build
A conversational email agent that can:
- Send emails via AI prompts
- Read your inbox
- Reply to messages
- Maintain context across chats
Let’s dive in.
🧠 Prerequisites
- An n8n instance (self-hosted or cloud)
- Gmail OAuth2 credentials set up in n8n
- OpenAI API credentials
- Basic familiarity with n8n workflows
🗺️ Workflow Blueprint
Here's the high-level flow:
- Trigger – Waits for a chat message
- AI Agent – Parses intent (send, get, reply)
- OpenAI Chat Model – Powers the agent's brain
- Gmail Tool – Performs email actions
- Memory Buffer – Maintains chat context
🔧 Step-by-Step Setup
1. Trigger: Start When a Chat Message Is Received
Use the When chat message received
node. This will simulate a user sending instructions to the agent, like:
Send an email to [email protected] saying “Meeting moved to 3 PM.”
2. AI Agent: Interpret the Command
Add an AI Agent
node from the Advanced AI collection.
-
Prompt Type:
Define Below
-
Text:
{{ $json.chatInput }}
- Connect this to:
- Language Model (next step)
- Memory (Simple Memory node)
- Tools (Gmail nodes)
3. LLM: Add the OpenAI Chat Model
Use the OpenAI Chat Model
node.
-
Model:
gpt-4o-mini
(or another OpenAI model) - Provide your OpenAI API credentials
Connect this to the AI Agent
via Chat Model
.
4. Memory: Store Chat Context
Add a Simple Memory
node.
- Connect it to the AI Agent via
Memory
.
This allows your agent to remember previous commands and carry conversation context.
📬 Gmail Tool Integration
You’ll need to authenticate with Gmail via OAuth2 in n8n.
a) Send Emails
Add a Gmail Tool
node:
-
Operation:
Send
-
To:
{{ $fromAI('To') }}
-
Subject:
{{ $fromAI('Subject') }}
-
Message:
{{ $fromAI('Message') }}
Connect this to the AI Agent via Tool
.
b) Get Recent Emails
Add another Gmail Tool
node:
-
Operation:
Get Many
-
Limit:
10
- Same credentials, connected to the AI Agent.
c) Reply to Emails
Add one more Gmail Tool
node:
-
Operation:
Reply
-
Message ID:
{{ $fromAI('Message_ID') }}
-
Message:
{{ $fromAI('Message') }}
Same connection and credential setup.
🚀 Example Prompts
Try any of these in your chat interface:
Send an email to [email protected] with subject “Invoice” and say “Please find the attached invoice.”
Show me my latest 5 emails.
Reply to the email from Jane saying “Thanks, sounds good!”
🧪 Test & Deploy
Once everything is connected:
- Activate the workflow
- Send chat prompts to trigger the workflow
- Watch your AI assistant handle your emails ✨
📦 Final Thoughts
This Email Agent leverages the power of LLMs and n8n's automation to streamline your inbox. Expand this further by integrating file attachments, smart filtering, or even calendar scheduling.
💡 Pro tip: You can extend this to other email platforms like Outlook with minimal changes.