Basic Automatic Gmail Email Labelling with OpenAI and Gmail API n8n Workflow Template

n8n workflow template · 9 nodes · Gmail

Basic Automatic Gmail Email Labelling with OpenAI and Gmail API is a free n8n workflow template that starts from the Gmail Trigger node and works with Gmail and OpenAI. It has 9 nodes, 3 of them AI nodes (OpenAI Chat Model, AI Agent and Window Buffer Memory). The notes inside the workflow say: “Poll Gmail every x minutes, trigger when a new email is received.” To run it, you add your own credentials for Gmail OAuth2 and OpenAI API.

At a glance

Trigger
Gmail Trigger (Gmail Trigger)
Nodes
9
Apps
Gmail and OpenAI
AI nodes
3
Credentials
2 types
Category
Email automation

Apps and integrations

  • Gmail
  • OpenAI

Node types used

  • Gmail Trigger
  • OpenAI Chat Model
  • Gmail Tool
  • AI Agent
  • Window Buffer Memory
  • Wait

How it works

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Gmail trigger

Poll Gmail every x minutes, trigger when a new email is received.

  • Gmail API

Setup notes

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Gmail API

  • Add credentials

Gmail labelling agent

  • Read the message
  • Read existing labels
  • Create a new label if needed
  • Assign label to message

Objective: Automatically categorize incoming emails based on existing Gmail labels or create a new label if none match.

  • Get message
  • Read all labels
  • Create label
  • Assign label to message

Analyze the email's subject, sender, recipient, keywords, and content. Compare with existing Gmail labels to find the most relevant match. Label Assignment

Assign the email to the most appropriate existing label. Remove the inbox label if the email is of less importance (like ads, promotions, aka "Reclame"), keep normal and important emails in the inbox. If no suitable label exists, create a new label based on the existing labels. Try reusing existing labels as much as possible. Always create a label as a sublabel, if no label applies, if the main label already exists, create the new label under the existing label, if no main label exists, create the label AI and create the new label under this label. Label Creation

Ensure new labels align with the structure of existing ones, including capitalization, delimiters, and prefixes. Examples

If the email subject is "Project Alpha Update," assign to [Project Alpha] if it exists. For "New Vendor Inquiry," create "Vendor Inquiry" if no relevant label exists. Outcome: Emails are consistently categorized under the appropriate or newly created labels, maintaining Gmail's organizational structure.

Nodes in this workflow

The 9 nodes in file order (sticky notes excluded). Branches and connections determine execution order in n8n:

  1. Gmail Trigger (Gmail Trigger)
  2. OpenAI Chat Model1 (OpenAI Chat Model)
  3. Gmail - read labels (Gmail Tool)
  4. Gmail - get message (Gmail Tool)
  5. Gmail - add label to message (Gmail Tool)
  6. Gmail - create label (Gmail Tool)
  7. Gmail labelling agent (AI Agent)Objective: Automatically categorize incoming emails based on existing Gmail labels or create a new label if none match. Tools: - Get message - Read all labels - Create label - Assign label to message Instructions: Label Matching: Analyze the email's subject, sender, recipient, keywords, and content. Compare with existing Gmail labels to find the most relevant match. Label Assignment: Assign the email to the most appropriate existing label. Remove the inbox label if the email is of less importance (like ads, promotions, aka "Reclame"), keep normal and important emails in the inbox. If no suitable label exists, create a new label based on the existing labels. Try reusing existing labels as much as possible. Always create a label as a sublabel, if no label applies, if the main label already exists, create the new label under the existing label, if no main label exists, create the label AI and create the new label under this label. Label Creation: Ensure new labels align with the structure of existing ones, including capitalization, delimiters, and prefixes. Examples: If the email subject is "Project Alpha Update," assign to [Project Alpha] if it exists. For "New Vendor Inquiry," create "Vendor Inquiry" if no relevant label exists. Outcome: Emails are consistently categorized under the appropriate or newly created labels, maintaining Gmail's organizational structure.
  8. Window Buffer Memory (Window Buffer Memory)
  9. Wait (Wait)

Credentials you need

  • Gmail OAuth2 – used by Gmail - add label to message, Gmail - create label, Gmail - get message and Gmail - read labels and others
  • OpenAI API – used by OpenAI Chat Model1

Configure your own credentials after importing. Create each credential in n8n, then select it in the node.

How to import into n8n

  1. Download the JSON file with the button above.
  2. In n8n, open a new workflow, select the ... menu and choose Import from File. You can also paste the JSON straight into the editor canvas.
  3. Open each node that shows a warning and select or create its credential.
  4. Read the notes on the canvas, test the workflow with sample data, then activate it.

Do you need somewhere to run it? Read our guide to self-hosted n8n.

Need this customised?

We can adapt this workflow to your apps, data and process, then deploy and support it.

Contact Us