Automatic Gmail Email Categorization using AI - n8n Workflow

Use this powerful n8n workflow to automatically categorize and label existing emails in your Gmail inbox using the GPT-4o mini AI model and structured output.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Individuals or teams struggling with cluttered Gmail inboxes.

  • Users who need to bulk process and organize existing, unlabeled emails.

  • Automation specialists seeking an advanced AI-powered n8n workflow example.

  • Anyone looking to implement large language models (LLMs) like GPT-4o mini within an n8n automation process.

Overview

This powerful n8n workflow provides an efficient solution for tackling existing, unlabeled emails in your Gmail inbox in bulk. Instead of manually sifting through messages, this n8n automation template leverages the power of GPT-4o mini to analyze the content (subject and body) of each email. It uses a manual n8n trigger, making it ideal for scheduled or on-demand inbox cleanups. The workflow ensures only unlabeled emails are processed by a dedicated Filter n8n node, maximizing efficiency and minimizing API costs. By utilizing structured output and dedicated routing, the n8n workflow accurately assigns and applies the correct Gmail label to categorize your communications automatically.

How it Works

The process begins with the Manual Trigger n8n node, initiated by clicking 'Execute workflow.'


  1. Fetch Emails: The Gmail 'Get many messages' n8n node fetches a specified batch of existing emails from your inbox.

  2. Filter Unlabeled: The 'Label Checker Filter' n8n node evaluates the fetched emails. It ensures that any email that already contains a label is skipped, passing only those requiring categorization to the AI Agent.

  3. AI Analysis: The email content is routed to the 'Give a Label AI Agent' n8n node. This agent, powered by the GPT-4o mini language model (configured in the 'OpenAI Chat Model' n8n node), analyzes the subject and text against predefined categorization rules (Work, Personal, Finance, etc.).

  4. Structure Output: The 'Structured Output Parser' n8n node forces the AI to return a clean JSON object containing only the determined label (e.g., {"emaillabel": "Shopping"}).

  5. Route by Category: The 'Switch' n8n node reads the emaillabel from the AI output and routes the email data down the corresponding path (e.g., the 'Finance' route).

  6. Apply Label: The final set of dedicated Gmail n8n node actions apply the appropriate, corresponding label (e.g., the 'Finance' label) back to the original message ID in your Gmail inbox. This robust n8n workflow handles categorization from start to finish.

Installation Guide

To deploy this n8n template, follow these steps:


  1. Import the Workflow: Copy the provided JSON and import it into your n8n instance.

  2. Configure Credentials:

- Gmail: Provide OAuth2 credentials for the 'Get many messages' and all subsequent Gmail label application n8n node steps. Ensure the connection has permissions to read messages and modify labels.
- OpenAI: Provide API key credentials for the OpenAI Chat Model n8n node to utilize GPT-4o mini.

  1. Prepare Gmail Labels: Before running the n8n workflow, you must manually create all target labels (Work, Personal, Finance, Shopping, Travel, Newsletters, Others) in your Gmail account. The names must exactly match those defined in the AI Agent's system message prompt.

  2. Customize and Save: Review the AI Agent prompt for categories and the batch size in the Gmail 'Get many messages' n8n node. Save the n8n workflow and execute it manually to start bulk processing.

Node Details

When clicking ‘Execute workflow’ (Manual Trigger): This n8n trigger starts the process on demand, enabling bulk execution for existing email cleanup.
Get many messages (Gmail n8n node):
Function: Fetches a batch of emails for processing.
Key Configuration: Operation is getAll (fetches many messages).
Label Checker Filter (Filter n8n node):
Function: Filters out emails that already have a label attached, ensuring the AI only processes unlabeled messages.
Key Configuration: Checks if labelIds[0] does not contain "Label".
OpenAI Chat Model (n8n node):
Function: Provides the language model backend for the AI Agent.
Key Configuration: Uses the gpt-4o-mini model.
Structured Output Parser (n8n node):
Function: Enforces that the AI output adheres to a strict JSON schema ({"emaillabel": "..."}), crucial for reliable routing.
Give a Label AI Agent (n8n node):
Function: Analyzes the email subject and description using the predefined system prompt to determine the correct category.
Key Configuration: System prompt defines 7 distinct categories that must match the Gmail labels.
Switch (n8n node):
Function: Routes the workflow based on the emaillabel extracted by the AI, creating separate branches for each category.
Gmail Label Nodes (e.g., 'Work', 'Finance'):
Function: Applies the specific category label to the corresponding email message ID.
Key Configuration: Operation is addLabels; uses the message ID retrieved by the initial n8n trigger data.

Related n8n Workflows

Free

Nodes: 8 Nodes
Updated: December 26 2025
View all
Created by

👋 Hi! I'm Arlin - a QA Engineer with 2 years of experience and a background in Dart & Flutter for mobile app development. ⚙️ I'm passionate about automation and love building efficient workflows using n8n. 📬 Reach out for any help with custom workflows

Featured*