• Solan Sync
  • Posts
  • How to Use n8n + GPT‐4o for Automating Smart Email Drafts in Gmail

How to Use n8n + GPT‐4o for Automating Smart Email Drafts in Gmail

Automate email management using n8n, OpenAI, and Gmail. Learn how to build an AI agent that classifies emails, drafts replies, and streamlines your workflow—ideal for engineers, founders, and automation enthusiasts.

Managing email can easily eat up as much time as your actual job — especially for engineers, founders, and anyone dealing with constant notifications, partner requests, and internal updates. The inbox fills up, and productivity drops. But with modern AI tools, you can automate away most of that pain.

Here’s how I built a smart email agent using n8n, OpenAI’s GPT-4o, and Gmail to automatically classify incoming messages, generate draft replies, and save them for your review. In this article, I’ll explain how the system works, how to build it, and why it’s increasingly relevant in today’s fast-paced business environment.

What the System Does

Every hour, the agent checks your Gmail inbox for new messages and:

  • 🧠 Classifies each email using AI (e.g., sponsorship, high-priority, normal)

  • 🤖 Generates context-aware draft replies with GPT-4o-mini

  • 📬 Saves responses as Gmail drafts (for your review and manual send)

With this setup, you reduce manual effort and ensure important messages get prompt, relevant attention.

How It Works: Workflow Overview

This solution runs on n8n, an open-source automation platform with native integration for Google, OpenAI, and LangChain. Here’s the step-by-step breakdown:

1. Trigger: Gmail Listener

  • Uses OAuth2 authentication to detect new emails every hour.

2. AI-Powered Classification

  • The LangChain text classifier node analyzes email subject and body, sorting messages into categories:

    • Sponsorship: Collaboration requests

    • Notification: Urgent messages

    • Normal: Everything else

3. GPT-4o-mini for Draft Reply Generation

  • Depending on the classification, the system routes emails to one of two OpenAI-powered reply generators:

    • High Priority: Polite, concise, and appropriate responses for internal or urgent notifications

    • Sponsorship: Warm, positive replies asking for details (deliverables, timeline, budget), signed as the company representative

4. Draft Saved in Gmail

  • The generated subject and body are passed to the Gmail node, creating a draft reply for your review (nothing is sent automatically).

