Fetching contributors…
Cannot retrieve contributors at this time
715 lines (533 sloc) 19.9 KB


dspam - dspam integration for qpsmtpd


Uses dspam to classify messages. Use spamassassin, karma, and naughty to train dspam.

Adds the X-DSPAM-Result and X-DSPAM-Signature headers to messages. The latter is essential for training dspam and the former is useful to MDAs, MUAs, and humans.

Adds a transaction note to the qpsmtpd transaction. The note is a hashref with at least the 'class' field (Spam,Innocent,Whitelisted). It will normally contain a probability and confidence rating.


If you enable dspam rejection without training first, you will lose valid mail. The dspam false positive rate is high when untrained. The good news is; dspam learns very, very fast.

The best method way to train dspam is to feed it two large equal sized corpuses of spam and ham from your mail server. The dspam authors suggest avoiding public corpuses. I train dspam as follows:

learn from SpamAssassin

See the SPAMASSASSIN section.

periodic training

I have a script that searches the contents of every users maildir. Any read messages that have changed since the last processing run are learned as ham or spam.

The ham message list consists of read messages in any folder not named like Spam, Junk, Trash, or Deleted. This catches messages that users have read and left in their inbox or filed away into subfolders.

on-the-fly training

The dovecot IMAP server has an antispam plugin that will train dspam when messages are moved to/from the Spam folder.



The path to the dspam binary. If yours is installed somewhere other than /usr/local/bin/dspam, set this.

autolearn [ naughty | karma | spamassassin | any ]


learn naughty messages as spam (see plugins/naughty)


learn messages with negative karma as spam (see plugins/karma)


learn from spamassassins messages with autolearn=(ham|spam). See SPAMASSASSIN.


all of the above, and any future tests too!


Set to a floating point value between 0 and 1.00 where 0 is no confidence and 1.0 is 100% confidence.

If dspam's confidence is greater than or equal to this threshold, the message will be rejected. The default is 1.00.

dspam reject .95

To only reject mail if dspam and spamassassin both think the message is spam, set reject agree.


reject_type [ perm | temp | disconnect ]

By default, rejects are permanent (5xx). Set reject_type temp to defer mail instead of rejecting it.

Set reject_type disconnect if you'd prefer to immediately disconnect the connection when a spam is encountered. This prevents the remote server from issuing a reset and attempting numerous times in a single connection.


dspam must be configured and working properly. I had to modify the following settings on my system:

mysql storage
Trust smtpd
TrainingMode tum
Tokenizer osb
Preference "trainingMode=TOE"
Preference "spamAction=deliver"
Preference "signatureLocation=headers"
TrainPristine off
ParseToHeaders off

Of those changes, the most important is the signature location. This plugin only supports storing the signature in the headers. If you want to train dspam after delivery (ie, users moving messages to/from spam folders), then the dspam signature must be in the headers.

When using the dspam MySQL backend, use InnoDB tables. DSPAM training is dramatically slowed by MyISAM table locks and dspam requires a lot of training. InnoDB has row level locking and updates are much faster.

DSPAM periodic maintenance

Install this cron job to clean up your DSPAM database.;a=tree;f=contrib/dspam_maintenance;hb=HEAD


DSPAM can be trained by SpamAssassin. This relationship between them requires attention to several important details:

  1. dspam must be listed after spamassassin in the config/plugins file. Because SA runs first, I set the SA reject_threshold up above 100 so that all spam messages will be used to train dspam.

    Once dspam is trained and errors are rare, I plan to run dspam first and reduce the SA load.

  2. For autolearn spamassassin to work, autolearn must be enabled and configured in SpamAssassin. SA autolearn will determine if a message is learned by dspam. The settings to pay careful attention to in your SA file are bayes_auto_learn_threshold_spam and bayes_auto_learn_threshold_nonspam. Make sure they are set to conservative values that will yield no false positives.

    If you are using autolearn spamassassin and reject, messages that exceed the SA threshholds will cause dspam to reject them. Again I say, make sure the SA autolearn threshholds are set high enough to avoid false positives.


For messages with multiple recipients, the user that dspam is running as will be the dspam username.

When messages have a single recipient, the recipient address is used as the dspam username. For dspam to trust qpsmtpd with modifying the username, you must add the username that qpsmtpd is running to to the dspamd.conf file.

ie, (Trust smtpd).


2012-06 - Matt Simerson - added karma & naughty learning support - worked around the DESTROY bug in dspam_process


2012 - Matt Simerson