Intelligent Gmail Labeling using AI Content Analysis - n8n Workflow

Use this powerful n8n workflow to automatically categorize and label incoming Gmail messages using OpenAI's GPT-4. Improve productivity with smart, AI-driven email sorting.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Business Owners and Executives overwhelmed by email volume.
Sales and Support Teams needing immediate categorization of inquiries (e.g., Quotation vs. Inquiry).
IT Professionals looking for advanced, AI-driven n8n templates for inbox management.
Anyone seeking to leverage an n8n node setup for sophisticated data classification.

Overview

Manually sorting and labeling high volumes of emails is a tedious and error-prone process. This automated solution transforms inbox management by implementing an intelligent n8n workflow that relies on cutting-edge AI.

This specific n8n template automatically listens for new messages in Gmail. Once an email arrives, it is processed by GPT-4, which analyzes the content and assigns categories like 'Quotation,' 'Project progress,' or 'Inquiry.' By replacing manual effort with this powerful n8n automation, users ensure consistent, immediate categorization, drastically improving response times and organizational efficiency. It’s a perfect example of how the n8n node ecosystem can handle complex linguistic tasks.

How it Works

The entire process begins with a dedicated n8n trigger listening for new emails.


  1. Trigger & Fetch: The Gmail trigger n8n node polls for new messages. The subsequent Gmail n8n node fetches the full message content based on the provided message ID.

  2. AI Analysis: The content is passed to the OpenAI GPT-4 n8n node setup. A specialized Langchain n8n node (Assign labels for message) uses a defined system prompt to categorize the email content and return the chosen labels (e.g., Inquiry) in a strict JSON format.

  3. Parsing & Setting: The output is validated and extracted using a JSON Parser n8n node, ensuring the data structure is clean. The Set label values n8n node extracts the array of assigned label names.

  4. Label Matching: The workflow simultaneously retrieves all existing Gmail labels and their unique IDs. It then splits the AI-assigned labels into separate items.

  5. Merging IDs: A Merge n8n node combines the AI-assigned label names with the actual Gmail label IDs, finding the corresponding ID for each name.

  6. Final Action: An Aggregate n8n node collects all required IDs into a single array, which is then used by the final Gmail n8n node (Add labels to message) to update the original email with all the newly assigned, AI-derived labels. This demonstrates a seamless, end-to-end n8n workflow.

Installation Guide

To deploy this comprehensive n8n workflow, follow these setup steps:


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

  2. Gmail Credentials: You must configure a valid Gmail OAuth2 credential. This credential needs access to read emails and modify labels.

  3. OpenAI Credentials: Set up an OpenAI API key credential. The workflow is configured to use GPT-4, so ensure your key has access to the appropriate models.

  4. Gmail Label Preparation: Crucially, ensure the labels used in the AI prompt (e.g., 'Quotation', 'Inquiry', 'Project progress', 'Notification') are already created and configured in your target Gmail account. The n8n template relies on finding these exact names.

  5. Customize AI Prompt: Review the prompt within the Assign labels for message n8n node and the schema in the JSON Parser n8n node. If you change label names or categories in Gmail, you must update them in both these n8n node settings to maintain synchronization.

  6. Activate: Save the n8n workflow and toggle it to 'Active' to start polling for new emails via the n8n trigger.

Node Details

Gmail trigger (n8n trigger):
Function: Starts the n8n workflow whenever a new email is received in the configured Gmail account.
Key Configuration: Simple polling mode, configured to check every minute.
Get message content (Gmail n8n node):
Function: Retrieves the full textual content of the message initiated by the n8n trigger, necessary for AI analysis.
Key Configuration: Uses the message ID from the preceding n8n node: ={{ $json.id }}.
OpenAI GPT-4 Based (OpenAI n8n node):
Function: Provides the language model (GPT-4) used for sophisticated content analysis.
Key Configuration: Model set to gpt-4-turbo-preview with response format set to jsonobject and temperature 0 (deterministic results).
Assign labels for message (Langchain n8n node):
Function: Sends the email text and a detailed instruction prompt to GPT-4 to categorize the content based on predefined labels.
Key Configuration: The system prompt strictly defines the four categories and requires JSON output.
JSON Parser (Langchain n8n node):
Function: Ensures the output from the AI is a valid JSON object matching the defined schema, specifically extracting the list of assigned labels.
Key Configuration: Uses a manual JSON schema specifying the possible labels array values.
Get all labels (Gmail n8n node):
Function: Fetches a complete list of all Gmail labels and their corresponding unique IDs, essential for the final labeling action.
Key Configuration: Resource set to label, operation set to getAll.
Merge corresponding labels (Merge n8n node):
Function: Matches the plain text label names provided by the AI with the corresponding label IDs retrieved from Gmail.
Key Configuration: Merge mode is combine, merging by field matching name and labels.
Add labels to message (Gmail n8n node):
Function: The final action in this n8n workflow, applying the collected label IDs to the original message, completing the automation.
Key Configuration: Uses aggregated label
ids and the original message ID.

Related n8n Workflows

Free

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

Featured*