How to Set It Up

  1. Log in to n8n (https://n8n.io/)

  2. Create a new workflow

  3. Select “Import file” and upload the provided JSON workflow file

  4. Configure the Gmail node:

    • Double-click to create new credentials

    • Sign in with Google and grant access

  5. Adjust OpenAI settings (free or paid account)

  6. Complete setup for remaining nodes

  7. Tweak prompts and classification rules as needed

How It Works: Workflow Overview

This solution runs on n8n, an open-source automation platform with native integration for Google, OpenAI, and LangChain. Here’s the step-by-step breakdown:

1. Trigger: Gmail Listener

  • Uses OAuth2 authentication to detect new emails every hour.

2. AI-Powered Classification

  • The LangChain text classifier node analyzes email subject and body, sorting messages into categories:

    • Sponsorship: Collaboration requests

    • Notification: Urgent messages

    • Normal: Everything else

3. GPT-4o-mini for Draft Reply Generation

  • Depending on the classification, the system routes emails to one of two OpenAI-powered reply generators:

    • High Priority: Polite, concise, and appropriate responses for internal or urgent notifications

    • Sponsorship: Warm, positive replies asking for details (deliverables, timeline, budget), signed as the company representative

4. Draft Saved in Gmail

  • The generated subject and body are passed to the Gmail node, creating a draft reply for your review (nothing is sent automatically).

How to Set It Up

  1. Log in to n8n (https://n8n.io/)

  2. Create a new workflow

  3. Select “Import file” and upload the provided JSON workflow file

  4. Configure the Gmail node:

    • Double-click to create new credentials

    • Sign in with Google and grant access

  5. Adjust OpenAI settings (free or paid account)

  6. Complete setup for remaining nodes

  7. Tweak prompts and classification rules as needed

Why This Matters

  • Focus and Efficiency: Offload repetitive back-and-forth so you can focus on core engineering or business tasks.

  • Scalable for Teams: Modular, customizable architecture works for freelancers and large organizations alike.

  • Proven AI Use Case: Shows practical, safe, and effective OpenAI deployment.

  • Accelerates Digital Transformation: Meets the growing need for smart, automated systems.

  • Adaptable Across Industries: With a few tweaks, the workflow can be used in legal, healthcare, finance, HR, and more.

Security Considerations

  • All sensitive data is processed on n8n’s secure runtime

  • Gmail integration uses OAuth2 authentication

  • GPT requests require authenticated API keys with usage restrictions

  • No data is stored or logged outside your own email account

Extending the Workflow

This project is just a starting point for more advanced AI-driven communication systems. Possible next steps include:

  • Integration with Slack, Notion, and other collaboration tools

  • Similar email routing using Pinecone vector search

  • Sentiment analysis before sending replies

  • Automatic sending if AI confidence is high enough

Final Thoughts

This is more than just a productivity hack—it’s a concrete example of how AI can expand your capabilities and help you reclaim time for higher-value work. By combining cloud tools, open-source automation, and large language models, anyone can start to build intelligent workflows that evolve with your needs.

This workflow is ideal for engineers, founders, and anyone interested in automation. Try importing the JSON and tailoring it to your use case.

Download the Workflow

You can import the .json workflow file directly into n8n and adjust prompts, Gmail settings, and classification logic as needed.

{
  "name": "Email Draft AI Agent",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyHour"
            }
          ]
        },
        "filters": {}
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [-340, 0],
      "id": "56124e5b-80d0-4e53-903f-bd8f4b2dffcc",
      "name": "Gmail Trigger",
      "credentials": {
        "gmailOAuth2": {
          "id": "__REDACTED__",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "inputText": "=Classify this email:\\n\\nSubject: {{ $json.Subject }}\\n\\nBody: {{$json[\"snippet\"]}}\\n\\nRespond with: HIGH_PRIORITY, NORMAL_PRIORITY, or NOT_SPONSORSHIP",
        "categories": {
          "categories": [
            {
              "category": "Sponsorship",
              "description": "This node uses natural language processing to analyze incoming email content and classify whether the message relates to sponsorship opportunities."
            },
            {
              "category": "Notifications",
              "description": "High priority emails often include words like urgent, notification, or similar."
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "typeVersion": 1,
      "position": [-120, 0],
      "id": "08534172-c4d3-4c8f-b026-fb496332d389",
      "name": "Text Classifier"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [-220, 260],
      "id": "a5f0a5c3-10fd-492c-9f9b-16f166ab7aa2",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "__REDACTED__",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "GPT-4O-MINI"
        },
        "messages": {
          "values": [
            {
              "content": "=You are an assistant of Software Engineer Adnan Latif.\nYour job is to respond to the email below.\n\nCraft a full reply including a subject line, greeting, body, and closing.\nUse the original subject from the email to create a reply subject like \"Re: {{ $json.Subject }}\".\n\n---\n\n**Email Subject:** {{ $json.Subject }}\n**Email Body:** {{ $json.textPlain || $json.textHtml || $json.snippet }}",
              "role": "system"
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [260, -80],
      "id": "b6ba6de9-a3e3-436b-b84a-2629b29841b5",
      "name": "High Priority",
      "credentials": {
        "openAiApi": {
          "id": "__REDACTED__",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ $json.message.content.subject }}",
        "message": "={{ $json.message.content.body }}",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [620, -80],
      "id": "c73cca4d-abc6-4b2e-b369-4b8997a6c8ef",
      "name": "Save Draft - High Priority",
      "credentials": {
        "gmailOAuth2": {
          "id": "__REDACTED__",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "GPT-4O-MINI"
        },
        "messages": {
          "values": [
            {
              "content": "=You are an assistant of Software Engineer Adnan Latif.\nYour job is to respond to the email below.\n\nCraft a full reply to a high-priority sponsorship email.\nInclude a subject line, greeting, body, and closing.\nUse the original subject as \"Re: {{ $json.Subject }}\"\nSign off as a representative of Tkxel.\n\n---\n\n**Email Subject:** {{ $json.Subject }}\n**Email Body:** {{ $json.textPlain || $json.textHtml || $json.snippet }}",
              "role": "system"
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [300, 180],
      "id": "98679d6b-7a8c-4422-8127-5cc3c9a7f596",
      "name": "Sponsorship Reply",
      "credentials": {
        "openAiApi": {
          "id": "__REDACTED__",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ $json.message.content.subject }}",
        "message": "={{ $json.message.content.body }}",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [660, 180],
      "id": "c474c159-8b9d-47bc-bd77-d62a8cc1749c",
      "name": "Save Draft - Sponsorship",
      "credentials": {
        "gmailOAuth2": {
          "id": "__REDACTED__",
          "name": "Gmail account"
        }
      }
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [[{ "node": "Text Classifier", "type": "main", "index": 0 }]]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [[{ "node": "Text Classifier", "type": "ai_languageModel", "index": 0 }]]
    },
    "Text Classifier": {
      "main": [
        [{ "node": "Sponsorship Reply", "type": "main", "index": 0 }],
        [{ "node": "High Priority", "type": "main", "index": 0 }]
      ]
    },
    "High Priority": {
      "main": [[{ "node": "Save Draft - High Priority", "type": "main", "index": 0 }]]
    },
    "Sponsorship Reply": {
      "main": [[{ "node": "Save Draft - Sponsorship", "type": "main", "index": 0 }]]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "v1.0.0",
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "id": "email-draft-ai-agent",
  "tags": []
}

Reply

or to participate.