Store SpamAssassin bayes in SQL



This article will guide you to configure related components to store SpamAssassin Bayes data in SQL server, and allow webmail users to report spam with one click.

Tested with:



Create required SQL database used to store bayes data

We need to create a SQL database and necessary tables to store SpamAssassin bayes data. The RPM package installed on CentOS 6 doesn't ship SQL template for bayes database, so we have to download it from Apache web site. We're running SpamAssassin-3.3.1, so what we need is this SQL template file: If you're running different version, please find the proper SQL file here:

# cd /root/
# wget

Create MySQL database and import SQL template file:

# mysql -uroot -p
mysql> CREATE DATABASE sa_bayes;
mysql> USE sa_bayes;
mysql> SOURCE /root/bayes_mysql.sql;

Create a new MySQL user (with password sa_user_password) and grant permissions. IMPORTANT NOTE: Please replace password sa_user_password by your own password.

mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON sa_bayes.* TO sa_user@localhost IDENTIFIED BY 'sa_user_password';

Enable Bayes modules in SpamAssassin

Edit /etc/mail/spamassassin/, add (or modify below settings):

use_bayes          1
bayes_auto_learn   1
bayes_auto_expire  1

# Store bayesian data in MySQL
bayes_store_module Mail::SpamAssassin::BayesStore::MySQL
bayes_sql_dsn      DBI:mysql:sa_bayes:

# Store bayesian data in PostgreSQL
#bayes_store_module Mail::SpamAssassin::BayesStore::PgSQL
#bayes_sql_dsn      DBI:Pg:sa_bayes:

bayes_sql_username sa_user
bayes_sql_password sa_user_password

# Override the username used for storing data in the database.
# This could be used to group users together to share bayesian filter data.
# You can also use this config option to trick sa-learn to learn data as a
# specific user.
# In iRedMail, SpamAssassin is called by Amavisd, so we must set it to be
# same as Amavisd daemon user:
#   - on Linux, it's user `amavis`.
#   - on FreeBSD, it's user `vscan`.
#   - on OpenBSD, it's user `_vscan`.
bayes_sql_override_username amavis

Make sure SpamAssassin will load bayes modules:

# /etc/init.d/amavisd stop
# amavisd -c /etc/amavisd/amavisd.conf debug 2>&1 | grep -i 'bayes'
May 16 09:59:33 ... SpamAssassin loaded plugins: ..., Bayes, ...
May 16 10:27:38 ... extra modules loaded after daemonizing/chrooting:
    Mail/SpamAssassin/BayesStore/, Mail/SpamAssassin/BayesStore/, ...

Looks fine, now press Ctrl-C to terminate above command, and start Amavisd service again normally:

# /etc/init.d/amavisd restart

It is required to initialize the database by learning a message. We use the sample spam email shipped in the RPM package provided by CentOS 6:

# rpm -ql spamassassin | grep 'sample-spam'

# sa-learn --spam --username=vmail /usr/share/doc/spamassassin-3.3.1/sample-spam.txt
Learned tokens from 1 message(s) (1 message(s) examined)

Enable Roundcube plugin: markasjunk2

# cd /var/www/roundcubemail/plugins/markasjunk2/
# cp
$rcmail_config['markasjunk2_learning_driver'] = 'cmd_learn';
$rcmail_config['markasjunk2_read_spam'] = true;
$rcmail_config['markasjunk2_unread_ham'] = false;
$rcmail_config['markasjunk2_move_spam'] = true;
$rcmail_config['markasjunk2_move_ham'] = true;
$rcmail_config['markasjunk2_mb_toolbar'] = true;

$rcmail_config['markasjunk2_spam_cmd'] = 'sa-learn --spam --username=vmail %f';
$rcmail_config['markasjunk2_ham_cmd'] = 'sa-learn --ham --username=vmail %f';
$rcmail_config['plugins'] = array(..., "markasjunk2");
# disable_functions =show_source,system,shell_exec,passthru,exec,phpinfo,proc_open ;

# NEW SETTING. exec is removed.
disable_functions =show_source,system,shell_exec,passthru,phpinfo,proc_open ;

You will see a new toolbar button after logging into Roundcube webmail:

Check SQL database sa_bayes before we testing this plugin:

# mysql -uroot -p
mysql> USE sa_bayes;
mysql> SELECT COUNT(*) FROM bayes_token;
| count(*) |
|       65 |

Back to Roundcube webmail, select a spam email (or a testing email), click Mark as Junk button, then this email will be scanned by command sa-learn. Check database sa_bayes again to make sure it's working:

# mysql -uroot -p
mysql> USE sa_bayes;
mysql> SELECT COUNT(*) FROM bayes_token;
| count(*) |
|      143 |

Note: You may get different result number as shown above.

So far so good. That's all we need to do.