# Generate Gmail reply drafts with OpenAI and Postgres: n8n workflow

> Started by an arriving email, and works across Gmail and Postgres. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply...

Source: https://millionrounds.com/workflow/generate-gmail-reply-drafts-with-openai-and-postgres (Millionrounds; every figure on the page links to its filing or article)

[Automation library](https://millionrounds.com/workflows) · [Content](https://millionrounds.com/workflows?category=Content#library)

# Generate Gmail reply drafts with OpenAI and Postgres

Started by an arriving email, and works across Gmail and Postgres. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply rather than from the model alone. 22 steps, 1 branch: large, and worth reading before you trust it with anything live.

Large Calls a model: OpenAI Retrieval

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fgenerate-gmail-reply-drafts-with-openai-and-postgres)

**22 steps** Large: read it before trusting it with anything live

**1** Branch: places the flow splits

**0** Loops or waits

**2** Services it connects to

## The flow

Starts it 1 Model 4 Logic 8 Service 9

The steps in the order they run, from what starts it.

- 1 **OpenAI Chat Model** Lm Chat Open Ai *Model*
- 2 **Simple Memory** Memory Buffer Window *Model*
- 3 **Gmail Trigger** Gmail trigger *Starts it*
- 4 **AI Agent - Classify** Agent *Model*
- 5 **Parse & Validate** Code *Logic*
- 6 **Merge** Merge *Logic*
- 7 **IF Valid** If *Logic*
- 8 **Parse Classification** Code *Logic*
- 9 **DB - Log Skipped** Postgres *Service*
- 10 **Generate Email Embedding** Http Request *Service*
- 11 **Extract Embedding** Code *Logic*
- 12 **DB - Fetch KB Data** Postgres *Service*
- 13 **DB - Fetch Similar Corrections** Postgres *Service*
- 14 **DB - Fetch Similar Scenarios** Postgres *Service*
- 15 **DB - Fetch Customer Transactions** Postgres *Service*
- 16 **Resolve Scenario** Code *Logic*
- 17 **Assemble Draft Prompt** Code *Logic*
- 18 **Generate Draft** Open Ai *Model*
- 19 **Extract Draft Text** Code *Logic*
- 20 **Create Gmail Draft** Gmail *Service*
- 21 **DB - Save AI Draft** Postgres *Service*
- 22 **DB - Log Success** Postgres *Service*

## How it's built

- **Starts on:** An arriving email
- **Steps:** 22, with 1 branch
- **Size:** Large: read it before trusting it with anything live
- **Connects to:** Gmail, Postgres
- **Model:** OpenAI
- **Retrieval:** Answers are drawn from documents you supply, not from the model alone

## Questions

What does it need to run?

An n8n account, plus your own Gmail and Postgres accounts, and a key for OpenAI. You connect your own accounts after importing it; none are included.

What starts it?

It starts on an arriving email.

Does it use AI?

Yes. It calls OpenAI. Its answers draw on documents you give it, not on the model alone. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Take your time. At 22 steps it is large: read it before trusting it with anything live.

How do I import it?

Copy the workflow, open a new workflow in n8n and paste. The steps appear on the canvas, ready for your own accounts.

**Before you run it**

Nothing here has been tested by us. It is somebody's working automation, read and described from its own steps. Treat the connections it wants as a list of accounts it will touch, and read the 22 steps before you point it at anything that matters.

## Run it in n8n

- **Create your n8n account** Free to start. n8n is where the workflow runs. [Create an n8n account ↗](https://millionrounds.com/n8n)
- **Download the workflow** Free with a Millionrounds account. [Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fgenerate-gmail-reply-drafts-with-openai-and-postgres)
- **Paste it into n8n** Open a new workflow in n8n and paste. Then connect your own accounts.

Stored with the author's traces removed: canvas notes, instance identifiers, saved credential names and any live document they had left behind.

## Connects to

Each opens the other workflows that use it.

[Gmail](https://millionrounds.com/workflows?service=Gmail#library)[Postgres](https://millionrounds.com/workflows?service=Postgres#library)

## Workflows like it

Also content, with the same services where possible, closest in size first.

- [**Detect viral YouTube videos with OpenAI and Gmail** 22 steps · calls a model · Gmail](https://millionrounds.com/workflow/detect-viral-youtube-videos-with-openai-and-gmail)
- [**English lesson generator from podcasts with RSS and OpenAI** 22 steps · calls a model · Gmail, RSS](https://millionrounds.com/workflow/english-lesson-generator-from-podcasts-with-rss-and-openai)
- [**Extract video metadata & auto-upload to YouTube with OpenAI** 22 steps · calls a model · Gmail, Google Sheets](https://millionrounds.com/workflow/extract-video-metadata-and-auto-upload-to-youtube-with-opena)
- [**Generate real-estate marketing images and videos with OpenAI** 22 steps · calls a model · Gmail, Google Drive](https://millionrounds.com/workflow/generate-real-estate-marketing-images-and-videos-with-openai)
- [**Refined post generator with Gmail** 22 steps · calls a model · Gmail, Google Sheets](https://millionrounds.com/workflow/refined-post-generator-with-gmail)
- [**Transcribe long audio files beyond 25MB limit with FileFlows** 22 steps · calls a model · Gmail](https://millionrounds.com/workflow/transcribe-long-audio-files-beyond-25mb-limit-with-fileflows)
