Knowledgebase: Security
Train SpamAssassin
Posted by on 02 July 2012 01:16 PM

You can train SpamAssassin to behave like you want it to... and customer Nathan Olson (thanks Nathan!) broached this topic in an old forum post about SpamAssassin training options.

Let's get started:

  1. Make sure your server has perl module DB_File installed; if it doesn't get in touch with us by submitting a Support ticket

  2. Make two new folders in your IMAP/webmail system called 'spam' and 'ham'
    Note: Spam is where you put spam you find, and ham is where you put examples of good mail (and misfires from the Spam folder). You can also use your inbox for this if you want.

  3. After activating Spam Assassin, log into your FTP account and change to 'home/.spamassassin'

  4. Open the file marked 'user_prefs' and at the bottom of the document, append these rules:

    auto_learn 1
    ok_languages en
    ok_locales en
    report_safe 1
    required_score 8
    use_bayes 1
    use_dcc 1
    use_pyzor 1
    use_razor2 1

  5. Change to the directory 'home/public_html/cgi-bin' and create a document with 755 as permissions called ''

  6. ​

    #These are comments, they will give instructions on how to use the script.

    echo "Learning SPAM"

    #Put your domain here

    for USER in $(ls $DNAME)
    echo ""
    echo "Processing $DNAME$USER"
    /usr/local/cpanel/3rdparty/bin/sa-learn --spam --showdots $DNAME/$USER/.spam/cur/
    /usr/local/cpanel/3rdparty/bin/sa-learn --spam --showdots $DNAME/$USER/.Junk/cur/

    #If you use a spam collection folder other than .spam or .Junk, simply uncomment the following line (remove the #) and edit the folder path:
    #/usr/local/cpanel/3rdparty/bin/sa-learn --spam --showdots $DNAME/$USER/spamfoldernamehere/
    /usr/local/cpanel/3rdparty/bin/sa-learn --ham --showdots $DNAME/$USER/.ham/cur/

    #This will delete the SPAM and move HAM back to your inbox.

    rm -rf $DNAME/$USER/.spam/cur/*

    #Place # next to mv and delete # next to rm below if you would prefer
    #to delete ham instead of moving it back to inbox.
    #It seems easier to just put it back as you identified that it is mail you want.

    mv $DNAME/$USER/.ham/cur/* $DNAME/$USER/cur/ > /dev/null 2>&1
    #rm -rf $DNAME/$USER/.ham/cur/*

    echo "Done"

  7. Log into cPanel and click Cron Jobs

  8. Input your email address so that the results of the two cron scripts will be returned to you each day

  9. After selecting 'Advanced (Unix Style)' input the following (using your account name where it says 'accountname'):
    Note: if Advanced (Unix Style) isn't available as an option, just proceed with inputting the following info.

    0 5 * * * /home/accountname/public_html/cgi-bin/

    Note: where 'accountname' is the cPanel account name.

(1 vote(s))
This article was helpful
This article was not helpful

Comments (1)
Joel Nickerson
26 July 2012 08:14 PM
Thanks to Nathan for this script. I had to modify it slightly to get it to run on my ASO account, and also set it to look at read mail in my spam and ham folders.

Just be careful with the rm commands so you don't lose ham messages from the spam folder before you have a chance to check them

echo "Learning SPAM"
for USER in $(ls $DNAME)
cd $USER
echo ""
echo "Processing $DNAME$USER"
sa-learn --spam --showdots .Junk/cur/
sa-learn --spam --showdots .Junk/new/
sa-learn --ham .Ham/cur/
sa-learn --ham .Ham/new/
rm -rf /.Junk/cur/*
rm -rf /.Junk/new/*
rm -rf /.Ham/cur/*
rm -rf /.Ham/new/*
cd ..
cd ..
echo "Done"
Post a new comment
Full Name:
Help Desk Software by Kayako fusion
ERROR: This domain name (, does not match the domain name in the license key file

For assistance with your license, please contact the Kayako support team: