Auto-label incoming Gmail messages with AI nodes n8n Workflow Template

n8n workflow template · 11 nodes · Gmail

Auto-label incoming Gmail messages with AI nodes is a free n8n workflow template that starts from the Gmail Trigger node and works with Gmail and OpenAI. It has 11 nodes, 3 of them AI nodes (Basic LLM Chain, Structured Output Parser and OpenAI Chat Model). The notes inside the workflow say: “With this workflow you can automatically set labels for your Gmail message according to its content.” To run it, you add your own credentials for Gmail OAuth2 and OpenAI API.

At a glance

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

Apps and integrations

  • Gmail
  • OpenAI

Node types used

  • Gmail Trigger
  • Gmail
  • Edit Fields (Set)
  • Split Out
  • Merge
  • Aggregate
  • Basic LLM Chain
  • Structured Output Parser
  • OpenAI Chat Model

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.

Add AI labels to Gmail messages

With this workflow you can automatically set labels for your Gmail message according to its content.

In this workflow available are 3 labels: "Partnership", "Inquiry" and "Notification". Feel free to adjust labels according to your needs.

Please remember to set label names both in your Gmail account and workflow.

Gmail Trigger

Receive data from Gmail about new incoming message.

Set polling interval according to your needs.

Get message content

Based on Gmail message ID retrieve body content of the email and pass it to AI chain.

Assign labels

Let the AI decide which labels suit the best content of the message.

Remember to edit system prompt - modify label names and instructions according to your needs.

JSON schema

Edit JSON schema and label names according to your needs.

Label names in system prompt and JSON schema should be the same.

Merge labels

Combine labels retrieved from Gmail account and assigned by AI together.

Aggregarte labels and add to message

Create array of label IDs and add to the desired email message in Gmail.

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.

Note

  1. Complete video guide for this workflow is available on my YouTube.
  2. Remember to add your credentials and configure nodes (covered in the video guide).
  3. If you like this workflow, please subscribe to my YouTube channel and/or my newsletter.

Thank you for your support!

Nodes in this workflow

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

  1. Gmail trigger (Gmail Trigger)
  2. Get message content (Gmail)
  3. Set label values (Edit Fields (Set))
  4. Get all labels (Gmail)
  5. Split out assigned labels (Split Out)
  6. Merge corresponding labels (Merge)
  7. Aggregate label IDs (Aggregate)
  8. Add labels to message (Gmail)
  9. Assign labels for message (Basic LLM Chain)
  10. JSON Parser (Structured Output Parser)
  11. OpenAI Chat (OpenAI Chat Model)

Credentials you need

  • Gmail OAuth2 – used by Add labels to message, Get all labels, Get message content and Gmail trigger
  • OpenAI API – used by OpenAI Chat

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