The Perfect SpamSnake - Ubuntu Jaunty Jackalope - Page 4

17. MailWatch Installation Instructions

This setup assumes you are using Apache v2.x and not Apache v1.x.

Before Starting, make sure that MailScanner is working before you continue with the MailWatch install!

*Notes for Ubuntu:

You must have a working MailScanner set-up and running copies of MySQL, Apache, and PHP. You must also have the Perl DBD-MySQL package installed for the Perl portions of MailScanner to utilize the MySQL database.

The default php.ini set should have the following set correctly, you may want to check this:

short_open_tag = On
safe_mode = Off
register_globals = Off
magic_quotes_gpc = On
magic_quotes_runtime = Off
session.auto_start = 0

These will be commented out you must remove the "#" to activate them:


17.1 Installation

Download the latest MailWatch release:

tar xzvf mailwatch-1.0.4.tar.gz
cd mailwatch-1.0.4

17.2 Create the database

mysql -p < create.sql

*Note: you will need to modify the above as necessary for your system if you have a root password for your MySQL database (recommended!) - Debian will ask for one.

Create a MySQL user and password & Set-up MailScanner for SQL logging:

mysql -p
mysql> GRANT ALL ON mailscanner.* TO [email protected] IDENTIFIED BY 'password';

Remember the password! You need the single quotes ' to surround your password.

17.3 Edit and move

Edit and change the $db_user and $db_pass values accordingly and move

mv /opt/MailScanner/lib/MailScanner/CustomFunctions/


17.4 Create a MailWatch Web User

mysql mailscanner -u mailwatch -p

Enter password: ******

mysql> INSERT INTO users VALUES ('username',md5('password'),'mailscanner','A','0','0','0','0','0');

17.5 Install & Configure MailWatch

From within the unpacked mailwatch directory move the directory called 'mailscanner' to /var/www/.

mv mailscanner/ /var/www/
cd /var/www/mailscanner

Make a temp directory:

mkdir temp
chgrp www-data temp
chmod g+w temp

Check the permissions of /var/www/mailscanner/images and /var/www/images/cache - they should be ug+rwx and owned by root and in the same group as the web server user.

chown root:www-data images
chmod ug+rwx images
chown root:www-data images/cache
chmod ug+rwx images/cache

Create conf.php by copying conf.php.example and edit the values to suit, you will need to set DB_USER and DB_PASS to the MySQL user and password that you created earlier.

Change these values as shown below:

define(DB_USER, 'mailwatch');
define(DB_PASS, 'password');
define(MAILWATCH_HOME, '/var/www/mailscanner');
define(MS_LIB_DIR, '/usr/lib/MailScanner/');
define(QUARANTINE_USE_FLAG, true);
define(QUARANTINE_FROM_ADDR, '[email protected]');

17.6 Set-up MailScanner

Edit /opt/MailScanner/etc/MailScanner.conf and make sure the following are set:

Quarantine User = root
Quarantine Group = www-data
Quarantine Permissions = 0640
Quarantine Whole Message = yes
Always Looked Up Last = &MailWatchLogging
Quarantine Whole Message As Queue Files = no
Detailed Spam Report = yes
Include Scores In SpamAssassin Report = yes

*Important: Spam Actions, High Scoring Spam Actions and No Spam Actions should also have 'store' as one of the keywords if you want to quarantine those items for bayes learning or viewing from within MailWatch.

17.7 Integrate SQL Blacklist/Whitelist (optional)

If you would like to manage the MailScanner whitelist and blacklist from within the MailWatch web interface perform the following steps.

1. Edit the MySQL connection values within the CreateList subroutine of to match the values you entered previously into Both files should contain the same values. (Look for the following lines in and enter your own data.)

my($db_user) = 'mailwatch';
my($db_pass) = 'password';

2. Copy to /opt/MailScanner/lib/MailScanner/CustomFunctions/.

3. Edit MailScanner.conf and set:

Is Definitely Not Spam = &SQLWhitelist
Is Definitely Spam = &SQLBlacklist

17.8 Fix to allow MailWatch to work with Postfix Inbound/Outbound Queue

Download the patch from

cd /usr/src
tar xvfz postfixmail.tar.gz
cd postfixmail
cp postfix* /var/www/mailscanner/
patch /var/www/mailscanner/functions.php functions.php.diff


17.9 SpamAssassin

First we need to disable the default SpamAssassin configuration file:

mv /etc/spamassassin/ /etc/spamassassin/

Now let's backup the SpamAssassin configuration file in MailScanner then edit:

cp /opt/MailScanner/etc/spam.assassin.prefs.conf /opt/MailScanner/etc/spam.assassin.prefs.conf.back

Add pyzor and razor paths to the top of /opt/MailScanner/etc/spam.assassin.prefs.conf:

pyzor_options --homedir /var/lib/MailScanner/
razor_config /var/lib/MailScanner/.razor/razor-agent.conf

Look for these lines and change them accordingly:

bayes_ignore_header X-YOURDOMAIN-COM-MailScanner
bayes_ignore_header X-YOURDOMAIN-COM-MailScanner-SpamCheck
bayes_ignore_header X-YOURDOMAIN-COM-MailScanner-SpamScore
bayes_ignore_header X-YOURDOMAIN-COM-MailScanner-Information

