May 23, 2026
Building an AI-Powered Email Classification System with n8n and OpenRouter
Manual email sorting is a bottleneck for any growing business. In this post, I break down how I built an autonomous workflow using n8n and OpenRouter to intelligently categorize incoming emails and route them to the correct department's Telegram and Gmail, complete with a custom Tailwind CSS testing portal.
Every growing business faces the same communication bottleneck: a flooded main inbox. When sales inquiries, bug reports, and server alerts are all dumped into one place, response times drop and things get missed.
I wanted to solve this by building a smart, automated routing system. The goal was simple: an email comes in, an AI reads it, determines the context, and instantly forwards it to the correct department's Slack, Telegram, or Gmail.
Here is a breakdown of how I built an autonomous email classification workflow using n8n, OpenRouter, and a custom Tailwind CSS frontend.
The Architecture
To keep the system modular and cost-effective, I chose a decoupled architecture:
- The Trigger: A Webhook node in n8n listening for POST requests.
- The Brain: An HTTP Request node pinging OpenRouter's API (using Llama-3-8B) for intelligent text categorization.
- The Router: An n8n Switch node mapping the AI's output to specific department pathways.
- The Interface: A lightweight, single-page HTML/Tailwind form to send test payloads.
Overcoming the "Chatty AI" Problem
The biggest technical hurdle wasn't connecting the nodes; it was prompt engineering. Smaller LLMs are inherently conversational. If you ask them to categorize an email, they don't just output "Sales." They output: "Sure! Based on the context, this email belongs in the Sales category." This chattiness instantly breaks conditional logic like Switch nodes, which expect strict, exact string matches.
To fix this, I had to transition from zero-shot prompting to few-shot prompting with strict semantic boundaries. I configured the system prompt to act as a rigid data API rather than an assistant:
Explicit Definitions: I mapped out exactly what constitutes a "Sales" email vs. an "HR" email.
Examples: I fed the model three specific input-to-output examples directly in the system prompt.
The Trigger Word: I ended the user payload with Category: to force the model's next token to be the answer.
Temperature Control: I dropped the API temperature to 0 to remove all creative variance.
Once I updated the prompt and added a quick .trim() method to the n8n expression to strip whitespace, the AI output became 100% deterministic.
The Routing Logic
With clean data coming from the LLM, the rest of the n8n workflow fell into place.
The payload passes into a Switch node programmed with strict rules. If the AI outputs "Finance," the workflow routes the email body and sender details to the Finance department's Gmail and triggers a Telegram alert. If an email is ambiguous or internal office chatter, the AI tags it as "Other," triggering a fallback route to prevent the workflow from failing.
Building the Testing Portal
Running curl commands in the terminal to test payloads gets tedious. To streamline testing and give the project a polished feel, I built a custom frontend portal.
Using Tailwind CSS via CDN, I designed a clean, responsive web form that captures the sender's email, subject line, and body. I used native JavaScript's Fetch API to POST the form data directly to my n8n webhook.
The trickiest part here was browser security. To allow my local UI to talk to the n8n cloud instance, I had to configure the n8n Webhook node to enable CORS (Cross-Origin Resource Sharing), setting the allowed origins to * during the testing phase.
Final Thoughts
Building this workflow was a great exercise in combining traditional API automation with modern AI capabilities. It highlighted that while LLMs are incredibly powerful, they still require strict engineering and edge-case management to function reliably within rigid software pipelines.
The system is now fully functional, heavily scalable, and ready to be adapted for a production environment.


