Skip to main content

Generate Gmail reply drafts with GPT-4o, pgvector, and PostgreSQL

Workflow preview

Workflow preview
100%
Generate Gmail reply drafts with GPT-4o, pgvector, and PostgreSQL preview
Open on n8n.io

1. Workflow Overview

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 e...

Best for

  • Ticket Management automation workflows
  • AI RAG automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.code, n8n-nodes-base.if, @n8n/n8n-nodes-langchain.lmchatopenai, @n8n/n8n-nodes-langchain.memorybufferwindow, @n8n/n8n-nodes-langchain.agent, n8n-nodes-base.merge, n8n-nodes-base.httprequest, n8n-nodes-base.postgres

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Vivekanand M.

Original n8n.io source

1.1 Workflow description

Title
Generate Gmail reply drafts with GPT-4o, pgvector, and PostgreSQL
Workflow name
Generate Gmail reply drafts with GPT-4o, pgvector, and PostgreSQL

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 kb_data
  • DB - Fetch Similar Corrections — Top 3 real customer-reply pairs from corrections
  • DB - Fetch Similar Scenarios — Top 3 matching handling patterns from scenario_patterns
  • 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.3. It follows the injected transaction message precisely, matches the customer's detected language, applies tone and formatting rules, and signs off as your team. Constrained generation — not open-ended.

📄 Extract Draft Text — Parses the AI response, fixes formatting (paragraph spacing, sign-off placement, line breaks), and converts to HTML for Gmail compatibility.

📥 Create Gmail Draft — Saves the formatted reply as a Gmail Draft, threaded directly under the original customer email. Your team opens Gmail, sees the draft pre-populated, reviews, edits if needed, and sends.

💾 DB - Save AI Draft — Writes the full draft to the ai_drafts table: thread ID, message ID, sender, subject, original body, classification, and draft text. Used by the Self-Learning Loop to capture what the AI produced.

✅ DB - Log Success — Writes a success record to email_logs with category, urgency, confidence score, and draft ID. Creates a full audit trail of every email processed.

🚫 DB - Log Skipped — For emails that fail the Parse & Validate filter, a skipped record is written to email_logs with the skip reason. Nothing is lost — every email is accounted for.


🧩 Prerequisites

  • Gmail account — OAuth2 credential. Must be the support inbox. Create a Gmail label for incoming support emails and configure it in the Get Many Messages node.
  • OpenAI API key — Used for GPT-4o-mini classification, text-embedding-3-small for vector search, and GPT-4o for draft generation.
  • PostgreSQL database — With pgvector extension enabled. Requires five tables as described in the schema below.
  • KB Builder workflow — Run at least once before activating this workflow to seed the Knowledge Base. Without KB data, the AI will still generate drafts but without contextual grounding.
  • n8n instance — Self-hosted or cloud. Requires PostgreSQL, Gmail, OpenAI, and HTTP Request nodes.

🗄️ Database Schema

Table: transactions

Field Type Notes
customer_email TEXT Used to match inbound email sender
buyer_name TEXT Customer display name
order_id TEXT Unique order identifier
transaction_type TEXT Buy / Sell
user_amount NUMERIC Amount paid
units NUMERIC Shares purchased
unit_price NUMERIC Per-share price
isin TEXT Share ISIN code
demat_number TEXT Customer demat account
payment_method TEXT NEFT / UPI / etc.
payment_status TEXT success / failed / created
admin_action TEXT pending / accepted / refunded / rejected
nsdl_checked BOOLEAN Whether shares have been transferred
nsdl_timestamp TIMESTAMP Date of NSDL transfer
transaction_date TIMESTAMP Transaction date

Table: ai_drafts

Field Type Notes
id SERIAL PRIMARY KEY
gmail_thread_id TEXT Gmail thread ID
gmail_message_id TEXT Gmail message ID
sender_email TEXT Customer email address
subject TEXT Email subject
original_email_body TEXT Customer's original message
classification TEXT AI-assigned category
ai_draft_text TEXT The generated draft
draft_saved BOOLEAN Whether draft was saved to Gmail
created_at TIMESTAMP

Table: email_logs

Field Type Notes
id SERIAL PRIMARY KEY
gmail_thread_id TEXT
gmail_message_id TEXT
sender_email TEXT
subject TEXT
classification TEXT
status TEXT draft_saved / skipped
draft_id INTEGER Reference to ai_drafts
notes TEXT Category, urgency, confidence
processed_at TIMESTAMP

Tables: kb_data, scenario_patterns, corrections

Populated by the KB Builder workflow. See that workflow's README for full schema.


💰 Cost Estimate

