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
- An n8n instance (n8n Cloud or self-hosted).
- A Gmail credential in n8n. On n8n Cloud you can sign in with Google directly. On self-hosted n8n you'll need your own Google OAuth client; see our guide on n8n Gmail OAuth setup for self-hosted n8n.
- An OpenAI API key, added in n8n as an OpenAI credential. Classification uses very little text per email, so costs are usually small, though they depend on your volume and model.
Step 1: Decide your categories
Keep the list short and distinct. Overlapping categories confuse both the model and you. A practical starting set:
- Urgent: time-sensitive requests or problems.
- Sales Lead: someone interested in buying or asking for a quote.
- Support: an existing customer who needs help.
- Billing: invoices, receipts, payment issues.
- Newsletter/Spam: bulk mail, promotions, cold pitches.
- Other: everything else.
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:
- Simplify: when on, you get a compact version of each message. Turn it off if you want the full body for better classification.
- Filters > Search: accepts normal Gmail search syntax. Adding
-category:promotions -category:socialkeeps obvious bulk mail away from the AI step and saves calls. - Filters > Label Names or IDs: limit the trigger to
INBOXif you only want fresh incoming mail.
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:
- 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.
- 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:
- Trim the body. The first few thousand characters are almost always enough. Long quoted threads add cost without adding much signal.
- Include the sender and subject. They often carry as much meaning as the body.
- Give one-line definitions. "Sales Lead: a new person or company asking about buying, pricing or a quote" beats a bare label.
- Pick a small model first. Sorting is a simple task. Move up only if results disappoint.
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:
- Resource: Message
- Operation: Add Label
- Message ID: the
idfrom the trigger output - Label Names or IDs: the label for this branch
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
- Every email lands in Other. The model output probably doesn't match your Switch values exactly. Check for extra spaces, punctuation or capitalization, and trim the output before routing.
- Labels not found. Label IDs differ between accounts. If you imported a workflow from someone else, reselect each label in the Gmail nodes.
- The same email processed twice. Avoid running two copies of the workflow, and keep the trigger filter consistent. Adding a "processed" label and excluding it in the search filter (
-label:ai-processed) is a simple safeguard. - Costs higher than expected. Filter promotions in the trigger, shorten the body you send, and confirm you're using a small model.
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:
- Inbox Triage Lite (free): four labels (Urgent, To Reply, Newsletter/Spam, Other). Available on Gumroad and in the n8n gallery.
- AI Inbox Triage ($29): six categories, creates only the labels your inbox needs, writes reply drafts for urgent, sales and support emails, and offers an optional 6 PM daily summary. Get it on Gumroad.
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.
