Generate Gmail reply drafts with GPT-4o, pgvector, and PostgreSQL
Ready for 3.0Ready for 3.0. This template uses no nodes that n8n 3.0 removes. No removed nodes, versions or helpers found. Static check, last run 2026-10-19.
Worth a look
- Gmail Trigger below v1.4 runs as v1.4 (drafts skipped, max emails per poll) (Gmail Trigger)
Nodes used (11)
- Code
n8n-nodes-base.code - If
n8n-nodes-base.if - OpenAI Chat Model
@n8n/n8n-nodes-langchain.lmChatOpenAi - Simple Memory
@n8n/n8n-nodes-langchain.memoryBufferWindow - AI Agent
@n8n/n8n-nodes-langchain.agent - Merge
n8n-nodes-base.merge - HTTP Request
n8n-nodes-base.httpRequest - Postgres
n8n-nodes-base.postgres - OpenAI
@n8n/n8n-nodes-langchain.openAi - Gmail
n8n-nodes-base.gmail - Gmail Trigger
n8n-nodes-base.gmailTrigger
What it does
Email Automation β AI Draft Generator n8n Workflow Template --- π Description This workflow is the core of a three-part email automation system. It monitors your Gmail inbox in real time, classifies every inbound customer email with AI, retrieves contextually relevant data from your Knowledge Base and transaction records, and generates a personalised, ready-to-send draft reply β deposited directly into Gmail Drafts for one-click human review and send. No more opening each email, reading the context, checking the transaction status, and writing a reply from scratch. The workflow does all of that automatically. Your support team only reviews and sends. The draft quality comes from three sources working together: a Knowledge Base built from your own historical support emails, real correction examples showing how your team has responded to similar situations in the past, and live transaction data pulled for the specific customer who wrote in. The result is a draft that reflects your team's actual tone, references accurate order details, and follows the correct resolution path based on the customer's transaction status. This is the main production workflow. It depends on two companion workflows that must be set up first: Workflow 1 β KB Builder: Historical Emails (free) seeds the Knowledge Base from your existing Gmail inbox. Run it once before activating this workflow. Workflow 2 β Self-Learning Loop (free) continuously improves draft quality by recording the difference between AI-generated drafts and what your team actually sent. Over time, the AI learns your team's preferred phrasing and corrections. --- βοΈ What This Workflow Does (Step-by-Step) π¬ Gmail Trigger β Polls your inbox for new unread emails matching a configured label. Fires once per new message automatically. π Parse & Validate β Extracts all email fields and applies a multi-signal filter. Drops marketing emails, no-reply senders, bounce notifications, spam, automated financial alerts (Razorpay, NSDL, Digio, etc.), and OTP/transaction alert subjects. Only genuine customer emails proceed. π€ AI Agent β Classify β Sends the email to GPT-4o-mini, which returns a structured JSON classification: category, subcategory, urgency, sentiment, language, summary, key entities, and a confidence score. π Merge + Parse Classification β Merges the AI output back with the original email fields and parses the JSON into clean structured data for all downstream nodes. π’ Generate & Extract Embedding β Calls the OpenAI Embeddings API to generate a 1536-dimension vector of the email content. Used to run semantic similarity searches across all three KB tables simultaneously. ποΈ Four Parallel DB Fetches β Triggered simultaneously from the embedding node: - DB - Fetch KB Data β Top 5 semantically similar Q&A pairs from kbdata - DB - Fetch Similar Corrections β Top 3 real customer-reply pairs from corrections - DB - Fetch Similar Scenarios β Top 3 matching handling patterns from scenariopatterns - DB - Fetch Customer Transactions β Last 5 transactions for this customer's email address from the transactions table β‘ Resolve Scenario β The intelligence core of the workflow. Analyses the customer's transactions and email content together to determine the exact scenario and pre-compose the transaction status message. Handles 12+ distinct scenarios including: NSDL transferred, payment failed, payment pending, accepted but shares pending, refunded, rejected, multiple unresolved transactions, missing shares queries, order ID mentioned but not found, and no transaction on record. Each scenario has a precise templated response that is passed to the AI for tone-matching β not free generation. π Assemble Draft Prompt β Builds the full system prompt and user prompt for the draft generation step. Injects KB context, correction examples, scenario patterns, and the transaction-specific message as hard constraints the AI must follow. βοΈ Generate Draft (GPT-4o) β GPT-4o drafts the reply at temperature 0.