AI Email Sorter and Automatic Gmail Labeler - n8n Workflow

Deploy this efficient n8n workflow to automatically classify new Gmail emails using AI (OpenRouter/Gemini) and apply the correct organizational labels instantly. Perfect for reducing inbox clutter.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Individuals struggling with inbox clutter and seeking automated organization.
Professionals who need standardized categorization of communication (e.g., "FYI," "To respond").
Developers looking for advanced n8n templates integrating LangChain LLM capabilities with core services.
Users seeking to leverage an external AI model via an n8n node for production tasks.

Overview

Managing an overflowing inbox is a constant challenge. This powerful n8n workflow solves this by introducing intelligent, automated email triage. Instead of manually reading and labeling dozens of emails daily, this automation instantly analyzes the content (snippet, subject, sender) using a detailed system prompt via the OpenRouter service (Gemini model). The result is a perfectly labeled inbox, ensuring high priority items are immediately categorized as 'To respond' or 'FYI,' drastically reducing cognitive load. This robust n8n workflow serves as an excellent foundation for more complex communication automations, showcasing the flexibility of the n8n platform and its diverse n8n node library.

How it Works

The process begins with the New Gmail Email Received n8n trigger, which initiates the n8n workflow whenever a new email lands.


  1. Data Collection (Parallel Processing): The incoming email data is collected. Simultaneously, a separate branch uses the Gmail n8n node to fetch a list of all currently available Gmail labels.

  2. Mapping IDs: A specialized Code n8n node processes the label list, creating a crucial map that links the human-readable label names (e.g., 'FYI') to their technical Gmail Label IDs.

  3. Data Merge: The original email item and the newly created Label ID map are combined using a Merge n8n node, ensuring all necessary data is available for the next step.

  4. AI Classification: The core logic runs in the Classify Email with AI n8n node. This LangChain-based n8n node connects to the OpenRouter Chat Model (using Gemini-2.5-Flash) and utilizes a detailed prompt instructing the AI to classify the email into one of eight categories and respond only with the corresponding label name and number (e.g., '4. Notification').

  5. Label Application: Finally, the Apply Classification Label Gmail n8n node executes the action. It uses the label name outputted by the AI to look up the correct Label ID in the map and applies that ID to the original message ID retrieved by the n8n trigger. This completes the fully automated n8n workflow.

Installation Guide

To deploy this useful n8n workflow template, follow these steps:


  1. Import: Copy the provided JSON code and import it directly into your n8n instance using the 'New' button and 'Import from JSON'.

  2. Gmail Credentials: You must configure two Gmail OAuth2 credentials, one for the 'New Gmail Email Received' n8n trigger and one for the subsequent Gmail n8n nodes ('Fetch Available Gmail Labels' and 'Apply Classification Label'). Ensure these credentials have full read/write access to Gmail labels and messages.

  3. OpenRouter Credentials: Configure the 'OpenRouter account' credential required for the 'OpenRouter Chat Model' n8n node. This service handles the AI requests using the specified Gemini model.

  4. Prerequisites: Ensure the required labels (e.g., 'To respond', 'FYI', 'Notification') exist in your Gmail account, as the n8n workflow relies on fetching and mapping these names to their IDs.

  5. Activation: Save the n8n workflow and activate the 'New Gmail Email Received' n8n trigger to start monitoring your inbox automatically.

Node Details

New Gmail Email Received (n8n trigger): The entry point for the n8n workflow. It monitors the linked Gmail account for new incoming emails, retrieving key metadata like the snippet, subject, and message ID.
Fetch Available Gmail Labels (Gmail n8n node): Retrieves a list of all custom and default labels in the user's Gmail account. This is essential for the final step to know the correct ID for the AI-generated label name.
Map Label Names to IDs (Code n8n node): A custom JavaScript n8n node that transforms the label array output from the previous step into an easily accessible JSON object where the key is the label name and the value is the Label ID. This enables quick lookup later in the n8n workflow.
OpenRouter Chat Model (LangChain n8n node): Specifies the AI provider and model (Gemini 2.5 Flash via OpenRouter) used for classification.
Classify Email with AI (LangChain n8n node): Executes the classification logic. It feeds the email snippet and metadata into the LLM alongside a detailed, multi-rule system prompt, ensuring the output is a standardized label name.
Apply Classification Label (Gmail n8n node): The final action. It uses expression logic to dynamically retrieve the correct Label ID based on the AI's classification output ({{ $('Map Label Names to IDs').item.json.combinedObject[$json.text] }}) and adds that label to the original email using the addLabels operation.

Related n8n Workflows

Free

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

Kiwi Developer & Founder. Working on AI Startups while in London. Passionate about AI Tech, Learning, and Helping Engineers.

Featured*