DEV Community

Cover image for Build an AI Email Lead Follow-Up Workflow with n8n, OpenAI & Gmail
Hashim khan
Hashim khan

Posted on

Build an AI Email Lead Follow-Up Workflow with n8n, OpenAI & Gmail

Responding to every new business enquiry manually sounds simple until the number of leads starts increasing.

A lead sends an email, someone reads it, decides whether it is relevant, writes a response, records the lead somewhere, and remembers to follow up later.

This entire process can be automated with n8n + OpenAI + Gmail + Google Sheets.

In this tutorial, we'll build a workflow that:

Detects new lead emails
Extracts lead information
Uses OpenAI to qualify the lead
Generates a personalized response
Sends the email automatically
Stores the lead in Google Sheets
Creates a follow-up task for qualified leads
Workflow Architecture
Gmail Trigger
↓
Extract Email Data
↓
OpenAI Lead Qualification
↓
Structured JSON Output
↓
IF Qualified?
↙ ↘
YES NO
↓ ↓
Generate Generate
Reply Basic Reply
↓ ↓
Gmail Send ←──┘
↓
Google Sheets
↓
Follow-up

  1. What You Need

You will need:

n8n Cloud or self-hosted n8n
Gmail account
OpenAI API access
Google Sheets
A Google Cloud project for OAuth credentials

The workflow can be built entirely with n8n nodes, so you don't need to create a separate backend application.

  1. Create the Gmail Trigger

Start with the Gmail Trigger node.

Configure it to watch for new emails.

For example:

Event: Message Received
Mailbox: Inbox

You can also use Gmail labels to make the workflow process only specific lead emails.

For example:

Label: New-Leads

This is useful if your inbox contains newsletters, notifications, invoices, and other emails that shouldn't enter the lead automation.

  1. Extract the Lead Information

Next, add a Code node.

The goal is to create a clean structure that the AI can understand.

Example:

const email = $json;

return [{
json: {
name: email.from?.value?.[0]?.name || "",
email: email.from?.value?.[0]?.address || "",
subject: email.subject || "",
message: email.textPlain || email.snippet || ""
}
}];

Now the workflow has a simple structure:

{
"name": "John Smith",
"email": "john@example.com",
"subject": "AI automation enquiry",
"message": "We need help automating our sales follow-ups."
}

Keeping the data structured before sending it to an AI model makes the next step much easier to control.

  1. Qualify the Lead with OpenAI

Add an OpenAI node.

Instead of asking the model for a normal paragraph, ask it to return structured information.

Example prompt:

You are a B2B lead qualification assistant.

Analyze the incoming email and classify the lead.

Return ONLY valid JSON using this structure:

{
"lead_score": 0,
"qualification": "hot",
"service_interest": "",
"business_type": "",
"summary": "",
"reason": "",
"recommended_action": ""
}

Scoring rules:

80-100 = Hot
50-79 = Warm
0-49 = Cold

Consider:

  • Clear business requirement
  • Budget or buying intent
  • Timeline
  • Relevance to our services

Email:

Name: {{ $json.name }}

Subject: {{ $json.subject }}

Message:
{{ $json.message }}

A possible response could be:

{
"lead_score": 91,
"qualification": "hot",
"service_interest": "AI lead follow-up automation",
"business_type": "Real estate",
"summary": "The company wants to automate follow-ups for inbound property enquiries.",
"reason": "Clear business requirement and implementation intent.",
"recommended_action": "Book a discovery call within 24 hours."
}

  1. Add Validation Before Taking Action

This is an important part of production AI automation.

Don't allow an AI model to directly perform every business action.

Use this pattern:

AI
↓
Structured Output
↓
Validation
↓
Business Rules
↓
Action

For example, add an IF node:

lead_score >= 80

If true, send the lead through the hot-lead workflow.

If false, use the normal lead workflow.

This gives you more control over AI-generated decisions.

  1. Generate the Personalized Email

For qualified leads, add another OpenAI node.

Prompt:

Write a short professional sales reply to this lead.

Requirements:

  • Address the person by name
  • Mention their specific requirement
  • Do not exaggerate
  • Keep the email under 150 words
  • Suggest a discovery call
  • Use a natural human tone
  • Do not mention that AI generated the email

Lead:

Name: {{ $json.name }}

Business: {{ $json.business_type }}

Requirement:
{{ $json.service_interest }}

Summary:
{{ $json.summary }}

The result could look like:

Hi John,

Thanks for reaching out.

It sounds like you're looking to automate follow-ups for your property enquiries. We can help build a workflow that captures new leads, qualifies them and automatically follows up with the right prospects.

I'd be happy to understand your current process and see what could be automated.

Would you be available for a quick call this week?

Best,
Hashim

  1. Send the Email with Gmail

Connect the OpenAI output to a Gmail node.

Configure:

To:
{{ lead email }}

Subject:
Re: {{ original subject }}

Message:
{{ AI generated response }}

Now the workflow can respond automatically whenever a new lead arrives.

  1. Store the Lead in Google Sheets

Add a Google Sheets node.

Create columns such as:

Date
Name
Email
Business
Requirement
Lead Score
Qualification
Recommended Action
Status

Example:

Name Score Qualification Status
John Smith 91 Hot Contacted
Sarah Lee 67 Warm Follow-up
Mike Jones 32 Cold Nurture

This gives the sales team a simple lead database without requiring a separate CRM.

  1. Automate the Follow-Up

The workflow can also create a follow-up process.

For example:

New Lead
↓
Immediate Reply
↓
Wait 24 Hours
↓
Check Lead Status
↓
No Response?
↓
Send Follow-Up

In n8n, you can use the Wait node for this.

A follow-up message could be:

Hi John,

Just following up on my previous email.

If automating your lead follow-up process is still a priority, I'd be happy to discuss your current workflow and possible improvements.

Best,
Hashim

  1. Complete Workflow

The final n8n workflow looks like:

Gmail Trigger
↓
Code
↓
OpenAI
↓
Structured Output
↓
IF
↙ ↘
Hot Warm/Cold
↓ ↓
OpenAI OpenAI
↓ ↓
Gmail Gmail
↘ ↙
Google Sheets
↓
Follow-Up

This architecture is simple enough for a small business but can also be expanded into a much larger sales automation system.

  1. Important Production Considerations

Before using this workflow with real leads, add proper safeguards.

Validate AI output

Don't assume the model will always return perfect JSON.

Validate required fields before continuing.

Prevent duplicate replies

Store the Gmail message ID or thread ID and check whether it has already been processed.

Add human approval

For high-value leads, you may want:

AI qualification
↓
Human approval
↓
Send email

instead of fully automatic sending.

Protect customer data

Avoid sending unnecessary personal information to external AI services.

Only process the information required for the workflow.

Handle failures

Use n8n's error handling so API failures don't silently stop the lead pipeline.

Conclusion

n8n makes it possible to connect email, AI, databases and business applications into a single automated workflow.

The important part isn't simply adding an AI node.

A reliable automation should follow:

Trigger
→ Extract
→ AI Analysis
→ Structured Output
→ Validation
→ Business Rules
→ Action
→ Logging

This pattern can be adapted for sales enquiries, support emails, recruitment applications, appointment requests and many other business processes.

For more AI automation workflows and implementation ideas, visit Aiotagen: https://aiotagen.com/

Top comments (0)