Personal / Self-Hosted · Mail Automation
Local Mail Categorizer
A self-hosted system that reads every mail account over IMAP, sorts mail into categories with the local Laya model, files it into folders in the real mailboxes and clears old mail on per-category timers. No mail content ever leaves the server.
Local Mail Categorizer sorts and cleans up mail from several accounts without handing any of it to a hosted service. It runs on a self-hosted server, reads every account over IMAP, decides a category for each mail, files it into folders in the real mailbox and deletes old mail on configurable timers. Classification happens on the server’s CPU, and the dashboard is reachable only over a private Tailscale network.
A tool that moves and deletes mail in real mailboxes has to be hard to get wrong, so most of the work went into making every write provable before it is allowed to happen.
One daily run
A scheduler starts a run once a day, at 04:00 by default, or on demand from the dashboard’s “Sync now” button:
- Refresh the phishing blocklists and ClamAV signatures.
- Fetch new mail from every account: inbox, sent and the provider’s spam folder. Reads never change read state.
- Store the body, headers and attachments, scan the attachments and check every link and header offline.
- Classify with rules first, then thread inheritance, then Laya.
- Move inbox mail past its grace period into
Sorted/<Category>. - Apply retention: move expired mail to Trash, within a daily cap.
- Verify every action, log it and send a Discord alert if anything failed.
Each run is a child process, so the model’s memory goes back to the operating system when it exits.
Classification with Laya
Mail that no rule catches goes to Laya, a 421M-parameter decision model under Apache 2.0. It returns a category and a probability in a single forward pass instead of generating text, which is what makes it practical on a laptop CPU. The multilingual checkpoint is used, so mail that mixes languages, such as Turkish and English, is handled by one model.
- Rules go first. Known senders, bank statement subjects and
List-Unsubscribeheaders are decided without the model, because they are free and always right. - Category descriptions are the prompt. Laya reads each category’s description as the definition of that option, so “Turkcell, Enerjisa, fatura, invoice” teaches it more than the bare word “Finance”.
- Low confidence means Undecided. Below the threshold a mail goes to a review queue instead of getting a weak guess, and replies inherit their thread’s category so a conversation never splits across folders.
- Measured before trusted. Before any mail moves, about 200 hand-labelled mails go through the real pipeline, and the report shows accuracy on decided mail, coverage, per-category precision and recall, and speed and memory on this CPU.
- Corrections become data. Every correction in the dashboard, and every mail dragged into another
Sorted/folder in a mail app, is saved and can be exported for Laya’s calibration or fine-tuning. - Swappable. The classifier sits behind one interface. Laya can run on PyTorch or ONNX Runtime, and a small Ollama model or a rules-only mode can replace it with one setting.
Safe on real mailboxes
- Hard read-only switch. With it on, the IMAP client refuses every command that changes a mailbox, and runs only log what they would have done. The dashboard cannot override it.
- Read state is never touched. Reads use
EXAMINEandBODY.PEEK, moves useUID MOVEto keep flags and dates, and every move checks afterwards that\Seenand\Flaggeddid not change. - Nothing is deleted permanently. Deleting means moving to the provider’s Trash. On Gmail, where deleting from a label only removes the label, each delete is also checked against All Mail.
- Every action is verified by Message-ID before and after. If a mail is not where the database expects it, it is left alone and logged.
- Dry-run before live. Write-back and deletion start off per account, and the dashboard will not switch either one to live until it has finished at least one run in dry-run.
- Guard rails on retention. Starred, Undecided and protected mail is never deleted, a run over the daily cap deletes nothing and alerts, and
mailcat undo <run>reverses a whole run. - Provable per provider.
provider-testruns the full acceptance checklist in a sandbox folder of a real account and fingerprints every other folder to prove the existing mail is untouched.
Security without network calls
Full URLs and mail content are never sent to an outside service, and the system never visits a link. Attachments get random names on a noexec volume and are scanned by ClamAV. Links are checked against local copies of the URLhaus and OpenPhish feeds and for lookalike domains, punycode and text that hides the real destination. Headers are parsed for SPF, DKIM and DMARC results, and HTML is sanitized and shown in a sandboxed iframe with remote images blocked.
Built for a small server
It shares an 8 GB laptop with other projects. The API idles at about 80 MB and the worker at about 60 MB. Containers run with a read-only root filesystem, dropped capabilities and an internal network, the data volume is LUKS-encrypted, and nightly backups are GPG-encrypted to a second disk. The test suite runs against real PostgreSQL and a GreenMail IMAP server, including the end-to-end acceptance checklist.