Analyze, classify, and summarize Gmail with OpenAI RAG and Google Sheets

Go to Workflow
0 views
Built by Amirhosein Zahedi Amirhosein Zahedi
Created on June 12, 2026

Description

📧 Analyze, classify, and summarize emails using RAG (automatic taxonomy learning)


This workflow automatically reads incoming Gmail messages, analyzes them using AI with a retrieval-augmented
classification system (RAG), organizes emails into structured categories, stores results in Google Sheets,
and even generates an audio summary sent directly to Telegram.




⚙️ How It Works


The workflow creates an intelligent email processing pipeline combining Gmail, OpenAI, vector embeddings,
and Google Sheets to continuously learn and improve email categorization accuracy.




Email Trigger
The workflow starts automatically whenever a new email arrives in Gmail using the Gmail Trigger node.



Email Extraction & Cleaning
The full email content, sender information, subject, and timestamp are retrieved and normalized.
HTML formatting is removed so the AI receives clean text data.



Knowledge Base Loading (RAG)
Email tagging examples stored in a Google Sheets document are loaded and converted into embeddings.
These examples act as the official taxonomy for categories and subcategories.



Vector Store Creation
The workflow builds an in-memory vector database containing historical tagging samples.
This allows semantic similarity search during classification.



AI Analysis Agent
An OpenAI-powered AI Agent performs multiple tasks in a single step:

Summarizes the email (short paragraph)
Creates a one-line short message
Classifies the email using retrieved vector examples
Extracts keywords
Assigns confidence score
Detects whether a new category or subcategory was created


The agent strictly prioritizes retrieved taxonomy examples to maintain consistent labeling.



Structured Data Storage
Processed email data — including summary, category, keywords, and metadata — is appended to a Google Sheets log,
creating a searchable email intelligence database.



Self-Learning Taxonomy
If the AI determines a new category or subcategory is required:

The new label is automatically added to the tagging samples sheet
An admin notification is sent via Telegram

This allows the system to evolve over time.



Audio Summary Generation
The summarized email text is converted into speech using OpenAI audio generation.



Telegram Notification
The generated audio summary is automatically delivered to Telegram, allowing quick email review without reading.





🧩 Features


✅ Fully automated Gmail monitoring
✅ Retrieval-Augmented Generation (RAG) email classification
✅ Consistent taxonomy enforcement via vector similarity
✅ Automatic keyword extraction & confidence scoring
✅ Self-updating category knowledge base
✅ Google Sheets email database
✅ AI-generated voice summaries
✅ Telegram admin notifications
✅ Continuously improving classification accuracy




🚀 Setup Steps

1. Required Accounts

Google account (Gmail + Google Sheets)
OpenAI API account
Telegram Bot
n8n instance (Cloud or Self-hosted)


2. Configure Credentials in n8n
Create the following credentials inside n8n:

Gmail OAuth2 – used by Gmail Trigger and message retrieval
Google Sheets OAuth2 – for email storage and taxonomy dataset
OpenAI API – for embeddings, AI analysis, and audio generation
Telegram API – for sending notifications and audio summaries


3. Prepare Google Sheets

Sheet 1 — Email Log

date
email
name
subject
summarized text
category
subcategory
keywords


Sheet 2 — Tagging Samples (Knowledge Base)

id
subject
email_text
category
subcategory
keywords



This sheet acts as the classification training reference used by the vector store.


4. Configure Telegram

Create a Telegram bot using BotFather
Copy the bot token into n8n credentials
Replace the chatId value with your Telegram user or group ID


5. Connect OpenAI

Add your OpenAI API key
Ensure access to chat models and embeddings
No additional configuration required


6. Activate the Workflow

Enable the workflow
Send a test email to your Gmail inbox
Verify that:

Email appears in Google Sheets
Categories are assigned
Audio summary arrives in Telegram






💡 Recommended Usage


Email triage automation
Customer support inbox classification
Sales lead categorization
Personal productivity systems
AI-assisted knowledge management




🧠 Architecture Notes


This workflow uses a Retrieval-Augmented Generation (RAG) pattern inside n8n by combining
Google Sheets as a lightweight knowledge base with an in-memory vector store.
Instead of relying purely on model reasoning, classification decisions are grounded
in previously approved examples, producing stable and explainable categorization.



Over time, the workflow becomes smarter as new categories are automatically added,
effectively turning your inbox into a continuously learning AI system.

Nodes Used (10)

AI Agent
@n8n/n8n-nodes-langchain.agent
Code
n8n-nodes-base.code
Default Data Loader
@n8n/n8n-nodes-langchain.documentDefaultDataLoader
Embeddings OpenAI
@n8n/n8n-nodes-langchain.embeddingsOpenAi
Gmail
n8n-nodes-base.gmail
Google Sheets
n8n-nodes-base.googleSheets
OpenAI
@n8n/n8n-nodes-langchain.openAi
OpenAI Chat Model
@n8n/n8n-nodes-langchain.lmChatOpenAi
Simple Vector Store
@n8n/n8n-nodes-langchain.vectorStoreInMemory
Telegram
n8n-nodes-base.telegram