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.

n8n AI email agent with OpenAI and Gmail integration.

⚙️ 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:

  1. Trigger – Waits for a chat message
  2. AI Agent – Parses intent (send, get, reply)
  3. OpenAI Chat Model – Powers the agent's brain
  4. Gmail Tool – Performs email actions
  5. 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.”

n8n trigger node for receiving chat messages.

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)

n8n AI Agent node setup using Tools Agent.

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.

OpenAI Chat Model node configured in n8n.

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.

Simple Memory node in n8n for storing chat 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.

n8n AI email agent with OpenAI and Gmail integration.


🚀 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:

  1. Activate the workflow
  2. Send chat prompts to trigger the workflow
  3. 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.