Auto Labeling and Organization Using AI for Gmail - n8n Workflow

Use this powerful n8n workflow to automatically organize your Gmail inbox. It leverages the OpenAI n8n node for intelligent content classification, ensuring every new email gets the correct custom label via a robust n8n trigger.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Professionals managing high email volume who need consistent organization.
Users looking for advanced categorization beyond standard Gmail filters.
n8n developers seeking practical examples of AI-powered email management.
Anyone wanting to deploy a robust, scheduled n8n trigger for inbox cleanup.

Overview

This advanced n8n template solves the challenge of manual email organization by leveraging sophisticated artificial intelligence capabilities. Every time the scheduled n8n trigger fires, the system checks for new, unlabeled emails. It sends the email content (subject and body) to the OpenAI service, which acts as a 'Personal Email Labeler' following strict, predefined guidelines.

This process allows for hyper-accurate, consistent labeling (e.g., 'Invoice', 'Urgent', 'Newsletter') far exceeding typical keyword filters. The efficiency of this n8n workflow ensures your inbox remains categorized and actionable, minimizing clutter and maximizing productivity. This custom n8n workflow structure demonstrates complex conditional logic combined with external AI processing.

How it Works

The entire process is initiated by a time-based n8n trigger, which runs on a schedule (e.g., every two minutes).


  1. Fetch Messages: The initial Gmail n8n node retrieves the most recent messages from the inbox.

  2. Filter Existing Labels: A Code n8n node filters this list, excluding any email that already possesses one of the 17 predefined AI labels. This prevents the n8n workflow from reprocessing already categorized items.

  3. Process Individual Emails: The workflow enters a loop to handle each filtered message sequentially.

  4. Retrieve Full Content: A second Gmail n8n node fetches the full body and metadata of the individual message.

  5. Extract Data: A Set n8n node aggregates the essential data points (ID, sender, subject, text body) for the AI prompt.

  6. AI Classification: The 'Categorize Email with AI' n8n node utilizes the OpenAI Chat Model. It feeds the raw email data into a highly specific system prompt that mandates the AI output only one of the 17 designated category labels (e.g., 'Inquiry', 'Action Required').

  7. Store Category: The resulting AI classification is stored temporarily.

  8. Label Existence Check: The n8n workflow retrieves a list of all existing Gmail labels and compares the AI-generated category name against them using the Compare Datasets n8n node.

  9. Apply or Create Label: The workflow branches:

If the label exists, a Gmail n8n node applies the existing Gmail ID to the email.
If the label does not exist, the n8n workflow first creates the new label in Gmail using an appropriate n8n node, then applies that newly created label to the message.

Installation Guide

To set up this intelligent n8n workflow, follow these steps:


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

  2. Credentials Setup:

Gmail: You will need OAuth2 credentials configured for the Gmail n8n node to allow n8n to read messages and manage labels. Ensure the credentials have sufficient scope (including https://mail.google.com/).
OpenAI: Set up an API Key credential for the OpenAI n8n node.

  1. Review the Code Node: Check the Filter Emails Without Excluded Labels Code n8n node. The hardcoded label IDs (Label5, Label21, etc.) may need to be updated to reflect the actual IDs of any system or common labels you wish to always ignore. For a fresh start, you can remove all IDs.

  2. Customize AI Prompt: Review the detailed system message in the 'Categorize Email with AI' n8n node to adjust the available labels or their definitions to better fit your organizational needs.

  3. Activate: Save the n8n workflow and activate the n8n trigger to begin scheduled execution.

Node Details

Schedule Trigger: This n8n trigger is set to run every 2 minutes, initiating the organization process frequently.
Gmail - Get All Messages: Fetches the initial batch of emails (limit 20) for processing.
Filter Emails Without Excluded Labels (Code n8n node): Essential filtering logic that uses custom JavaScript to prevent the n8n workflow from attempting to categorize emails that already have one of the system or AI-defined labels.
Gmail - Single Message: Retrieves the full, detailed content (including the body text) necessary for the AI analysis.
Categorize Email with AI (Agent n8n node): The core intelligence step. It utilizes the Langchain Agent framework and is configured to follow highly strict instructions via the system message to output a precise, single category label. This n8n node requires the OpenAI credentials.
List All Gmail Labels: Retrieves all existing labels in your account, which is necessary for the next step of the n8n workflow.
Check if Label Exists (Compare Datasets n8n node): Compares the AI's classification output against the current list of Gmail labels to determine if a new label needs to be created or if an existing one can be applied.
Apply Label to Email / Apply New Label / Create New Label (Gmail n8n nodes): These nodes handle the final action, applying the correct label ID to the original email based on the AI's determination.

Related n8n Workflows

Free

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

Cybersecurity leader turning complex workflows into seamless, AI-driven automations.

Featured*