Spam filtering
Spam filtering is the automatic detection and removal of unsolicited bulk messages, most often email, before they reach a recipient's inbox. Since the late 1990s many filters have relied at least partly on machine learning: a filter is trained on examples of spam and legitimate mail (called "ham") and learns to label new messages, a binary classification task.[13] Early learning-based filters used naive Bayes classifiers, following a 1998 paper by Mehran Sahami, Susan Dumais, David Heckerman and Eric Horvitz and Paul Graham's 2002 essay "A Plan for Spam".[5][6] Google has described using neural networks in Gmail's spam filter since 2015, and in December 2024 it said Gmail had begun using a large language model trained on phishing, malware and spam.[16][20] Researchers treat spam filtering as a problem of adversarial classification, because spammers change their messages to get past whatever filter is in use.[21][12]
Background
The word "spam" for network abuse comes from a Monty Python sketch in which a restaurant serves nearly everything with Spam and a group of Vikings keeps singing the word. According to Brad Templeton, who researched the word's history, the term moved through bulletin boards and MUDs before it was applied to Usenet and email. He dates the earliest documented email spam to 1978, when Digital Equipment Corporation sent an invitation to a product reception to ARPANET addresses on the US west coast, and he describes the April 1994 Usenet advertisement by two Phoenix lawyers, Canter and Siegel, as the event that made the word popular.[1]
Spam is still a large share of all email. The security company Kaspersky estimated that spam made up 47.27% of global email traffic in 2024 and 44.99% in 2025.[3][2] Harris Drucker, Donghui Wu and Vladimir Vapnik defined spam in 1999 as unwanted email, "the electronic version of junk mail", and noted that bulk email is cheap to send and that blocking known senders is easy to evade because spammers can obtain new sending addresses.[4]
The two kinds of error are not equally costly. A spam message that reaches the inbox is a nuisance, while a legitimate message sent to the junk folder (a false positive) may never be read. Research papers therefore commonly report errors on spam and on ham separately and tune filters toward few false positives.[5][10][13]
Rule-based filtering
Before learning-based filters, many commercial products let users write their own logical rules for junk mail. Sahami and colleagues argued in 1998 that this was a weak approach: it assumed users could write robust rules, and the rules had to be retuned by hand as spam changed.[5] Graham reported a similar experience with handwritten features. Looking only for the word "click" caught 79.7% of his spam corpus with 1.2% false positives, but making his rule-based filters stricter produced more false positives.[6]
Rule scoring did not disappear. Apache SpamAssassin, an open-source filter maintained by the Apache Software Foundation, combines many tests in a scoring framework: text analysis, Bayesian filtering, DNS blocklists and collaborative filtering databases.[9] Graham criticized such scores in 2002, arguing that a probability has a clear meaning while a sum of rule weights does not.[6]
Naive Bayes filtering
Sahami, Dumais, Heckerman and Horvitz (1998)
"A Bayesian Approach to Filtering Junk E-Mail", presented at the AAAI Workshop on Learning for Text Categorization in 1998, framed junk filtering as a decision-theoretic problem: a probabilistic classifier combined with unequal costs for the two kinds of misclassification.[5] The authors used a naive Bayes classifier, which assumes that each feature is conditionally independent of the others given the class. For a message with features $x_1, \dots, x_n$, the probability that it is junk ($J$) is proportional to the prior times the product of the per-feature likelihoods:
Features were words from the subject and body, about 35 hand-made phrase features such as "FREE!" and "only $", and about 20 non-textual features such as the sender's domain type and the share of non-alphanumeric characters in the subject line. The authors kept the 500 features with the highest mutual information with the class. To reflect the higher cost of losing legitimate mail, a message was labeled junk only if its estimated probability of being junk exceeded 99.9%.[5]
The main test used 1,789 real messages (1,578 junk, 211 legitimate), split by arrival time into 1,538 training and 251 test messages:[5]
The paper concluded that domain knowledge added on top of the raw text greatly improved accuracy. A further experiment that split junk into two subclasses (pornographic and other) made results worse, which the authors put down mainly to the extra variance of a model with more parameters.[5]
Paul Graham's "A Plan for Spam" (2002)
Bayesian filtering reached a wide audience through "A Plan for Spam", an essay the programmer Paul Graham published in August 2002 that was picked up by Slashdot.[6][7] Graham argued that content-based filters were the way to stop spam, because whatever else a spammer changes, the message itself has to be delivered.[6] His filter differed from a textbook naive Bayes classifier in several deliberate ways:[6]
- It started from one spam corpus and one non-spam corpus of about 4,000 messages each, and tokenized the whole message, including headers, embedded HTML and JavaScript.
- Counts in the non-spam corpus were doubled to bias the filter against false positives, and only tokens seen more than five times were used.
- Each token's spam probability was clamped between 0.01 and 0.99, and a token never seen before got 0.4.
- Only the fifteen "most interesting" tokens in a new message, those whose probabilities were farthest from 0.5, were combined, and the message was treated as spam above 0.9.
The fifteen probabilities $p_1, \dots, p_{15}$ were combined as
Graham reported that the tuned filter would "miss less than 5 per 1000 spams, with 0 false positives" on his own mail, and recommended that each user train a filter on their own messages.[6] In a January 2003 follow-up given as a talk at that year's Spam Conference, he reported that about 1,750 spams arrived between 10 December 2002 and 10 January 2003 and four got through, a filtering rate of about 99.75%. In the same talk he contrasted his results with an earlier academic filter by Pantel and Lin, which he said caught 92% of spam with 1.16% false positives, and attributed much of the gap to that filter ignoring message headers and training on only 160 spam and 466 non-spam messages.[7]
Refinements and open-source filters
Gary Robinson proposed changes to Graham's method in a March 2003 Linux Journal article. He adjusted the probability of rarely seen words with a Bayesian prior and combined word probabilities with Fisher's inverse chi-square method instead of the product formula. Tim Peters of the SpamBayes project then proposed an indicator built from those combined probabilities that sits near 0.5 when a message gives no clear evidence either way, or strong evidence both ways. SpamBayes used that middle range to mark messages as uncertain for the user to review, and Robinson wrote that SpamBayes and Bogofilter had used the techniques to improve filtering significantly.[8]
Which naive Bayes?
"Naive Bayes" covers several different models. In 2006 Vangelis Metsis, Ion Androutsopoulos and Georgios Paliouras pointed out that Sahami's filter used a multivariate Bernoulli model with Boolean (present or absent) attributes, while Pantel and Lin's filter effectively used the multinomial model, which counts term frequencies. They compared five versions on six new public datasets, called Enron-Spam, that paired ham from individual Enron employees with fresh spam and kept the messages in time order. Two less-used versions, flexible Bayes and multinomial naive Bayes with Boolean attributes, gave the best results, and the authors leaned toward the latter because it was cheaper to run.[11] AI Wiki's naive Bayes article covers the model variants and smoothing in more detail.
Other learning methods
Researchers tested most mainstream text classifiers on spam. Drucker, Wu and Vapnik compared support vector machines with Ripper, Rocchio and boosted decision trees in 1999. SVMs did best with binary features, boosted trees and SVMs both had acceptable accuracy and speed, and SVMs trained in much less time.[4]
Andrej Bratko, Gordon Cormack and colleagues showed in 2006 that adaptive data compression models can serve as spam classifiers. Their filters modeled each message as a sequence of characters or bits with dynamic Markov compression or prediction by partial matching, which skips tokenization altogether. In their evaluation the compression models outperformed established spam filters.[12]
Transformer language models were later applied as well. Maxime Labonne and Sean Moran compared BERT-like models, sentence transformers and sequence-to-sequence models with naive Bayes and LightGBM baselines on four public datasets in 2023. The language models beat the baselines in most cases, especially when only a few labeled examples were available. They also introduced Spam-T5, a fine-tuned version of Flan-T5.[15]
Evaluation and datasets
Because the costs of the two errors differ, evaluation became a research topic of its own. Androutsopoulos and co-authors made the cost explicit in 2000: if blocking a legitimate message is $\lambda$ times as costly as letting a spam through, a message is classified as spam when
which is the same as a probability threshold $t = \lambda / (1 + \lambda)$. Sahami's 99.9% threshold corresponds to $\lambda = 999$. The authors evaluated naive Bayes at $\lambda$ values of 1, 9 and 999 and concluded that a naive Bayes filter would need additional safety nets to be viable in practice.[10]
The Text REtrieval Conference (TREC) introduced a Spam Track in 2005. Its framework presents messages to a filter one at a time in arrival order, and the correct label is revealed right after each decision, imitating a user who reports mistakes. Filters were scored on ham misclassification percentage, spam misclassification percentage, their logistic average, and the area above the ROC curve. In 2005, 12 groups submitted 44 filters, and 9 variants of open-source filters were also run, for 53 in all. The organizers concluded that content-based spam filters "can be quite effective, but not a panacea".[13]
| Dataset | Year | Contents | Source |
|---|---|---|---|
| Ling-Spam | 2000 | 2,893 messages: 2,412 from the Linguist mailing list and 481 spam | [10] |
| TREC 2005 public corpus (trec05p-1) | 2005 | 39,399 ham (Enron email) and 52,790 spam | [13] |
| Enron-Spam | 2006 | Six datasets of Enron users' ham mixed with spam, in time order | [11] |
| SMS Spam Collection | 2011 | 5,574 SMS messages labeled spam or ham | [14] |
Machine learning at large providers
The largest email services describe their filters only in general terms, mostly in company blog posts, and the figures below are the companies' own claims. Google said in July 2015 that less than 0.1% of email in the average Gmail inbox was spam and less than 0.05% of wanted mail went to the spam folder. The same post said the filter had begun using an artificial neural network to catch spam that could pass for wanted mail, and that it could now reflect individual users' preferences.[16]
In February 2019 Google said that new TensorFlow models were blocking about 100 million additional spam messages a day, including image-based messages, mail with hidden embedded content, and mail from newly created domains, and that these complemented existing machine learning and rule-based protections.[17]
In November 2023 Google described RETVec, a multilingual text vectorizer built to resist typos and character-level manipulation. Replacing the Gmail spam classifier's previous vectorizer with RETVec improved the spam detection rate by 38% over the baseline, cut the false positive rate by 19.4%, and reduced the model's TPU usage by 83%, according to the company.[18] The RETVec paper was presented at NeurIPS 2023 and the code was released under the Apache 2 license.[19] In December 2024 Google said a new large language model trained on phishing, malware and spam blocked 20% more spam than before and reviewed 1,000 times more user-reported spam each day.[20]
Adversarial dynamics
Spam filters face an opponent that adapts. Nilesh Dalvi, Pedro Domingos and colleagues wrote in 2004 that naive Bayes filters were at first quite successful, but spammers soon learned to insert non-spam words and break up spam words with punctuation, and the two sides settled into a "never-ending game". They modeled classification as a game between the classifier and an adversary and built a classifier that is optimal given the adversary's optimal strategy, which in their spam experiments greatly outperformed a classifier trained in the standard way.[21] (See game theory and adversarial attack.)
The main attack types studied are:
| Attack | How it works | Reported effect |
|---|---|---|
| Obfuscation | Misspelling or altering spam words, such as "vi@gra" | Graham argued a trained filter learns the altered word too, and that "c0ck" becomes stronger evidence of spam than the original word[6][22] |
| Good word attack | Adding words typical of legitimate mail to a spam message | Against naive Bayes and maximum entropy filters, adding 150 words or fewer got half of blocked spam through without filter feedback, and 30 words did when the attacker could query the filter[22] |
| Training data poisoning | Getting crafted messages into the filter's training set | A dictionary attack made SpamBayes unusable when the attacker controlled 1% of training messages; focused attacks blocked specific legitimate emails[23] |
| Character tricks | Homoglyphs, invisible characters and keyword stuffing | Google built RETVec to make Gmail's classifier resilient to such manipulation[18] |
Blaine Nelson and colleagues at UC Berkeley, who demonstrated the poisoning attacks in 2008, also proposed defenses, including a "Reject On Negative Impact" method that does not train on messages that hurt the filter's accuracy.[23] AI Wiki's data poisoning article covers the attack class more broadly.
Spam written by language models
Generative models have given spammers a new way to rewrite messages. Barracuda Networks, working with researchers at Columbia University and the University of Chicago, trained detectors to recognize AI-generated text, applied them to a dataset of malicious and unsolicited email from February 2022 to April 2025, and estimated that 51% of spam was generated by AI by April 2025, compared with 14% of business email compromise attacks. They found AI-generated emails more formal and with fewer grammatical errors, features that they said likely help the messages get past detection systems.[24]
Malte Josten and Torben Weis used GPT-3.5 Turbo to rewrite spam emails in 2024 and found that SpamAssassin classified up to 73.7% of the rewritten spam as legitimate, against a maximum of 0.4% for a simpler dictionary-replacement attack. They estimated the cost at 0.17 cents per email.[25] Language-model detectors are not immune either: a 2025 study fine-tuned GPT-2 and BERT as spam filters on the Enron, Ling-Spam and SMS Spam Collection datasets and found them effective but susceptible to adversarial and data poisoning attacks.[26]
Sender authentication
Content filters run alongside checks on where a message came from. Three IETF standards let a receiving server check a sender's domain:
| Standard | Purpose | Specification |
|---|---|---|
| SPF (Sender Policy Framework) | A domain lists the hosts allowed to use its name, and the receiving host checks it | RFC 7208, April 2014[27] |
| DKIM (DomainKeys Identified Mail) | A domain takes responsibility for a message with a cryptographic signature verified through a public key in DNS | RFC 6376, September 2011[28] |
| DMARC | The owner of the author's domain publishes how to handle mail that fails validation and requests reports | RFC 9989, May 2026, which replaced RFC 7489[29] |
In October 2023 Google announced that from February 2024 senders of more than 5,000 messages a day to Gmail addresses would have to authenticate their mail, offer one-click unsubscription and honor it within two days, and stay under a reported spam rate threshold. A Yahoo product director was quoted in the announcement backing the same changes as an industry standard.[30] The same announcement said Gmail's AI-based defenses stopped more than 99.9% of spam, phishing and malware and blocked nearly 15 billion unwanted emails each day.[30]
Continue reading
- Naive BayesNaive Bayes is a family of probabilistic classification algorithms that apply Bayes' theorem under a strong ("naive") assumption that every feature is conditionally independent of every other feature given the…Mentioned 13 times above
- Data poisoningData poisoning is a class of adversarial attack in which a malicious actor deliberately corrupts the training data used to build machine learning modelsMentioned 3 times above
- Adversarial attackAn adversarial attack is a technique for crafting inputs that are deliberately designed to cause artificial intelligence systems, particularly machine learning models, to produce incorrect or undesired outputs.Listed in See also
- Bayes' theoremBayes' theorem (also called Bayes' rule or Bayes' law) is a fundamental theorem of probability theory that describes how to update the probability of a hypothesis given new evidence.Listed in See also
- Text Classification ModelsText classification models are machine learning systems that assign one or more predefined categorical labels to a span of natural language text, such as positive vs. negative, spam vs. hamListed in See also
- AI in CybersecurityAI in cybersecurity is the use of artificial intelligence and machine learning to both defend computer systems and conduct cyberattacks, spanning threat detection, security operations center (SOC) automation…Listed in See also
More from See also
More to read
Topics:AI in CybersecurityMachine LearningNatural Language Processing
References
- ^Templeton, Brad. "Origin of the term "spam" to mean net abuse". templetons.com. templetons.com/...spamterm
- ^Kulikova, Tatyana; Altukhova, Olga; Dedenok, Roman; Kovtun, Andrey; Shimko, Irina; Lazaricheva, Anna. "Spam and phishing in 2025". Securelist (Kaspersky), 2026-02-11. securelist.com/...118785
- ^Kulikova, Tatyana; Altukhova, Olga; Dedenok, Roman; Kovtun, Andrey; Shimko, Irina; Lazaricheva, Anna. "Spam and phishing in 2024". Securelist (Kaspersky), 2025-02-19. securelist.com/...115536
- ^1 ^2Drucker, Harris; Wu, Donghui; Vapnik, Vladimir N. "Support Vector Machines for Spam Categorization". IEEE Transactions on Neural Networks 10(5), 1048-1054, September 1999. doi.org/...72.788645
- ^1 ^2 ^3 ^4 ^5 ^6 ^7Sahami, Mehran; Dumais, Susan; Heckerman, David; Horvitz, Eric. "A Bayesian Approach to Filtering Junk E-Mail". AAAI Workshop on Learning for Text Categorization, 1998. microsoft.com/...approach-to-filtering-junk-e-mail
- ^1 ^2 ^3 ^4 ^5 ^6 ^7 ^8Graham, Paul. "A Plan for Spam". paulgraham.com, August 2002. paulgraham.com/spam
- ^1 ^2Graham, Paul. "Better Bayesian Filtering". paulgraham.com, January 2003. paulgraham.com/better
- ^Robinson, Gary. "A Statistical Approach to the Spam Problem". Linux Journal, 2003-03-01. linuxjournal.com/...6467
- ^Apache Software Foundation. "Apache SpamAssassin". spamassassin.apache.org. spamassassin.apache.org
- ^1 ^2 ^3Androutsopoulos, Ion; Koutsias, John; Chandrinos, Konstantinos V.; Paliouras, George; Spyropoulos, Constantine D. "An evaluation of Naive Bayesian anti-spam filtering". Workshop on Machine Learning in the New Information Age, 11th European Conference on Machine Learning, Barcelona, 2000, pp. 9-17. arXiv:cs/0006013. arxiv.org/...0006013
- ^1 ^2Metsis, Vangelis; Androutsopoulos, Ion; Paliouras, Georgios. "Spam Filtering with Naive Bayes - Which Naive Bayes?". Third Conference on Email and Anti-Spam (CEAS 2006), Mountain View, California, 2006. www2.aueb.gr/...ceas2006_paper.pdf
- ^1 ^2Bratko, Andrej; Cormack, Gordon V.; Filipič, Bogdan; Lynam, Thomas R.; Zupan, Blaž. "Spam Filtering Using Statistical Data Compression Models". Journal of Machine Learning Research 7, 2673-2698, 2006. jmlr.org/...bratko06a.pdf
- ^1 ^2 ^3 ^4Cormack, Gordon; Lynam, Thomas. "TREC 2005 Spam Track Overview". Proceedings of the Fourteenth Text REtrieval Conference (TREC 2005), NIST. trec.nist.gov/...SPAM.OVERVIEW.pdf
- ^Almeida, Tiago; Hidalgo, Jose. "SMS Spam Collection". UCI Machine Learning Repository, 2011, donated 2012-06-21. doi:10.24432/C5CC84 (introduced in Almeida, Hidalgo and Yamakami, "Contributions to the study of SMS spam filtering: new collection and results", ACM Symposium on Document Engineering, 2011). archive.ics.uci.edu/...sms+spam+collection
- ^Labonne, Maxime; Moran, Sean. "Spam-T5: Benchmarking Large Language Models for Few-Shot Email Spam Detection". arXiv:2304.01238, 2023. arxiv.org/...2304.01238
- ^1 ^2Somanchi, Sri Harsha. "The mail you want, not the spam you don't". Official Gmail Blog, 2015-07-09. gmail.googleblog.com/...you-want-not-spam-you-dont
- ^Kumaran, Neil. "Spam does not bring us joy - ridding Gmail of 100 million more spam messages with TensorFlow". Google Workspace Blog, 2019-02-07. workspace.google.com/...m-messages-with-tensorflow
- ^1 ^2Bursztein, Elie; Zhang, Marina. "Improving Text Classification Resilience and Efficiency with RETVec". Google Security Blog, 2023-11-29. security.googleblog.com/...ing-text-classification
- ^Bursztein, Elie; Zhang, Marina; Vallis, Owen; Jia, Xinyu; Kurakin, Alexey. "RETVec: Resilient and Efficient Text Vectorizer". 37th Conference on Neural Information Processing Systems (NeurIPS 2023). arXiv:2302.09207. arxiv.org/...2302.09207
- ^1 ^2Wen, Andy. "Email scams surge over the holiday - here's how Gmail keeps you safe". Google (The Keyword), 2024-12-18. blog.google/...gmail-holidays-2024-spam-scam
- ^1 ^2Dalvi, Nilesh; Domingos, Pedro; Mausam; Sanghai, Sumit; Verma, Deepak. "Adversarial Classification". Proceedings of the Tenth ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 2004, pp. 99-108. doi.org/...1014052.1014066
- ^1 ^2Lowd, Daniel; Meek, Christopher. "Good Word Attacks on Statistical Spam Filters". Conference on Email and Anti-Spam (CEAS), 2005. ceas.cc/...125.pdf
- ^1 ^2Nelson, Blaine; Barreno, Marco; Chi, Fuching Jack; Joseph, Anthony D.; Rubinstein, Benjamin I. P.; Saini, Udam; Sutton, Charles; Tygar, J. D.; Xia, Kai. "Exploiting Machine Learning to Subvert Your Spam Filter". First USENIX Workshop on Large-Scale Exploits and Emergent Threats (LEET '08), 2008. usenix.org/...nelson.pdf
- ^Hao, Wei. "Half the spam in your inbox is generated by AI - its use in advanced attacks is at an earlier stage". Barracuda Networks Blog, 2025-06-18. blog.barracuda.com/...half-spam-inbox-ai-generated
- ^Josten, Malte; Weis, Torben. "Investigating the Effectiveness of Bayesian Spam Filters in Detecting LLM-modified Spam Mails". EAI International Conference on Digital Forensics & Cyber Crime, 2024. arXiv:2408.14293. arxiv.org/...2408.14293
- ^Tang, Qiyao; Li, Xiangyang. "An Investigation of Large Language Models and Their Vulnerabilities in Spam Detection". HotSoS 2025 (work in progress paper). arXiv:2504.09776. arxiv.org/...2504.09776
- ^Kitterman, S. "Sender Policy Framework (SPF) for Authorizing Use of Domains in Email, Version 1". RFC 7208, IETF, April 2014. rfc-editor.org/...rfc7208
- ^Crocker, D.; Hansen, T.; Kucherawy, M. (eds.). "DomainKeys Identified Mail (DKIM) Signatures". RFC 6376, IETF, September 2011. rfc-editor.org/...rfc6376
- ^Herr, T.; Levine, J. (eds.). "Domain-Based Message Authentication, Reporting, and Conformance (DMARC)". RFC 9989, IETF, May 2026. rfc-editor.org/...rfc9989
- ^1 ^2Kumaran, Neil. "New Gmail protections for a safer, less spammy inbox". Google (The Keyword), 2023-10-03. blog.google/...rity-authentication-spam-protection
Improve this article
Add missing citations, update stale details, or write a clearer explanation. Anyone can edit: changes go live when you publish, and every edit is checked by an editor.
2 revisions · v2 · 3,522 words · full history
Fact-checks are independent of edits: a reviewer re-verifies the article against its sources and stamps the date. How we verify
Research and drafting on this wiki are AI-assisted, under named human editorial standards. How AI is used here
Reviewer note: Independent fact-check before publication on November 18, 2026: 118 claims checked against 34 cited and independent sources; 0 material and 6 minor corrections applied before publication.
Cite this page: AI Wiki. "Spam filtering." aiwiki.ai, updated 18 Nov 2026, fact-checked 18 Nov 2026. CC BY 4.0. https://aiwiki.ai/wiki/spam_filtering