"YOURDOMAIN-COM" should be replaced with whatever you used for "%org-name%" in the MailScanner.conf file. Leave the "X-" in place. This is the same orgname used in the MailScanner.conf above.

Edit the SpamAssassin v310.pre to enable Razor and DCC:

loadplugin Mail::SpamAssassin::Plugin::DCC
loadplugin Mail::SpamAssassin::Plugin::Razor2

17.10 SpamAssassin SQL Bayes


a. You'll need the perl-DBI and perl-DBD-MySQL modules installed.

Assumptions and Variables:

SpamAssassin Bayes Database Name: sa_bayes
SpamAssassin Bayes Database UserName: sa_user
SpamAssassin Bayes Database Password: sa_password

Create the MySQL database:

First of all, create a database on the server where you intend on storing the bayesian information.

mysql -u root -p

mysql> create database sa_bayes;
mysql> GRANT ALL ON sa_bayes.* TO [email protected] IDENTIFIED BY 'sa_password';
mysql> flush privileges;

Locate the bayes_mysql.sql file:

find / -name bayes_mysql.sql
mysql -u sa_user -p sa_bayes < /path/to/bayes_mysql.sql

Make some changes to your spam.assassin.prefs.conf:

bayes_store_module Mail::SpamAssassin::BayesStore::SQL
bayes_sql_dsn DBI:mysql:sa_bayes:localhost
bayes_sql_username sa_user
bayes_sql_password sa_password
bayes_sql_override_username root

Make sure the following is commented:

#bayes_path /etc/MailScanner/bayes/bayes
#bayes_file_mode 0660

Also add this to your crontab:

30 01 * * * /usr/bin/sa-learn --force-expire --sync -p /opt/MailScanner/etc/spam.assassin.prefs.conf 
Share this page:

Suggested articles

9 Comment(s)

Add comment


By: citybird

 the following setting should also be changed in conf.php in step 17.5

define(MS_CONFIG_DIR, '/opt/MailScanner/etc/');

By: bearman

We got it!

It works if Y substitute:






By: nibb13

Try changing line 4 of create.sql to read:

-- -------------------------------------

That seems to have done the trick for me.

Cheers, nibb13


This is one of the most interesting and useful "how tos", but extremely unprofessionally done and has lots of bugs. I say thank you to the author, but please read the comments and edit the steps and missteps. 

 Also, some steps are extremely for kids who don't know computers, but some steps you say that require extra research that expert users in computers, but not in linux won't be able to follow.

 Also to make it easier, when you say "edit this file" you can simpler provide a command "vi/nano /etc/blahblahblah"

 Now, step 17.9, a little lower you say "Edit the SpamAssassin v310.pre to enable Razor and DCC"
where is it? where do I find it to edit?

By: Jim Morbid

joe /etc/mail/spamassassin/v310.pre



By: ScarEye

17.2 is kicking my ass this what I get when I run

mysql -p < create.sql

[email protected]:/usr/src/mailwatch-1.0.4# mysql -p < create.sql
Enter password:
ERROR 1064 (42000) at line 4: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-------------------------------------------------------



CREATE DATABASE /*' at line 1
[email protected]:/usr/src/mailwatch-1.0.4#

Here is a few lines of create.sql

-- MySQL dump 8.23
-- Host: localhost    Database: mailscanner
-- Server version       3.23.58

-- Current Database: mailscanner

CREATE DATABASE /*!32312 IF NOT EXISTS*/ mailscanner;

USE mailscanner;

-- Table structure for table `audit_log`

CREATE TABLE audit_log (
  timestamp timestamp(14) NOT NULL,
  user varchar(20) NOT NULL default '',
  ip_address varchar(15) NOT NULL default '',
  action text NOT NULL

-- Table structure for table `blacklist`

CREATE TABLE blacklist (
  id int(11) NOT NULL auto_increment,
  to_address text,
  to_domain text,
  from_address text,
  PRIMARY KEY  (id),
  UNIQUE KEY blacklist_uniq (to_address(100),from_address(100))

-- Table structure for table `geoip_country`

CREATE TABLE geoip_country (
  begin_ip varchar(15) default NULL,
  end_ip varchar(15) default NULL,
  begin_num bigint(20) default NULL,
  end_num bigint(20) default NULL,
  iso_country_code char(2) default NULL,
  country text,
  KEY geoip_country_begin (begin_num),
  KEY geoip_country_end (end_num)



I ran a few lines manually and they seem to work.  Then I tried to re-run mysql -p < create.sql

but no luck.

Please help, so close to being done.






 To fix the error 1064 with this setup on a new system, just remove the (14) on the timestamp timestamp entries in create.sql

 There are 2 one at the top and one on line 78 (to get there in nano press CTRL _)

By: ScarEye

Anyone here know how to fix the 2010 bug with spamassassin?  I see this here.  Check this out.

 But in spamsnake we disbale, so what's the work around?




By: Anonymous

In step 17.10, I ran into an error when trying to run:

 mysql -u sa_user -p sa_bayes < /path/to/bayes_mysql.sql

A simple fix is to add the following to the top of the .sql file:

 CONNECT sa_bayes;