Training
The bundled model works out of the box. A model trained on your own mail works better, because it learns what your hamWanted mail: the opposite of spam. looks like: your newsletters, your colleagues' writing, the languages you receive.
Train a model#
Point train at folders of spam and ham:
spamscanner train --spam ~/Maildir/.Junk --ham ~/Maildir/cur --ham ~/Maildir/.Archive --out my-model.jsonSources can be:
- mbox files, also gzip-compressed (
.mbox.gz; other.gzfiles are skipped), - a Maildir (its
curandnewfolders are read,tmpis skipped), - a folder of
.emlfiles, read recursively, - a dataset: a CSV or JSON Lines file with a text column and a label column (
--dataset). Columns, or JSON fields, namedtext,message,body,email text,email,content,smsorv2, andlabel,category,is_spam,spam,email type,class,type,label_text,v1orlabels, in any case, are found on their own (in JSON Lines,label_textbeforelabel); otherwise use--text-columnand--label-column. A JSON line that is not an object is skipped. Labels such asspam,1,phishingandham,0,not_spam,legitimateare understood.
Duplicate messages are counted once. A message in both the spam and the ham sources is kept as spam, as the spam sources are read first. To build on the bundled model rather than start empty, add --merge.
Use the model:
spamscanner scan message.eml --model my-model.json
SPAMSCANNER_MODEL=/var/lib/spamscanner/model.json spamscanner milterconst scanner = new SpamScanner({classifier: '/var/lib/spamscanner/model.json'});How much mail is enough: a few hundred messages of each kind give a useful model, a few thousand a good one. Keep the two roughly balanced, and keep mail you do not want to filter (password resets, invoices from your own suppliers) in the ham.
Measure it#
Keep some mail out of training and measure on it:
spamscanner eval --model my-model.json --spam held-out/spam --ham held-out/hamThe bundled model on SMS messages in 21 languages that it never saw, most of them in languages it barely knows:
Messages: 13028 spam, 92406 ham
Precision: 87.34% Recall: 10.01% F1: 17.96%
False positives: 189 (0.20% of ham) False negatives: 372
Unsure: 87222 (82.73%)Precision is how much of what it calls spam is spam; recall, how much of the spam it catches. Unsure messages count as missed spam here, though in a scan the other checks and the language model can still catch them. The number to watch is false positives: ham marked as spam. In the run above, the model is unsure about most of these messages rather than wrong about them, which is the intended behavior for languages it has little mail in.
--json gives the same numbers for scripts.
Learning from reports#
When users move mail into or out of a Junk folder, teach the model one message at a time:
spamscanner learn spam message.eml --model /var/lib/spamscanner/model.json
spamscanner learn ham message.eml --model /var/lib/spamscanner/model.jsonThe first learn creates the file from the bundled model. Over HTTP, POST /learn/spam and /learn/ham on the HTTP API do the same, and spamc -L spam works against the spamd server with --allow-tell. Dovecot's IMAPSieve can call either when a message is moved.
From Node.js:
await scanner.learn(raw, 'spam');
await scanner.unlearn(raw, 'spam'); // undo, before learning it as ham
scanner.saveModel('/var/lib/spamscanner/model.json');A message reported as misclassified should be unlearned from the wrong class before it is learned in the right one, if it was learned before.
The bundled model#
model/classifier.json is built by npm run model:train from these public datasets on Hugging Face, all under open licenses:
| Dataset | License | Content |
|---|---|---|
| FredZhang7/all-scam-spam | Apache-2.0 | Messages and emails in 43 languages |
| SetFit/enron_spam | Public research corpus | The Enron-Spam corpus |
| alt-gnome/telegram-spam | CC0-1.0 | Russian Telegram messages |
| tanaos/synthetic-spam-detection-dataset-german, -italian, -spanish | MIT | Synthetic messages |
| kudzaiprichard/aura-phishing-email-corpus (SpamAssassin, Nazario, Nigerian fraud) | CC-BY-4.0 | Mailing lists, newsletters, spam, phishing and fraud emails |
| anmolshrivastav/scam_ham_india_14_languages | MIT | Messages in 14 languages of India |
| chairulridjal/high-accuracy-email-classifier-indonesian | Apache-2.0 | Indonesian emails by category |
| JeisonJimenez/emails_2025 | Apache-2.0 | A company's mail in Spanish |
| imnim/multiclass-email-classification, weijianzhg/email-triage-action-seed, dedsec995/Email_Classification_Text, vpakarinen/open-wiki-email-templates, neurocheckout-ai/synthetic-abandoned-cart-email-examples, dhnanjay/MarketMail-AI-Dataset | MIT, Apache-2.0, CC-BY-4.0 | Ordinary mail: bills, bookings, newsletters, promotions and account notices |
| AmazonScience/massive | CC-BY-4.0 | What people ask of an assistant, in 20 languages |
| sinabis-group/sinabis-100k-synthetic-emails | CC-BY-4.0 | A made-up company's mail in French |
| scripts/everyday-mail | With this project | Everyday mail and frauds in 23 languages, written for this project |
The ham includes ordinary business and shop mail (invoices, order and account notices, newsletters, promotions), so such mail alone does not read as spam.
Bounces in the datasets are left out: they are only among the spam, which would teach the classifier that every bounce is spam.
The last three rows are ordinary text in the languages the other datasets have mostly spam of (French, Portuguese, Dutch, the Nordic languages, Polish, Turkish, Arabic, Japanese, Korean, Chinese and more), where every ordinary word would otherwise read as spam. Of 10,000 ordinary texts in 20 languages that the model was not trained on, 1.4% get a probability of 80% or more (4.8% without these rows) and 0.1% one of 99% or more. This text is not real mail, and the frauds written for a language are few: the classifier is made surer of ordinary mail in these languages, and catches little more of their spam.
It learned from 81,254 spam and 133,317 ham messages. The script holds out every tenth message, trains on the rest and measures the classifier alone, without the other checks:
| Held-out test | Messages | Precision | Recall | False positives | Unsure |
|---|---|---|---|---|---|
| English | 8,481 | 100.0% | 96.3% | 0.0% | 2.6% |
| Russian | 1,680 | 100.0% | 97.4% | 0.0% | 2.2% |
| Italian | 1,392 | 99.0% | 81.0% | 0.9% | 12.6% |
| German | 1,314 | 97.9% | 71.4% | 1.9% | 22.3% |
| Spanish | 2,088 | 98.7% | 53.3% | 0.4% | 16.2% |
| Enron-Spam | 2,870 | 100.0% | 95.2% | 0.0% | 2.6% |
| all-scam-spam | 4,222 | 99.9% | 92.2% | 0.0% | 9.1% |
| All | 21,329 | 99.6% | 76.6% | 0.2% | 14.2% |
Spam here means a classifier probability of 99% or more, the point at which the classifier alone reaches the spam threshold. In a scan, spam it is less sure of still gets points, and the other checks add theirs.
The German, Spanish and Italian results come from synthetic datasets, which contain near-identical messages labelled both spam and ham: part of that error is in the labels, not the model. Mail in your own languages is the best fix. The numbers, with every language and dataset, are in the model's metadata.metrics.
More languages#
npm run model:train -- --with multilingual-sms adds the SMS Spam Multilingual Collection: the SMS Spam Collection machine-translated into 21 languages. It is left out of the bundled model because its card gives a GPL license; check that it suits how you share the model. Trained with it, the held-out results for languages the bundled model barely knows were:
| Language | Messages | Precision | Recall | False positives |
|---|---|---|---|---|
| Chinese | 432 | 100.0% | 86.1% | 0.0% |
| Arabic | 432 | 100.0% | 83.1% | 0.0% |
| Korean | 412 | 100.0% | 78.3% | 0.0% |
| Japanese | 486 | 96.1% | 87.5% | 0.5% |
| Hindi | 494 | 100.0% | 76.2% | 0.0% |
| French | 511 | 98.7% | 88.6% | 0.6% |
| Turkish | 222 | 100.0% | 70.4% | 0.0% |
Retrain it#
npm run model:train # downloads the datasets into data/ the first time; needs 4 GB of memory
npm run model:train -- --no-download # reuse data/
npm run model:train -- --out /tmp/model.json --max-features 200000The model file#
A model is a JSON file: the number of spam and ham messages learned, and for each hashed feature how many spam and ham messages contained it, sorted and base64-encoded. It holds no words and no message text. --max-features keeps only the most frequent features and --min-count drops rare ones, trading accuracy for size; the bundled model keeps 400,000 features in about 6 MB.
Models from Spam Scanner 6 and earlier cannot be loaded: they hashed different features. Train a new one from the same mail.