Training DSPAM with scripts

Please note that this information is no longer up to date. The content of this site has moved to a new domain which is kept up to date. Apologies for the inconvienince.

I have chosen to train DSPAM by moving the spam messages that I receive into a Junk folder in my mail client. I’ve made a small script that checks all Junk folders on the server and feeds them to DSPAM as false negatives to learn DSPAM what spam looks like. This is currently the easiest way to train DSPAM without having to rely on the web based GUI. Please note that this only works if you use IMAP folders, if your users use POP3 we have to look at another solution.

The script looks into the database of postfixadmin to see which users are defined on your system and where their mail directory is located. It then feeds every single message to DSPAM indicating it as an error and to correct DSPAM’s behaviour. After the message is processed the it is removed from the folder.

You can select which IMAP folder your want to use for storing all your SPAM including false negatives (uncaught spam) by changing this entry in the script (don’t forget the leading .):


You can define the folder where it should get the false positives (email wrongly identified as spam) in this variable (don’t forget the leading .):


Configure your postfixadmin database settings with these entries in the script:


If you want the script not to delete the identified SPAM when the email has been processed then change the DELETESPAM option in something else than YES, like NO:


The same rule is valid for the NotSpam folder. To not delete the email from the folder for false positives set this variable to NO.


If you copy the script into /etc/periodic/daily and make it executable it will run once a day and learn each users spam.

You can download the script train.dspam here

Please note that DSPAM won’t catch spam at first, you’ll need to train it first. The longer you train it the better it will get. You can use your email client junk mail controls to train DSPAM. Configure your mail client to move emails it identifies into the Junk folder. See the screenshots below on how to configure and Thunderbird to do this.

18 Responses to “Training DSPAM with scripts”

  1. Dave Says:

    hi. since i updated to your new script version i get:

    Thu Nov 16 15:44:45 CET 2006 Begin AutoSpam processing
    ./dspam.train: line 20: mysql: command not found
    Thu Nov 16 15:44:45 CET 2006 End AutoSpam processing

    did you change anything else? unfortunately i didn’t backup my old version.



  2. Dave Says:

    i just added
    on top
    and changed the mysql call to
    $MYSQLDIR/mysql and so on. maybe that helps others, too.

  3. Dave Says:

    hmm.. maybe i found a little mistake in the script.
    when it runs it sais:

    rm: dspam.users: No such file or directory
    Thu Nov 16 18:00:23 CET 2006 End AutoSpam processing

    it tries to delete a non existant file. maybe there is something wrong?

  4. Richard5 Says:

    Thanks Dave, I’ve solved the issue with the dspam.users file and corrected the mysql command. I added MYSQL=/usr/local/mysql/bin/mysql to include the full command line in line with the DSPAM variable.

  5. Dave Says:

    hmm. but i am still getting this error:

    Thu Nov 16 20:29:23 CET 2006 Begin AutoSpam processing
    Thu Nov 16 20:29:23 CET 2006 Processing in /usr/local/virtual/
    rm: dspam.users: No such file or directory
    Thu Nov 16 20:29:23 CET 2006 End AutoSpam processing

    seems like the dspam.users which should be deleted is not existant.

  6. Richard5 Says:

    Sorry, typo… Please try again ;-)

  7. Dave Says:

    excellent. now it works fine!
    i am just working on another project (nothing to do with this setup – i work as a freelance music editor), when my client pays (i hope he will) i’ll donate some bucks and i really hope that others donate you some bucks, too for all the time you are donating into these tutorials!

  8. DSPAM with VExim and IMAP training at Al Maw Says:

    [...] Spam is getting worse, so I’m trying out DSPAM. I’ve added it into my vexim set-up, but during the initial training phase, at least, it’s tedious to have to forward mail to training addresses. I use IMAP, so I figured, it’d be nice to get DSPAM to pull files out of a folder I can just drag unclassified spam into. I found a bash script which does the right thing for postfix. [...]

  9. Ischia Says:

    Is possible use Dspam from command line for analizing a file with emails inside?

  10. e_ Says:

    your idea of training dspam with imap subfolders of spam or ham is a pretty good way to do it, too bad you aren’t using an imap folder but a maildir.. which is not what most of the imap server provide. also this script only works with mysql as dspams backend :/
    well, too bad. thank’s for the idea though

  11. Felipe Says:

    Hello, I’m from Brazil.
    Do you have this script for DSPAM+qmail+Mysql ?

  12. Richard5 Says:

    Sorry, I don’t know qmail, maybe someone else can help you.

  13. JayBinks Says:

    would love to know how to use this with Cyrus IMAP server…. any info would be great.

  14. Richard5 Says:

    I don’t know, I haven’t got a Cyrus installation so I don’t know what kind of format is used to store email. If Cyrus uses the maildir format it would work just like with Courier !

  15. Brian Says:


    I have dspam working now, and I’m using RoundCube. Dspam marks messages as [SPAM] but it doesn’t put them in the Junk folder. Is there a way to make that happen?

  16. Brian Says:

    Hi again,

    Never mind — I hadn’t read about maildrop yet, but I guess that’s the answer to my question!

    P.S. Thanks for this site, it’s a fantastic resource!

  17. jagowan Says:

    where i have to put “SPAM_VIRTUAL_FOLDER=.Junk” ?


  18. Richard5 Says:

    In the script itself.