Item Estimated Cost
GPT-4o-mini classification ~$0.002–0.005 per email
text-embedding-3-small ~$0.0001 per email
GPT-4o draft generation ~$0.01–0.03 per email
Total per email ~$0.012–0.035
100 emails/day ~$1.20–3.50/day
PostgreSQL + pgvector (self-hosted) ~$5–15/mo
n8n self-hosted (AWS t3.small) ~$10–15/mo

⚙️ Setup Instructions

  1. Run KB Builder first — Import at least 50–100 historical emails before activating this workflow. The richer the KB, the better the draft quality from day one.

  2. Gmail — Connect Gmail OAuth2 in n8n. Create a dedicated label for support emails (e.g. "Support Inbox"). Set that label ID in the Get Many Messages node filters.

  3. OpenAI — Add your OpenAI API key as an n8n credential. Used by three nodes: AI Agent - Classify, Generate Email Embedding, and Generate Draft.

  4. PostgreSQL — Connect your database. Enable pgvector (CREATE EXTENSION IF NOT EXISTS vector). Ensure all five tables exist.

  5. Customise Parse & Validate — Update the automatedSenders and automatedSubjects arrays to match the automated senders specific to your business. Update internalDomains with your support team's email domain.

  6. Customise the AI prompt — Open Assemble Draft Prompt and update the company name, tone rules, sign-off, and language instructions to match your brand and team style.

  7. Customise Resolve Scenario — Update the scenario logic and templated responses in the Resolve Scenario node to match your actual transaction statuses, products, and resolution paths.

  8. Activate the workflow — Click Activate. The Gmail trigger will begin polling for new unread emails automatically.


💡 Key Benefits

Fully automated draft generation — every inbound email gets a contextual, accurate reply draft without any manual effort ✔ Transaction-aware — pulls live order data and applies the correct resolution template based on payment status, admin action, and NSDL transfer status ✔ KB-grounded — drafts are informed by your own historical support knowledge, not generic AI outputs ✔ Self-improving — the Self-Learning Loop workflow captures human edits and feeds them back into the corrections table, continuously improving future drafts ✔ Human stays in control — every draft lands in Gmail Drafts for review. Nothing is sent automatically ✔ Full audit trail — every email processed, skipped, or drafted is logged to the database with classification and confidence data ✔ Language-aware — detects customer language and instructs the AI to match it in the reply ✔ Modular — swap GPT-4o for Claude or any other model with minimal changes to the Generate Draft node


👥 Perfect For

  • Customer support teams handling high volumes of transaction-related emails
  • Fintech and investment platforms where reply accuracy and transaction context are critical
  • Small support teams who want AI assistance without losing human oversight
  • Any business running Gmail-based support that wants to cut reply time without cutting quality

🔗 Part of a Three-Workflow System

Workflow 1 — KB Builder: Historical Emails (free, run once) — Seeds the Knowledge Base from your existing inbox Workflow 2 — Self-Learning Loop (free, always-on) — Captures human edits to improve future drafts Workflow 3 — Email Automation: AI Draft Generator (this workflow) — The live production workflow

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Parse & Validate

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 2 - IF Valid

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.3

Block 3 - OpenAI Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenAi - lmChatOpenAi
Config choices
Version 1.3

Block 4 - Simple Memory

Type / Role
@n8n/n8n-nodes-langchain.memoryBufferWindow - memoryBufferWindow
Config choices
Version 1.3

Block 5 - AI Agent - Classify

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 3.1

Block 6 - Merge

Type / Role
n8n-nodes-base.merge - merge
Config choices
Version 3.2

Block 7 - Parse Classification

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 8 - Generate Email Embedding

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.4

Block 9 - Extract Embedding

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 10 - DB - Fetch KB Data

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 11 - DB - Fetch Similar Corrections

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 12 - DB - Fetch Similar Scenarios

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 13 - DB - Fetch Customer Transactions

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 14 - Resolve Scenario

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 15 - Assemble Draft Prompt

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 16 - Generate Draft

Type / Role
@n8n/n8n-nodes-langchain.openAi - openAi
Config choices
Version 2.1

Block 17 - Extract Draft Text

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 18 - Create Gmail Draft

Type / Role
n8n-nodes-base.gmail - gmail
Config choices
Version 2.2

Block 19 - DB - Save AI Draft

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 20 - DB - Log Success

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 21 - DB - Log Skipped

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 22 - Gmail Trigger

Type / Role
n8n-nodes-base.gmailTrigger - gmailTrigger
Config choices
Version 1.3

Block 23 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 24 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Showing the first 24 of 26 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Generate Gmail reply drafts with GPT-4o, pgvector, and PostgreSQL
Complexity advanced
Nodes 26
Categories Ticket Management, AI RAG
Author Vivekanand M
Published 24 Apr 2026

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/15272/15272.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Generate Gmail reply drafts with GPT-4o, pgvector, and PostgreSQL do?

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 e...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Ticket Management, AI RAG use case.