My public contact inbox had become a mess. Every day there were SEO pitches, “your password has expired” phishing, fake invoices and newsletters I never signed up for. The few real emails were buried in all of that, and I’d find them days late.
I didn’t want to send that mail through a cloud AI service, and I didn’t want to pay for one either. So I built my own assistant on the homelab. Now it:
- sorts every new email using a local LLM, moving spam and phishing out of the way
- pings me on Telegram when a real person writes in
- writes a reply for me when I reply to that Telegram alert with what I want to say: threaded, in my own writing style, with my signature, saved in my drafts folder
- revises the draft, adds a CC or attaches a file when I reply to it again
- never sends anything on its own. I open the draft in my mail client and hit send myself.
Everything runs locally: n8n for the workflow, Ollama with a Qwen3 8B model on an old GTX 1080 Ti, and IMAP to a normal cPanel mailbox. Here’s how it fits together, and the gotchas that cost me most of a night.
Telegram alert and draft
The stack
| Piece | What it does |
|---|---|
| cPanel mail (IMAP) | The mailboxes. Nothing special: any IMAP server works. |
| SpamAssassin (built into cPanel) | First pass. Blatant spam never reaches the LLM. |
| n8n (self-hosted, Docker) | The workflow engine that ties everything together. |
| n8n-nodes-imap (community node) | Move, flag, delete and create drafts over IMAP. The built-in node can’t do these. |
| Ollama + qwen3-vl:8b-instruct | Local LLM on a GTX 1080 Ti (11 GB). Classifies mail and writes drafts. |
| Telegram bot | My “chat window” to the assistant. Made with @BotFather in two minutes. |
| Betterbird | My desktop mail client. Drafts sync over IMAP and I send from there. |
Part 1: Sorting the inbox
Let SpamAssassin take the first swing
cPanel already ships with SpamAssassin. Under Email → Spam Filters I turned on Process New Emails and Mark them as Spam, and Move New Spam to a Separate Folder with a threshold of 8.
That threshold matters. I first set it to 4, but short, legitimate emails (especially from website contact forms) can score 4 or 5. Anything that goes into the spam folder never reaches the workflow, so a real enquiry would just disappear quietly. At 8, only the obvious junk goes, and the LLM handles everything in between.
Why I didn’t use the built-in IMAP trigger
n8n has an Email Trigger (IMAP) node, and it’s quick because it uses IMAP IDLE. The problem is that it can only read mail: afterwards it can mark a message as read or do nothing. It can’t move a message to another folder.
The community node n8n-nodes-imap can move, flag, delete and create drafts. Install it under Settings → Community Nodes.
Flag what you’ve processed, without marking it read
Each run is a schedule trigger every 5 minutes that searches the inbox for mail without a custom IMAP keyword:
- Flags → Custom Flag (Not Set):
$Triaged - Date range: last 7 days
- Limit: 10 per run
After classifying an email, the workflow sets $Triaged on it. Emails stay unread in my client, but the workflow never sees the same one twice, and if n8n is down for an hour it simply catches up afterwards. (Dovecot, which cPanel uses, supports custom keywords out of the box.)
Let the model label, and let code act
This is the most important design decision in the whole build: the model never decides what happens to an email. It only returns a label. Plain JavaScript decides what to do.
Ollama can force JSON output against a schema, which makes this reliable even with an 8B model:
js
format: {
type: 'object',
properties: {
category: { type: 'string', enum: ['enquiry', 'support', 'notification', 'spam', 'other'] },
confidence: { type: 'number' },
summary: { type: 'string' }
},
required: ['category', 'confidence', 'summary']
},
options: { temperature: 0, num_ctx: 4096 }
Then a Code node maps that to an action:
- enquiry / support → stays in the inbox, Telegram alert
- spam with confidence ≥ 0.8 →
Triage/Junk - notification (invoices, service alerts) → stays put, no alert
- anything else, or the model fails →
Triage/Review, quiet alert
Nothing is ever deleted. The worst a mistake can do is move an email to a folder.
I also strip the HTML and cut the body to about 2,500 characters before sending it to the model. The body is wrapped in clear <<<START>>> / <<<END>>> markers, and the prompt says the email is untrusted data. Spam will try “ignore previous instructions”, and since the model can only return a label, there’s nothing for that to hijack.
Catching spoofed “from yourself” phishing
The first real test failed in an interesting way. A “Your password has expired” phish claimed to come from IT@mydomain. I’d put my own domain on a “never junk” safe list, so it went to Review instead of Junk.
The fix: a sender only counts as safe if SPF or DKIM actually passed. I pull Authentication-Results, Received-SPF and X-Spam-Status from the headers and check for a pass. Anything claiming to be from my own domain without a pass is marked as spoofed. The model is told so, it goes to Junk, and it can never trigger a “new enquiry” alert.
Part 2: The Telegram assistant
Alerts are useful, but the part I actually use is replying to them.
Why Telegram
I looked at Signal first because it’s end-to-end encrypted. But Signal has no bot API, so you’d need a spare phone number and signal-cli. Telegram has proper bots (@BotFather, no phone number needed) and n8n has a built-in Telegram trigger. It’s less private, so it was a trade-off, and I took it. The workflow is built so I can switch to Signal later without redoing the logic.
How the conversation works
One Telegram Trigger, a Code node that works out what I meant, and a Switch:
| I do this in Telegram | It does this |
|---|---|
| Reply to an alert: “Tuesday 10am works for me” | Fetches the original email by UID, writes a reply, saves a threaded draft |
| Send “email [email protected] about the hours report, cc [email protected]“ | Writes a new email from my notes |
| Reply to a ✏️ draft: “shorter, mention the call” | Rewrites the draft |
| Reply to a ✏️ draft with a file | Attaches it to the draft |
| Anything else | Sends the help text |
Telegram tells you which message you replied to, so the bot can track context without a database. Each alert carries #<UID> and each draft preview carries [draft <UID>]. The bot reads those back to know which email or draft you mean.
The first line of the Parse node is the security check: it drops any message that isn’t from my chat ID. The bot’s webhook has to be public for Telegram to reach it, so anyone could find the bot, but only I get answers.
Drafts, not sending
Drafts are built as raw RFC 822 messages in a Code node and saved with the community node’s Create Draft operation, using its RFC822 input. Building the raw message myself means I can set the headers that matter:
From: Me <me@mydomain>
To: Jane <[email protected]>
Cc: [email protected]
Subject: Re: Quote for office Wi-Fi
In-Reply-To: <original-message-id>
References: <earlier-ids> <original-message-id>
In-Reply-To and References keep the reply in the sender’s thread, even though I reply from my personal address and they wrote to the shared one.
I then open the draft in Betterbird, check it, and click send.
“Editing” a draft over IMAP
I didn’t want 20 versions of every email piling up in my drafts folder. But IMAP can’t edit a message in place. Every mail client “edits” a draft by saving a new one and deleting the old one.
So the workflow does the same:
- Save the new draft. The append returns the new UID.
- Only if that worked, delete the old draft using the UID from the
[draft N]tag. - Send me the new preview with the new tag.
If saving fails, the old draft is left alone.
Attachments
When I reply to a draft with a file, the workflow:
- downloads the old draft’s full source (Download as EML)
- downloads the file from Telegram (bots can fetch up to 20 MB)
- wraps the draft in
multipart/mixedwith the file as a new part - saves it, then deletes the old one
Revising the text later keeps the attachments: before replacing the draft, it pulls any Content-Disposition: attachment parts from the old one and adds them to the new one.
The CC safety rule
The model returns the recipient and any CC addresses as fields, but code only accepts addresses I actually typed (or that were already on the draft). If the model invents an address, it’s dropped and the preview shows a warning. An 8B model that adds a stranger to an email is not a mistake I want to find after I’ve hit send.
Part 3: Making it sound like me
The first drafts were… AI. “We’ve attached the report”, although I’m one person. Filler like “we can do it quick if you’re busy”. It drifted from what I actually asked.
What fixed it, all for free:
- A style guide in the prompt, built from a few of my real emails: always “I”, open with “Hi Name,”, short for simple things, no filler, say exactly what I asked.
- Real examples (with names and figures changed), which small models copy very well.
- Temperature 0.3 instead of 0.4.
Two lessons about small models:
- They follow examples over rules. I told it to sign off with just “Kind regards,” but every example ended with “Kind regards, Trevor”, so it kept adding my name. Fixing the examples fixed it straight away.
- They take lists literally. I listed “so long” (South African for “in the meantime”) as a phrase I use, and it appeared on a line of its own as a sign-off.
It also greeted someone as “Hi Admin,” because the address was admin@. One extra rule fixed that: generic mailboxes get “Hi there,”.
Part 4: The signature
Betterbird only inserts your signature when you start a new email, not when you open an existing draft. So the workflow adds it.
My signature is HTML with a logo and icons, and it was using data: image URIs. Gmail and Outlook block those, so recipients would see empty boxes. The proper way is what mail clients do themselves: send the images as inline MIME parts and point to them with cid::
multipart/mixed (only if there are attachments)
├── multipart/related
│ ├── multipart/alternative
│ │ ├── text/plain (body + plain-text signature)
│ │ └── text/html (body + HTML signature)
│ ├── image/png Content-ID: <sig-logo>
│ └── image/png Content-ID: <sig-phone> ...
└── application/pdf (your attachment)
html
<img src="cid:sig-logo" width="110" height="110" alt="Logo">
While I was at it, I shrank the logo from 100 KB to 11 KB (resized to 2× display size and saved as an 8-bit PNG). Nobody needs 100 KB of logo in every email.
Gotchas that cost me time
- n8n’s Telegram node sends in Markdown mode by default. My
[draft 14]tag arrived asdraft 14because Markdown ate the brackets. Underscores in file names can also make Telegram reject a message completely. Set Parse Mode → HTML on every Telegram node and escape&,<,>in your text. - Code node modes matter. “Run Once for Each Item” expects one object back. “Run Once for All Items” expects an array, and it’s the only mode where
$input.all()works. - cPanel/Dovecot folder paths look like
INBOX.Triage.Junk, notTriage/Junk. Choose folders from the node’s List mode to see the real path. - The built-in Telegram trigger needs a public webhook. My n8n sits behind Cloudflare Access, so only the webhook path is public. The chat-ID check in the workflow is what actually keeps it private.
- A DNS hiccup on the n8n host made one Telegram send fail while the draft itself saved fine. Turn on Retry On Fail for the Telegram nodes.
Was the 8B model good enough?
For sorting, yes. With forced JSON, confidence thresholds and code making the decisions, it’s been accurate. Strict SPF/DKIM checks cover what it can’t see.
For writing, it’s good with a style guide and examples. Without them it’s noticeably generic. If I want better drafts later, the next step is qwen3:14b for the two writing nodes only. It fits in 11 GB, but it means Ollama swapping models, and everything else on the GPU waits while it’s loaded.
What’s next
- One-tap buttons in Telegram (“Send draft”, “Remind me tomorrow”)
- Looking up who the sender is, so drafts get more context
- A daily summary: “3 enquiries, 12 junked, 1 in Review”
The whole thing runs on hardware I already had, costs nothing per email, and no email ever leaves my network for an AI service. Best of all, I don’t have to dig through junk to find the real emails anymore.

Leave a Reply