SA-LEARN(1)
๐ธ NAME: sa-learn (SA-LEARN(1)) ๐ธ
Okay so sa-learn is literally the tool that trains SpamAssassin's Bayesian classifier, bestie โ ham vs spam vibes, no cap. This is the SA-LEARN(1) manual page from User Contributed Perl Documentation, and it's giving "teach the filter your vibes" energy. ๐
โจ SYNOPSIS โจ
Run it like this, bestie:
sa-learn [options] [file]...
sa-learn [options] --dump [ all | data | magic ]
Options:
--hamโ Learn the following messages as ham (non-spam). ๐ซถ--spamโ Learn the following messages as spam.--forgetโ Forget the following messages.--use-ignoresโ Usebayes_ignore_fromandbayes_ignore_to.--syncโ Synchronize the database and the journal if needed.--force-expireโ Force a database sync and expiry run.--dbpath <path>โ Allows commandline override (inbayes_pathform) for where to read the Bayes DB from.--dump [all|data|magic]โ Display the contents of the Bayes database. Takes optional argument for what to display.--regexp <re>โ For dump only, specifies which tokens to dump based on a regular expression.-f file,--folders=fileโ Read list of files/directories fromfile.--dirโ Ignored; historical compatibility. It's a no-op, bestie.--fileโ Ignored; historical compatibility. Also a no-op.--mboxโ Input sources are in mbox format.--mbxโ Input sources are in mbx format.--max-size <b>โ Skip messages larger thanbbytes; defaults to 500 KB,0implies no limit.--showdotsโ Show progress using dots.--progressโ Show progress using progress bar.--no-syncโ Skip synchronizing the database and journal after learning.-L,--localโ Operate locally, no network accesses. Use of this is recommended, see documentation. ๐--importโ Migrate data from older version/nonDB_Filebased databases.--clearโ Wipe out existing database. ๐--backupโ Backup, toSTDOUT, existing database.--restore <filename>โ Restore a database fromfilename.-u username,--username=usernameโ Override username taken from the runtime environment, used with SQL.-C path,--configpath=path,--config-file=pathโ Path to standard configuration dir.-p prefs,--prefspath=file,--prefs-file=fileโ Set user preferences file.--siteconfigpath=pathโ Path for site configs (default:/usr/etc/mail/spamassassin).--cf='config line'โ Additional line of configuration.-D,--debug [area,...]โ Print debugging messages.-V,--versionโ Print version.-h,--helpโ Print usage message.
๐ DESCRIPTION ๐
Given a typical selection of your incoming mail classified as spam or ham (non-spam), this tool will feed each mail to SpamAssassin, allowing it to 'learn' what signs are likely to mean spam, and which are likely to mean ham. It's giving classifier glow-up, fr fr. โจ
Simply run this command once for each of your mail folders, and it will ''learn'' from the mail therein. ๐ฆ
๐ Globbing note:csh-style globbing in the mail folder names is supported; in other words, listing a folder name as*will scan every folder that matches. SeeMail::SpamAssassin::ArchiveIteratorfor more details.
If you are using mail boxes in format other than maildir you should use the --mbox or --mbx parameters. ๐ฌ
Files compressed with gzip/bzip2/xz/lz4/lzip/lzo are uncompressed automatically. See Mail::SpamAssassin::ArchiveIterator for more details. ๐
SpamAssassin remembers which mail messages it has learnt already, and will not re-learn those messages again, unless you use the --forget option. Messages learnt as spam will have SpamAssassin markup removed, on the fly. ๐
If you make a mistake and scan a mail as ham when it is spam, or vice versa, simply rerun this command with the correct classification, and the mistake will be corrected. SpamAssassin will automatically 'forget' the previous indications. No dramatic fallout, bestie. ๐ซถ
Users of spamd who wish to perform training remotely, over a network, should investigate the spamc -L switch. ๐
โจ OPTIONS โจ
okay so bestie, this is the whole option buffet โ every single flag, argument, default and warning is here, no cap. Buckle up ๐
--hamโ Learn the input message(s) in the files following the option as ham. If you have previously learnt any of the messages as spam, SpamAssassin will forget them first, then re-learn them as ham. Alternatively, if you have previously learnt them as ham, it'll skip them this time around. If the messages have already been filtered through SpamAssassin, the learner will ignore any modifications SpamAssassin may have made. ๐--spamโ Learn the input message(s) in the files following the option as spam. If you have previously learnt any of the messages as ham, SpamAssassin will forget them first, then re-learn them as spam. Alternatively, if you have previously learnt them as spam, it'll skip them this time around. If the messages have already been filtered through SpamAssassin, the learner will ignore any modifications SpamAssassin may have made. ๐ฅ--folders=filename,-f filenameโsa-learnwill read in the list of folders from the specified file, one folder per line in the file. If the folder is prefixed withham:type:orspam:type:,sa-learnwill learn that folder appropriately, otherwise the folders will be assumed to be of the type specified by--hamor--spam.
โtypeโ above is optional, but is the same as the standard for ArchiveIterator: mbox, mbx, dir, file, or detect (the default if not specified). ๐
--mboxโsa-learnwill read in the file(s) containing the emails to be learned, and will process them inmboxformat (one or more emails per file). โจ--mbxโsa-learnwill read in the file(s) containing the emails to be learned, and will process them inmbxformat (one or more emails per file). ๐--use-ignoresโ Don't learn the message if a from address matches configuration file itembayes_ignore_fromor a to address matchesbayes_ignore_to. The option might be used when learning from a large file of messages from which the hammy spam messages or spammy ham messages have not been removed. ๐--syncโ Synchronize the journal and databases. Upon successfully syncing the database with the entries in the journal, the journal file is removed. ๐ง--force-expireโ Forces an expiry attempt, regardless of whether it may be necessary or not. Note: This doesn't mean any tokens will actually expire. Please see the EXPIRATION section below.
Note: --force-expire also causes the journal data to be synchronized into the Bayes databases. โณ
--forgetโ Forget the input message(s) in the files following the option as previously learnt. ๐--dbpathโ Allows a commandline override of thebayes_pathconfiguration option. ๐ ๏ธ--dump optionโ Display the contents of the Bayes database. Without an option or with thealloption, all magic tokens and data tokens will be displayed.magicwill only display magic tokens, anddatawill only display the data tokens.
Can also use the --regexp RE option to specify which tokens to display based on a regular expression. ๐
--clearโ Clear an existing Bayes database by removing all traces of the database.
WARNING: This is destructive and should be used with care. ๐ญ
--backupโ Performs a dump of the Bayes database in machine/human readable format.
The dump will include token and seen data. It is suitable for input back into the --restore command. ๐พ
--restore=filenameโ Performs a restore of the Bayes database defined by filename.
WARNING: This is a destructive operation, previous Bayes data will be wiped out. ๐ซถ but also ๐ฌ
-h,--helpโ Print help message and exit. ๐-u username,--username=usernameโ If specified this username will override the username taken from the runtime environment. You can use this option to specify users in a virtual user configuration when using SQL as the Bayes backend.
NOTE: This option will not change to the given username, it will only attempt to act on behalf of that user. Because of this you will need to have proper permissions to be able to change files owned by username. In the case of SQL this generally is not a problem. ๐
-C path,--configpath=path,--config-file=pathโ Use the specified path for locating the distributed configuration files. Ignore the default directories (usually/usr/share/spamassassinor similar). ๐--siteconfigpath=pathโ Use the specified path for locating site-specific configuration files. Ignore the default directories (usually/etc/mail/spamassassinor similar). ๐๏ธ--cf='config line'โ Add additional lines of configuration directly from the command-line, parsed after the configuration files are read. Multiple--cfarguments can be used, and each will be considered a separate line of configuration. โจ๏ธ-p prefs,--prefspath=prefs,--prefs-file=prefsโ Read user score preferences from prefs (usually$HOME/.spamassassin/user_prefs). ๐ธ--progressโ Prints a progress bar (to STDERR) showing the current progress. In the case where no valid terminal is found this option will behave very much like the--showdotsoption. ๐-D [area,...],--debug [area,...]โ Produce debugging output. If no areas are listed, all debugging information is printed. Diagnostic output can also be enabled for each area individually; area is the area of the code to instrument. For example, to produce diagnostic output on bayes, learn, and dns, use:
spamassassin -D bayes,learn,dns
Use an empty string (-D '') to indicate no areas when the next item on the command line is a path, to prevent the path from being parsed as an area.
For more information about which areas (also known as channels) are available, please see the documentation at:
https://wiki.apache.org/spamassassin/DebugChannels
Higher priority informational messages that are suitable for logging in normal circumstances are available with an area of info. ๐
--no-syncโ Skip the slow synchronization step which normally takes place after changing database entries. If you plan to learn from many folders in a batch, or to learn many individual messages one-by-one, it is faster to use this switch and runsa-learn --synconce all the folders have been scanned.
Clarification: The state of --no-sync overrides the bayes_learn_to_journal configuration option. If not specified, sa-learn will learn to the database directly. If specified, sa-learn will learn to the journal file.
Note: --sync and --no-sync can be specified on the same commandline, which is slightly confusing. In this case, the --no-sync option is ignored since there is no learn operation. ๐ตโ๐ซ
-L,--localโ Do not perform any network accesses while learning details about the mail messages. This should be normally used, as there really isn't anything Bayes can learn from network lookup results. Official SpamAssassin plugins do not currently do any network lookups when learning, but it's possible that third party ones might. ๐ฐ๏ธ--importโ If you previously used SpamAssassin's Bayesian learner without theDB_Filemodule installed, it will have created files in other formats, such asGDBM_File,NDBM_File, orSDBM_File. This switch allows you to migrate that old data into theDB_Fileformat. It will overwrite any data currently in theDB_File.
Can also be used with the --dbpath path option to specify the location of the Bayes files to use. ๐ฆ
๐ฆ MIGRATION ๐ฆ
okay so plot twist: there are now multiple backend storage modules available for storing user's bayesian data. As such you might want to migrate from one backend to another. Here is a simple procedure for migrating from one backend to another. โจ
Note that if you have individual user databases you will have to perform a similar procedure for each one of them. ๐ฏ
sa-learn --syncโ This will sync any outstanding journal entries. ๐งตsa-learn --backup > backup.txtโ This will save all your Bayes data to a plain text file. ๐sa-learn --clearโ This is optional, but good to do to clear out the old database. ๐งน- Repeat! โ At this point, if you have multiple databases, you should perform the procedure above for each of them. (i.e. each user's database needs to be backed up before continuing.) ๐
- Switch backends โ Once you have backed up all databases you can update your configuration for the new database backend. This will involve at least the
bayes_store_moduleconfig option and may involve some additional config options depending on what is required by the module. (For example, you may need to configure an SQL database.) ๐ sa-learn --restore backup.txtโ Again, you need to do this for every database. ๐ซถ
If you are migrating to SQL you can make use of the -u username option in sa-learn to populate each user's database. Otherwise, you must run sa-learn as the user whose database you are restoring. ๐
๐ซ INTRODUCTION TO BAYESIAN FILTERING ๐ซ
(Shoutout to Michael Bell for this section, bestie really came through ๐ซถ)
ok so if you want the full-length tea on how this works, go to http://www.paulgraham.com/ and read "A Plan for Spam". It's actually pretty readable ngl, even if statistics make me break out in hives ๐ญ
The short semi-inaccurate version: give a spam heuristics engine some training, and it can take the most "spammy" and "hammy" words and run probabilistic analysis on them. And like โ once it's got a basis for the analysis, it can keep learning iteratively, applying both the non-Bayesian and Bayesian rulesets together to create evolving "intelligence". main character energy fr โจ
SpamAssassin 2.50 and later supports Bayesian spam analysis, in the form of the BAYES rules. It's a new feature, it's genuinely powerful, and it's disabled until enough messages have been learnt.
The pros of Bayesian spam analysis:
- Can greatly reduce false positives and false negatives.
- It learns from your mail, so it is tailored to your unique e-mail flow. ๐
- Once it starts learning, it can continue to learn from SpamAssassin and improve over time.
And the cons:
- A decent number of messages are required before results are useful for ham/spam determination.
- It's hard to explain why a message is or isn't marked as spam.
- i.e.: a straightforward rule that matches, say, "VIAGRA" is easy to understand. If it generates a false positive or false negative, it is fairly easy to understand why.
- With Bayesian analysis, it's all probabilities โ "because the past says it is likely, as this falls into a probabilistic distribution common to past spam in your systems". Tell that to your users! Tell that to the client when he asks "what can I do to change this". (By the way, the answer in this case is "use welcomelisting".) ๐
- It will take disk space and memory.
- The databases it maintains take quite a lot of resources to store and use.
๐ GETTING STARTED ๐
Still interested? ok bestie, here's the guidelines for getting this working. ๐ฆ
First a high-level overview:
- Build a significant sample of both ham and spam.
- I suggest several thousand of each, placed in SPAM and HAM directories or mailboxes. Yes, you MUST hand-sort this โ otherwise the results won't be much better than SpamAssassin on its own. Verify the spamminess/haminess of EVERY message. You're urged to avoid using a publicly available corpus (sample) โ this must be taken from YOUR mail server, if it is to be statistically useful. Otherwise, the results may be pretty skewed.
- Use this tool to teach SpamAssassin about these samples, like so:
sa-learn --spam /path/to/spam/folder
sa-learn --ham /path/to/ham/folder
sa-learn --ham hampath1 hampath2 --spam spampath1 spampath2
...
- Let SpamAssassin proceed, learning stuff. When it finds ham and spam it will add the "interesting tokens" to the database. ๐
- If you need SpamAssassin to forget about specific messages, use the
--forgetoption.- This can be applied to either ham or spam that has run through the
sa-learnprocesses. It's a bit of a hammer, really, lowering the weighting of the specific tokens in that message (only if that message has been processed before).
- This can be applied to either ham or spam that has run through the
- Learning from single messages uses a command like this:
sa-learn --ham --no-sync mailmessage
This is handy for binding to a key in your mail user agent. It's very fast, as all the time-consuming stuff is deferred until you run with the --sync option. โก
- Autolearning is enabled by default.
- If you don't have a corpus of mail saved to learn, you can let SpamAssassin automatically learn the mail that you receive. If you are autolearning from scratch, the amount of mail you receive will determine how long until the
BAYES_*rules are activated.
- If you don't have a corpus of mail saved to learn, you can let SpamAssassin automatically learn the mail that you receive. If you are autolearning from scratch, the amount of mail you receive will determine how long until the
๐ฅ EFFECTIVE TRAINING ๐ฅ
Learning filters require training to be effective. If you don't train them, they won't work. Periodt. And on top of that, you need to train them with new messages regularly to keep them up-to-date, or their data will become stale and impact accuracy. ๐ซถ
You need to train with both spam and ham mails. One type of mail alone will not have any effect. (like, genuinely zero, don't try it ๐)
Note that if your mail folders contain things like forwarded spam, discussions of spam-catching rules, etc., this will cause trouble. You should avoid scanning those messages if possible. (An easy way to do this is to move them aside, into a folder which is not scanned.)
If the messages you are learning from have already been filtered through SpamAssassin, the learner will compensate for this. In effect, it learns what each message would look like if you had run spamassassin -d over it in advance.
Another thing to be aware of, is that typically you should aim to train with at least 1000 messages of spam, and 1000 ham messages, if possible. More is better, but anything over about 5000 messages does not improve accuracy significantly in our tests.
Be careful that you train from the same source โ for example, if you train on old spam, but new ham mail, then the classifier will think that a mail with an old date stamp is likely to be spam. ๐ญ
It's also worth noting that training with a very small quantity of ham will produce atrocious results. You should aim to train with at least the same amount (or more if possible!) of ham data than spam. fr fr, ham is the icon here ๐
On an on-going basis, it is best to keep training the filter to make sure it has fresh data to work from. There are various ways to do this:
- Supervised learning This means keeping a copy of all or most of your mail, separated into spam and ham piles, and periodically re-training using those. It produces the best results, but requires more work from you, the user.
(An easy way to do this, by the way, is to create a new folder for 'deleted' messages, and instead of deleting them from other folders, simply move them in there instead. Then keep all spam in a separate folder and never delete it. As long as you remember to move misclassified mails into the correct folder set, it is easy enough to keep up to date.) ๐
- Unsupervised learning from Bayesian classification Another way to train is to chain the results of the Bayesian classifier back into the training, so it reinforces its own decisions. This is only safe if you then retrain it based on any errors you discover.
SpamAssassin does not support this method, due to experimental results which strongly indicate that it does not work well, and since Bayes is only one part of the resulting score presented to the user (while Bayes may have made the wrong decision about a mail, it may have been overridden by another system). ๐ซ
- Unsupervised learning from SpamAssassin rules Also called 'auto-learning' in SpamAssassin. Based on statistical analysis of the SpamAssassin success rates, we can automatically train the Bayesian database with a certain degree of confidence that our training data is accurate.
It should be supplemented with some supervised training in addition, if possible.
This is the default, but can be turned off by setting the SpamAssassin configuration parameter bayes_auto_learn to 0.
- Mistake-based training This means training on a small number of mails, then only training on messages that SpamAssassin classifies incorrectly. This works, but it takes longer to get it right than a full training session would.
๐ฆ FILES ๐ฆ
sa-learn and the other parts of SpamAssassin's Bayesian learner use a set of persistent database files to store the learnt tokens, as follows.
โจ bayes_toks
The database of tokens, containing the tokens learnt, their count of occurrences in ham and spam, and the timestamp when the token was last seen in a message.
This database also contains some 'magic' tokens, as follows: the version number of the database, the number of ham and spam messages learnt, the number of tokens in the database, and timestamps of: the last journal sync, the last expiry run, the last expiry token reduction count, the last expiry timestamp delta, the oldest token timestamp in the database, and the newest token timestamp in the database.
This is a database file, using DB_File. The database 'version number' is 0 for databases from 2.5x, 1 for databases from certain 2.6x development releases, 2 for 2.6x, and 3 for 3.0 and later releases. ๐
โจ bayes_seen
A map of Message-Id and some data from headers and body to what that message was learnt as. This is used so that SpamAssassin can avoid re-learning a message it has already seen, and so it can reverse the training if you later decide that message was learnt incorrectly.
This is a database file, using DB_File.
โจ bayes_journal
While SpamAssassin is scanning mails, it needs to track which tokens it uses in its calculations. To avoid the contention of having each SpamAssassin process attempting to gain write access to the Bayes DB, the token timestamps are written to a 'journal' file which will later (either automatically or via sa-learn --sync) be used to synchronize the Bayes DB. ๐
Also, through the use of bayes_learn_to_journal, or when using the --no-sync option with sa-learn, the actual learning data will take be placed into the journal for later synchronization. This is typically useful for high-traffic sites to avoid the same contention as stated above.
โณ EXPIRATION โณ
Okay so here's the ~situation~ bestie: SpamAssassin can auto-learn messages, which means the Bayes database files could just... keep growing. Forever. Until they fill your disk and ruin your whole life. ๐ญ To keep that from happening, SpamAssassin does journal synchronization and bayes expiration periodically โ but only when certain criteria (listed right below, don't worry) are met.
SpamAssassin can sync the journal and expire the DB tokens either manually or opportunistically. A journal sync is due if --sync is passed to sa-learn (that's the manual one โจ), or if all of the following is true (the opportunistic one):
bayes_journal_max_sizedoes not equal0(because0means don't sync)- the journal file exists
and either:
- the journal file has a size greater than
bayes_journal_max_size
or
- a journal sync has previously occurred, and at least 1 day has passed since that sync
Expiry is due if --force-expire is passed to sa-learn (manual again ๐
), or if all of the following are true (opportunistic):
- the last expire was attempted at least 12hrs ago
bayes_auto_expiredoes not equal0- the number of tokens in the DB is >
100,000 - the number of tokens in the DB is >
bayes_expiry_max_db_size - there is at least a 12 hr difference between the oldest and newest token atimes
๐งฎ EXPIRE LOGIC
So if either the manual or opportunistic method kicks off an expire run, here's the logic it uses, step by step:
- figure out how many tokens to keep. take the larger of either
bayes_expiry_max_db_size * 75%or100,000tokens. therefore, the goal reduction is number of tokens - number of tokens to keep. - if the reduction number is <
1000tokens, abort (genuinely not worth the effort, goodbye ๐). - if an expire has been done before, guesstimate the new atime delta based on the old atime delta. (
new_atime_delta = old_atime_delta * old_reduction_count / goal) - if no expire has been done before, or the last expire looks "weird", do an estimation pass. The definition of "weird" is:
- last expire over 30 days ago
- last atime delta was < 12 hrs
- last reduction count was < 1000 tokens
- estimated new atime delta is < 12 hrs
- the difference between the last reduction count and the goal reduction count is > 50%
๐ ESTIMATION PASS LOGIC
Go through each of the DB's tokens. Starting at 12hrs, calculate whether or not the token would be expired (based on the difference between the token's atime and the db's newest token atime) and keep the count. Work out from 12hrs exponentially by powers of 2. ie: 12hrs * 1, 12hrs * 2, 12hrs * 4, 12hrs * 8, and so on, up to 12hrs * 512 (6144hrs, or 256 days).
The larger the delta, the smaller the number of tokens that will be expired. Conversely, the number of tokens goes up as the delta gets smaller. So starting at the largest atime delta, figure out which delta will expire the most tokens without going above the goal expiration count. Use this to choose the atime delta to use, unless one of the following occurs:
- the largest atime (smallest reduction count) would expire too many tokens. this means the learned tokens are mostly old and there needs to be new tokens learned before an expire can occur.
- all of the atime choices result in 0 tokens being removed. this means the tokens are all newer than 12 hours and there needs to be new tokens learned before an expire can occur.
- the number of tokens that would be removed is < 1000. the benefit isn't worth the effort. more tokens need to be learned.
If the expire run gets past this point, it will continue to the end. A new DB is created since the majority of DB libraries don't shrink the DB file when tokens are removed. So we do the "create new, migrate old to new, remove old, rename new" shuffle. ๐ iconic little dance, no notes.
โ๏ธ EXPIRY RELATED CONFIGURATION SETTINGS
bayes_auto_expireโ used to specify whether or not SpamAssassin ought to opportunistically attempt to expire the Bayes database. The default is1(yes).bayes_expiry_max_db_sizeโ specifies both the auto-expire token count point, as well as the resulting number of tokens after expiry as described above. The default value is150,000, which is roughly equivalent to a6Mbdatabase file if you're usingDB_File.bayes_journal_max_sizeโ specifies how large the Bayes journal will grow before it is opportunistically synced. The default value is102400.
๐ฆ INSTALLATION
The sa-learn command is part of the Mail::SpamAssassin Perl module. Install this as a normal Perl module, using perl -MCPAN -e shell, or by hand. however you vibe ๐ซถ
๐ SEE ALSO
Also, the reading list, because your girl did her homework ๐:
http://www.paulgraham.com/โ Paul Graham's "A Plan For Spam" paperhttp://www.linuxjournal.com/article/6467โ Gary Robinson's f(x) and combining algorithms, as used in SpamAssassinhttp://web.archive.org/web/20120512230723/http://www.bgl.nu/~glouis/bogofilter/โ 'Training on error' page. A discussion of various Bayes training regimes, including 'train on error' and unsupervised training.
โ PREREQUISITES
Mail::SpamAssassin
๐ AUTHORS
The SpamAssassin(tm) Project <https://spamassassin.apache.org/> โ absolute legends, honestly ๐
and that's the whole page, bestie!! go forth and train your bayes filter, you're gonna slay it ๐โจ๐
๐
fin ๐
โ this is perl v5.42.0, dated 2025-08-31, and yes the SA-LEARN(1) manual page ate and left no crumbs ๐ฆ
๐ฆ where this page comes from
- alpine 3.23: spamassassin-doc 4.0.2-r0 ยท Apache-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms