Spam, one of the most irritating things when using mail.
In this article I will explain how to configure SpamAssassin to filter spam and to learn from messages you mark as Spam.
Based on assumption messages remaining in your INBOX will be learned as Ham (not spam).
When a message in your INBOX is Spam, you only have to move it to the Spam folder to let SpamAssassin learn it as spam.
SpamAssassin Setup
First of all, lets configure SpamAssassin correctly to start filtering Spam.
Go to the Spamassassin Setup page and configure the settings like shown in the below screenshot.

Where do you want the spam to go?
Send the spam to the user’s spam folder.
What score threshold do you wish to use?
Low Threshold (5.0)
Would you like to delete high scoring spam?
Yes, block all spam scoring higher than: 10 (1-50, no decimals)
Do you wish to rewrite the subject of a spam email?
No, leave the subject unchanged.
How should the spam be delivered?
Don’t use attachments (dangerous).
Second is to install a script which will learn what is Spam or Ham to SpamAssassin.
This can be done on two ways, one script that runs as root and starts sa-learn for every mailbox under the user which owns the mailbox or a script that runs as an unprivileged user.
As extra feature it will remove spam messages from INBOX.spam who are older than one month.
Learn as unprivileged user
This will explain how to run the script as an unprivileged user, you have to place this script in ~/scripts/sa-learn.sh and create a cronjob to call it once in a while. The interval I choose is 5 minutes. I don’t have many mailboxes so this is fine for me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#!/bin/sh # # Learning script for SpamAssassin on DirectAdmin # # Will learn what is Spam or Ham and delete Spam older than 30 days. # Runs as unprivileged user and scans only domains owned by that user. # # Marius van Witzenburg <info@mariusvw.com> # saExit() { echo "Stopping..." exit 0 } trap saExit SIGINT r=/home/$USER/imap for d in $(ls $r) do dr=$r/$d if [ -h $dr ]; then continue; fi for s in new cur do for m in $(ls -d $dr/*/Maildir 2>/dev/null) do if [ -d $m/.INBOX.spam/$s ] then echo "Learning spam via $m/.INBOX.spam/$s ..."; sa-learn --no-sync --spam $m/.INBOX.spam/$s fi if [ -d $m/.Junk/$s ] then echo "Learning spam via $m/.Junk/$s ..."; sa-learn --no-sync --spam $m/.Junk/$s fi if [ -d $m/$s ] then echo "Learning ham via $m/$s ..."; sa-learn --no-sync --ham $m/$s fi done done if [ -d "$m/.INBOX.spam/cur" ] then echo "Cleaning old Spam from $m/.INBOX.spam/cur ..." find $m/.INBOX.spam/cur -type f -mtime +30 -delete fi if [ -d "$m/.Junk/cur" ] then echo "Cleaning old Spam from $m/.Junk/cur ..." find $m/.Junk/cur -type f -mtime +30 -delete fi done echo ""; echo "Syncing..."; sa-learn --sync echo ""; echo "Current status:" sa-learn --dump magic |
Don’t forget to make the script executable:
1 |
chmod 700 ~/scripts/sa-learn.sh |
Then add a cronjob to your account via the DirectAdmin control panel.
1 2 3 |
MAILTO=your@mail.address # Learn SpamAssassin something every 5 minutes */5 * * * * $HOME/scripts/sa-learn.sh |
Optionally instead of mailing the output you can mute the output.
1 2 |
# Learn SpamAssassin something every 5 minutes */5 * * * * $HOME/scripts/sa-learn.sh >/dev/null |
Or mute all output including errors
1 2 |
# Learn SpamAssassin something every 5 minutes */5 * * * * $HOME/scripts/sa-learn.sh >/dev/null 2>&1 |
Learn as root user
This will explain how to run the script as root, you have to place this script in /root/scripts/sa-learn.sh and create a cronjob to call it once in a while. The interval I choose is 15 minutes. This is higher than the unprivileged-user script due you have to look through more mailboxes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#!/bin/sh # # Learning script for SpamAssassin on DirectAdmin # # Will learn what is Spam or Ham and delete Spam older than 30 days. # Runs as root and scans for all Maildirs for all users. # # Marius van Witzenburg <info@mariusvw.com> # saExit() { echo "Stopping..." exit 0 } trap saExit SIGINT for m in $(ls -d /home/*/imap/*/*/Maildir 2>/dev/null) do parts=(${m//\// }) shelluser=${parts[1]} maildomain=${parts[3]} mailuser=${parts[4]} dr=/home/$shelluser/imap/$maildomain if [ -h $dr ]; then continue; fi for s in new cur do if [ -d "$m/.INBOX.spam/$s" ] then echo "Learning spam via $m/.INBOX.spam/$s ..." sudo -u $shelluser -- sa-learn --no-sync --spam "$m/.INBOX.spam/$s" fi if [ -d "$m/$s" ] then echo "Learning ham via $m/$s ..." sudo -u $shelluser -- sa-learn --no-sync --ham "$m/$s" fi done if [ -d "$m/.INBOX.spam/cur" ] then echo "Cleaning old Spam from $m/.INBOX.spam/cur ..." find $m/.INBOX.spam/cur -type f -mtime +30 -delete fi if [ -d "$m/.Junk/cur" ] then echo "Cleaning old Spam from $m/.INBOX.spam/cur ..." find $m/.Junk/cur -type f -mtime +30 -delete fi echo "" sudo -u $shelluser -- sa-learn --sync echo "-------------------------" echo "" done |
Don’t forget to make the script executable:
1 |
chmod 700 /root/scripts/sa-learn.sh |
Then add a cronjob to your account via the DirectAdmin control panel.
1 2 3 |
MAILTO=your@mail.address # Learn SpamAssassin something every 15 minutes */15 * * * * /root/scripts/sa-learn.sh |
Optionally instead of mailing the output you can mute the output.
1 2 |
# Learn SpamAssassin something every 15 minutes */15 * * * * /root/scripts/sa-learn.sh >/dev/null |
Or mute all output including errors
1 2 |
# Learn SpamAssassin something every 15 minutes */15 * * * * /root/scripts/sa-learn.sh >/dev/null 2>&1 |
Information
The following links were inspiring or helped me to get the right information how to use SpamAssassin Bayesian classifier.