ReadyUpAgentsReadyUpAgents

How to Auto-Label Gmail with AI in n8n

Gmail filters are great when you can predict the sender or a keyword. They struggle with the messy middle: a customer asking about an invoice from a new address, a sales inquiry with a vague subject, an urgent note buried in a long thread. An AI step can read the email the way you would and pick a category, and n8n can then apply the matching Gmail label.

This guide walks through building that workflow yourself: a trigger for new mail, an OpenAI classification step, and a Gmail node that applies the label.

What you need

Step 1: Decide your categories

Keep the list short and distinct. Overlapping categories confuse both the model and you. A practical starting set:

Create matching labels in Gmail. Using a parent label keeps the sidebar tidy: a label named AI/Urgent appears nested under AI. You can create labels in the Gmail web app or with the n8n Gmail node (Label resource, Create operation).

Step 2: Add the Gmail Trigger

Add a Gmail Trigger node and select your credential. It polls Gmail on a schedule you choose, such as every minute or every five minutes.

Useful settings:

Run the node once with a test email in your inbox, then pin the output. Pinned data lets you build and test the rest of the workflow on the same sample without waiting for new mail.

Step 3: Classify the email with OpenAI

You have two good options in n8n:

  1. Text Classifier node (under the AI nodes). You list categories with a short description for each, connect an OpenAI chat model, and the node routes each item to a matching output. This is the most visual approach.
  2. OpenAI node with a prompt that returns a single category name, followed by a Switch node.

If you write your own prompt, make the output strict. For example:

You sort business email. Read the email below and reply with exactly one
of these categories and nothing else:
Urgent, Sales Lead, Support, Billing, Newsletter/Spam, Other

From: {{ $json.from }}
Subject: {{ $json.subject }}
Body: {{ $json.text.slice(0, 3000) }}

A few tips that help accuracy and cost:

Step 4: Map the category to a label

Gmail's API applies labels by ID, not name. In the n8n Gmail node you can choose labels from a dropdown, which handles this for you. If you're mapping dynamically, use a Switch node with one output per category, then one Gmail node per output, each set to its label.

Alternatively, fetch your labels once with the Gmail node (Label resource, Get Many operation) and keep a small lookup in a Code or Set node that turns a category name into a label ID.

Step 5: Apply the label

Add a Gmail node with:

Optionally add a second operation to Remove Label INBOX for Newsletter/Spam, which archives bulk mail while keeping it searchable.

Step 6: Test, then activate

Send yourself a few varied emails: a fake invoice, a pricing question, a newsletter. Check that each lands in the right label. When you're happy, activate the workflow so the trigger runs on its schedule.

Common problems and fixes

Going further: reply drafts

Once mail is sorted, the next step is preparing replies. For Urgent, Sales Lead and Support emails, you can add an OpenAI step that writes a response and a Gmail node (Draft resource, Create operation) that saves it inside the thread. The draft waits in Gmail until you review and send it yourself.

Skip the build

If you'd rather start from a working workflow, we offer two templates:

Both run on your own n8n with your own OpenAI key, and paid templates include a 14-day refund window.

n8n is a trademark of n8n GmbH. ReadyUpAgents is independent and not affiliated with or endorsed by n8n.