This is the fourth part of a five part tutorial that will show you how to install a full featured email server on your Raspberry Pi. This tutorial covers how to mark emails as spam with Spamassassin.
The parts are:
The Introduction & Contents Page (read first)
Raspberry Pi Email Server Part 1: Postfix
Raspberry Pi Email Server Part 2: Dovecot
Raspberry Pi Email Server Part 3: Squirrelmail
Raspberry Pi Email Server Part 4: Spam Detection with Spamassassin
Raspberry Pi Email Server Part 5: Spam Sorting with LMTP & Sieve
Intro
I don’t actually get very many spam emails (famous last words, right?) but the occasional email gets past my helo access restrictions list (discussed in Raspberry Pi Email Server Part 1: Postfix). So, I decided to set up Spamassassin, a program that will check incoming emails and mark them as spam if they look suspicious. Spamassassin is pretty clever, it uses bayesian filtering to decide what’s spam and what’s not, and it will learn based on previous results, so it gets more accurate over time if you correct it when it gets things wrong. Spamassassin will only mark emails as spam, it will not sort them into folders for you as well. We’ll be doing the sorting with Dovecot’s Local Mail Transfer Protocol (LMTP) and the Sieve plugin, in the next tutorial: Raspberry Pi Email Server Part 5: Spam Sorting with LMTP & Sieve. Let’s get started:
Installing & Configuring Spamassassin
First, install Spamassassin:
sudo apt-get update sudo apt-get install spamassassin
Now we need to edit values in the file /etc/spamassassin/local.cf
. Some of these may already be set, in which case you can leave them as they are; add or amend the others as necessary: This one will add the spam score to the subject line of emails that Spamassassin considers to be spam:
rewrite_header Subject [***** SPAM _SCORE_ *****]
Spamassassin will also flag spam emails with “X-Spam-Flag: YES” in the headers. This flag is what we will eventually use to sort emails with; the rewritten subject line is purely to make the score easier to see. This next setting will tell Spamassassin to modify headers only, without making any changes to the body of the email:
report_safe 0
This one lowers the threshold for mail to be considered spam from 5 to 2. You can change this later if you get lots of false positives, but it’s nice to have some emails set off the rules to begin with, just so you know it’s working:
required_score 2.0
This tells Spamassassin to use Bayesian filtering:
use_bayes 1
This turns on automatic learning:
bayes_auto_learn 1
Now edit /etc/default/spamassassin and set:
ENABLED=1
You can now start the spamassassin daemon:
sudo service spamassassin start
If you are using a modern Debian derivative (Jessie or later), the init system has changed to systemd. You need to run this additional command to enable spamassassin, which will cause it to automatically start when you boot:
sudo systemctl enable spamassassin
Instructing Postfix to use Spamassassin
At this stage, the Spamassassin daemon is running but none of your incoming emails are being passed through it. We need to edit this line in /etc/postfix/master.cf
(just under the headers):
smtp inet n - - - - smtpd -o content_filter=spamassassin
And append this to the bottom of that same file, which will pipe the output back to Postfix using the Postfix’s Sendmail compatibility interface:
spamassassin unix - n n - - pipe user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
Note: this is all one line, even if it appears wrapped in your browser. Now restart postfix:
sudo service postfix restart
If you get an error like this:
[....] Stopping Postfix Mail Transport Agent: postfix/usr/sbin/postconf: fatal: file /etc/postfix/master.cf: line 22: bad field count postfix/postfix-script: fatal: cannot execute /usr/sbin/postconf! failed!
…then check the whitespace before the -o in content_filter=spamassassin. I can’t quite remember what I did but I think I changed tabs to spaces or the other way round, and then restarted postfix. Now watch the mail log with this command:
tail -f /var/log/mail.log
…and send a test email. You should see testing something like this:
Jan 8 22:21:18 samhobbs postfix/smtpd[952]: connect from blu0-omc2-s3.blu0.hotmail.com[65.55.111.78] Jan 8 22:21:19 samhobbs postfix/smtpd[952]: 542E83F519: client=blu0-omc2-s3.blu0.hotmail.com[65.55.111.78] Jan 8 22:21:19 samhobbs postfix/cleanup[957]: 542E83F519: message-id=Jan 8 22:21:19 samhobbs postfix/qmgr[941]: 542E83F519: from= , size=1579, nrcpt=1 (queue active) Jan 8 22:21:19 samhobbs spamd[445]: spamd: connection from localhost [127.0.0.1] at port 35680 Jan 8 22:21:19 samhobbs postfix/smtpd[952]: disconnect from blu0-omc2-s3.blu0.hotmail.com[65.55.111.78] Jan 8 22:21:19 samhobbs spamd[445]: spamd: setuid to debian-spamd succeeded Jan 8 22:21:19 samhobbs spamd[445]: spamd: creating default_prefs: /var/lib/spamassassin/.spamassassin/user_prefs Jan 8 22:21:19 samhobbs spamd[445]: config: created user preferences file: /var/lib/spamassassin/.spamassassin/user_prefs Jan 8 22:21:19 samhobbs spamd[445]: spamd: processing message for debian-spamd:111 Jan 8 22:21:24 samhobbs spamd[445]: spamd: clean message (0.0/2.0) for debian-spamd:111 in 5.0 seconds, 1541 bytes. Jan 8 22:21:24 samhobbs spamd[445]: spamd: result: . 0 - HTML_MESSAGE,MSGID_FROM_MTA_HEADER scantime=5.0,size=1541,user=debian-spamd,uid=111,required_score=2.0,rhost=localhost,raddr=127.0.0.1,rport=35680,mid= ,autolearn=ham Jan 8 22:21:24 samhobbs postfix/pickup[940]: D83DE3F521: uid=111 from= Jan 8 22:21:24 samhobbs postfix/pipe[958]: 542E83F519: to= , relay=spamassassin, delay=5.7, delays=0.44/0.05/0/5.2, dsn=2.0.0, status=sent (delivered via spamassassin service) Jan 8 22:21:24 samhobbs postfix/qmgr[941]: 542E83F519: removed Jan 8 22:21:24 samhobbs postfix/cleanup[957]: D83DE3F521: message-id= Jan 8 22:21:24 samhobbs postfix/qmgr[941]: D83DE3F521: from= , size=1890, nrcpt=1 (queue active) Jan 8 22:21:25 samhobbs postfix/local[964]: D83DE3F521: to= , relay=local, delay=0.2, delays=0.06/0.1/0/0.03, dsn=2.0.0, status=sent (delivered to maildir) Jan 8 22:21:25 samhobbs postfix/qmgr[941]: D83DE3F521: removed Jan 8 22:21:25 samhobbs spamd[439]: prefork: child states: II
So the steps you can see here are:
- Outlook server connects to RasPi/Postfix on port 25
- Postfix accepts the message and hands it to Spamassassin to process
- Spamassassin decides the message is clean and marks it as HAM
- The email is passed back from Spamassassin to Postfix and delivered to the inbox
Training Spamassassin
We’ve deliberately set the score limit for spam to a low value. This inevitably means we’ll get some false positives, but we can use these to train Spamassassin and make it better. First, some things to understand about the Maildir format we’re using. Here’s what my structure looks like:
admin@samhobbs ~ $ sudo ls -al /home/sam/Maildir/ total 604 drwx------ 12 sam sam 4096 Mar 6 14:55 . drwxr-xr-x 3 sam sam 4096 Mar 5 23:07 .. drwx------ 2 sam sam 36864 Mar 6 12:59 cur -rw------- 1 sam sam 11920 Mar 6 04:14 dovecot.index -rw------- 1 sam sam 415744 Mar 6 14:50 dovecot.index.cache -rw------- 1 sam sam 10332 Mar 6 13:08 dovecot.index.log -rw------- 1 sam sam 32784 Mar 5 16:22 dovecot.index.log.2 -rw------- 1 sam sam 30 Jan 13 22:30 dovecot-keywords -rw------- 1 sam sam 144 Mar 3 17:49 dovecot.mailbox.log -rw------- 1 sam sam 27138 Mar 6 09:27 dovecot-uidlist -rw------- 1 sam sam 8 Mar 5 23:07 dovecot-uidvalidity -r--r--r-- 1 sam sam 0 Nov 23 22:55 dovecot-uidvalidity.52913278 drwx------ 5 sam sam 4096 Mar 5 22:36 .Drafts drwx------ 5 sam sam 4096 Mar 4 21:53 .foo drwx------ 5 sam sam 4096 Mar 3 17:49 .INBOX.foo drwx------ 2 sam sam 4096 Mar 6 09:37 new drwx------ 5 sam sam 4096 Mar 5 22:36 .Sent drwx------ 5 sam sam 4096 Mar 6 14:37 .Spam -rw------- 1 sam sam 37 Mar 3 17:49 subscriptions drwx------ 5 sam sam 4096 Nov 27 19:00 .Templates drwx------ 2 sam sam 4096 Mar 6 09:27 tmp drwx------ 5 sam sam 4096 Mar 6 04:08 .Trash
You can see I’ve created a couple of test folders here: one top level folder called “foo” and another subfolder in the inbox also called “foo” (.INBOX.foo). Each folder has three subdirectories: new for new (unread) emails, cur for emails that have been read, and tmp for temporary storage during delivery. You can read more about this on the Dovecot Wiki if you’d like to know more. So, the important thing to take away from this is that HAM emails are stored here: /home/username/Maildir/cur
…and SPAM emails will be stored here (after sieve has been configured): /home/username/Maildir/.Spam/cur
Spamassassin has a commandline training tool that is invoked like this:
sa-learn --no-sync [--spam or --ham] [folder/{cur,new}]
Each user has its own spamassassin database, which is located in the user's home directory in a hidden folder (.spamassassin
). By default, the sa-learn
command trains the database in the home directory of the user running the command, and since the spamassassin pipe we set up processes email as the user debian-spamd
, we need to make sure we train the database in debian-spamd
's home directory (which is /var/lib/spamassassin
- you can check by looking in /etc/passwd
). Unfortunately, if you run the command as debian-spamd
using sudo -u debian-spamd command
, you won't have read permissions for your emails. Here’s the plan: move any false positives back into the inbox with your email client, and move any missed spam into the spam folder. Then run these three commands using sudo, so you have permission to read your emails and write to the spamassassin database, and use the --dbpath
option to specify which database to write to:
# Scan HAM sudo sa-learn --dbpath /var/lib/spamassassin/.spamassassin/ --no-sync --ham /home/username/Maildir/{cur,new} # Scan SPAM sudo sa-learn --dbpath /var/lib/spamassassin/.spamassassin/ --no-sync --spam /home/username/Maildir/.Spam/{cur,new} # sync the journal and databases sudo sa-learn --dbpath /var/lib/spamassassin/.spamassassin/ --sync
On my Pi, running the HAM command took about 5mins to process ~500 messages, with WordPress running at the same time. If you’re sure you will always move emails into the correct folders, you could add these two commands to a cron job so that they run regularly and keep everything up to date. Alternatively, you can just run the commands when you notice a few false positives or missed spam emails. Over time, your spam filter will get better and better.
Automated learning using a script
If you don't want to run the commands manually all the time, you can use this simple cron job I wrote. The cron job runs as root, so you don't need the sudo
part we used earlier. Create the script like this:
sudo nano /etc/cron.daily/spamassassin-learn
Now copy and paste this into the file (ctrl + shift + v to paste in nano):
#!/bin/bash # Script by Sam Hobbs, see the following URL for updates: # https://samhobbs.co.uk/2014/03/raspberry-pi-email-server-part-4-spam-detection-with-spamassassin # redirect errors and output to logfile exec 2>&1 >> /var/log/spamassassin.log NOW=$(date +"%Y-%m-%d") # Headers for log echo "" echo "#================================ $NOW ================================#" echo "" # learn HAM echo "Learning HAM from Inbox" sa-learn --dbpath /var/lib/spamassassin/.spamassassin/ --no-sync --ham /home/sam/Maildir/{cur,new} # learn SPAM echo "Learning SPAM from Spam folder" sa-learn --dbpath /var/lib/spamassassin/.spamassassin/ --no-sync --spam /home/sam/Maildir/.Spam/{cur,new} # Synchronize the journal and databases. echo "Syncing" sa-learn --dbpath /var/lib/spamassassin/.spamassassin/ --sync
Important: edit the paths so that they match your username! If you want to scan ham and spam for all users (this only works if you trust all users to be sensible and move ham/spam to the right folder) then replace the username "sam" with a glob ("*"), i.e:
sa-learn --dbpath /var/lib/spamassassin/.spamassassin/ --no-sync --ham /home/*/Maildir/{cur,new}
Now make the script executable:
sudo chmod +x /etc/cron.daily/spamassassin-learn
The script will learn from ham/spam daily, and write a log file at /var/log/spamassassin.log
. Make sure you move any spam you find into your spam folder, and any false positives back into your inbox. Don't worry if ham is accidentally marked as spam one day and gets "learned", if you move the messages to their correct locations then the next time the script runs spamassassin will correct itself.
What’s next?
We’re now done with Spamassassin. The only thing left to do is find a way to sort spam emails directly into the spam folder, which is covered in the next tutorial: Raspberry Pi Email Server Part 5: Spam Sorting with LMTP & Sieve. Feel free to leave a comment to let me know how you get on!
Comments
install spamc
Don't forget to install spamc, otherwise the relay will not work:
sudo apt-get install spamc
Auto installed for me
Instructing Postfix to use Spamassassin
Hello Sam,
In the section "Instructing Postfix to use Spamassassin" you suggest setting in /etc/postfix/master.cf the following:
smtp inet n - - - - smtpd
-o content_filter=spamassassin
However, In part 2 of this tutorial you suggest adding smtps to master.cf. So shouldn't the content filter flag be set for the smtps service too, and not only smtp?
In any case, I'd like to use this comment to thank you for writing this tutorial. I've had some trouble setting up a mail server and your tutorial made my life easier.
Thanks
Ariel
Hi Ariel, thanks for your
IMAP access
hi,
great tutorial!
I set up everything and it's 99% working. The only problem I have is that I cannot receive emails on my client.
I'm using clawsmail, and the 'get mail' button seems just unresponsive.
I do get the emails, and theget stored in ~/Maildir/new folder. Is this correct and my clawsmail settings are wrong?
Or incoming mails are supposed to land somewhere else?
In case you're familiar with clawsmail, these are my settings:
'Basic' tab:
Name of account: myName@mySite.com
Mail address: myName@mySite.com
Protocol: IMAP4
Server for receiving: mySite.com
User ID: myName
password: myPassword
'Receive' tab:
Authentication method: login
IMAP server directory: empty
'SSL' tab:
Use SSL for IMAP4 connection
Use non-blocking SSL
Thanks again for this magnificent tutorial!
error found
hi,
sorry for spamming you.
I managed to fix my clawsmail, so no need to answer me.
Thanks anyway and all my best,
AC
No worries
Spams for training
Hi all,
we have an every-day growing spam database, you can use it to train Spamassassin: http://artinvoice.hu/spams
Regards: Frantique
Spamassassin CPU usage
Just run through this excellent tutorial and am pleased with how well it replicates my existing setup (In addition I have a getmail running to collect mail from my various freemail accounts). However, spamassassin is SLLOOOWWW and maxing out my Pi CPU alarmingly. Is that just par for the course or have I mucked something up? The only change I made in my spamassassin install was to import the old 'dictionary' of learnt spam/ham from my existing box...
It does slow things down, not a problem unless it's multitasking
can't send email through openssl
Hi, thank you for the tutorial. While I can send email to and from k9 and thunderbird, I'm having trouble sending from openssl. I'm using the command provided:
# openssl s_client -connect localhost:465 -quiet
But, whenever I try
rcpt to: user@domain.com
I get the message "554 5.7.1 : Recipient address rejected: Access denied
Any clue whats happening here?
Authentication
Many thanks but...
Hi, you have used 'user=debian-spamd' in the example above, should this have been created for me? Or if I create it, or use another user, what privs should it have? I tried root but that seems to create another error.
Thanks for your work,
Glyn
The user should have been created during installation
Hmmm.
No, user does not exist, tried removing and re-installing but still no such user... Any ideas of creating by hand? My first trawl through Google has not given me a quick fix. Don't worry if not, I'll keep trawling until I get a good idea.
Thanks,
Glyn
Still skeptical
Nope, checked users and groups two ways.
The problem could be that the system is still on debian linux 6, running Linux 2.6.37 on armv5tel. It's a sheevaplug. The second install I did showed :
SqueezePlug:~# apt-get install spamassassin
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
razor libnet-ident-perl libio-socket-ssl-perl pyzor libmail-dkim-perl
The following NEW packages will be installed:
spamassassin
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/1236 kB of archives.
After this operation, 3977 kB of additional disk space will be used.
Selecting previously deselected package spamassassin.
(Reading database ... 59411 files and directories currently installed.)
Unpacking spamassassin (from .../spamassassin_3.3.1-1.1_all.deb) ...
Processing triggers for man-db ...
Setting up spamassassin (3.3.1-1.1) ...
Starting SpamAssassin Mail Filter Daemon: spamd.
The directory wasn't created either.
I may have found enough hints to have a go myself - but I'll wait until I've slept on it! Not something to try this tired...
Many thanks again.
Glyn
Interesting, never heard of a SheevaPlug before!
I have added the user, and
I have added the user, and group, that created the dir, which I chowned. I'll get back to you after I have received some spam with the details.
The sheevaplug looks like a wallwart but has arm +linux, it was one of the first to be so simple and under £100. If I was starting again there are others I would choose.
If you are ever after a project how about documenting LDAP etc needed to create a Windows domain controller? Sometime when I have time free I really want to have a bash at this...
Thanks again! Will report on success or failure.
Glyn
Sounds like you're making
Well, been watching the log
Well, been watching the log all night and have recently received some spam, and it's marked as spam! On to the next lesson soon.
Aha, lucky fellow, the dreaded gpaid werk demands Windows but I'm getting (minutely) better at linux every week.
Yes, If I go for an ADalike I will do my best to document it. Thanks, yet again. Now, back to Telnet client on an Arduino.
Glyn
sa-learn
I am still getting some spam coming thru and therefore I have copied it to a folder called NewSpam and have attempted using sa-learn to train spamassassin but without success. The command I tried was:-
/usr/bin/sa-learn --spam --mbox --showdots /home/alf/Maildir/.NewSpam
Please tell me what I got wrong.
Hi Alf,
X-Spam-Status
part): BTW, I added some new bits at the bottom of the tutorial that you might like to use (including a simple script so that learning is automatic) Samsa-learn
Sorry that it took so long to get back to you. Caused by your email ending up in my Spam folder.....:-( Should not happen again thanks to your showing how to automate spam handling.
Again Thank you.
The irony is killing me
irony
Thank you as you see that worked fine.....:-)
SpamAssassin Error
Hey Sam,
Fist off I would like to say that this is a great tutorial! Everything up until spamassassin has worked great! So the installation and configuration of spam assassin appeared to go smoothly, but when I send an email with spamassassin enabled the email doesn't make it into my inbox. I have been going through my logs and researching online, but haven't as of yet been able to sort out the problem. The only clue I have, is in my logs I get the following error:
Jan 14 06:12:01 raspberrypi spamd[9057]: spamd: processing message <46D1CF71-D35E-400E-AD43-D7571F5C01CD@mac.com> for debian-spamd:110
Jan 14 06:12:03 raspberrypi spamd[9057]: spamd: clean message (0.0/2.0) for debian-spamd:110 in 2.0 seconds, 1266 bytes.
Jan 14 06:12:03 raspberrypi spamd[9057]: spamd: result: . 0 - FREEMAIL_FROM,T_TO_NO_BRKTS_FREEMAIL scantime=2.0,size=1266,user=debian-spamd,uid=110,required_score=2.0,rhost=localhost,raddr=127.0.0.1,rport=59582,mid=<46D1CF71-D35E-400E-AD43-D7571F5C01CD@mac.com>,autolearn=ham
Jan 14 06:12:03 raspberrypi spamc[9230]: exec failed: No such file or directory
Jan 14 06:12:03 raspberrypi postfix/pipe[9229]: DC06140A72: to=, relay=spamassassin, delay=2.5, delays=0.2/0.05/0/2.2, dsn=4.3.0, status=deferred (system resource problem)
Jan 14 06:12:04 raspberrypi spamd[9054]: prefork: child states: II
I have verified that spamc is located in "/usr/bin/spamc" which is something some people have seemed to have an issue with. I have also double checked my master.cf settings which seem correct? Here are the key parts;
smtp inet n - - - - smtpd -o content_filter=spamassassin
spamassassin unix - n n - - pipe user=debian-spamd argv=/usr/bin/spamc -f -e /user/sbin/sendmail -oi -f ${sender} ${recipient}
So I'm not really sure where the problem is? Is it possible that there is a permission issue with a certain file or folder? or that something else could be missing? Any help would be greatly appreciated, Thanks!
spam and git
Ok, SpamAssasin just put [***** SPAM 3.4 *****] in the subject line of an email that a friend sent to my pi email server. So I did a little tuning on it. Can you point me to any good source on how to do the tuning?
I use git for all my code and other settings that I use for various software - to keep track of changes. I'd like to use git to keep track of the changes to the email server settings, postfix, dovecot and spamassassin. I realize that there are not a lot of settings to play with and keep track of in such a formal way. However, do you have any suggestions on the best way use git for this?
I also added the spamassassin configuration folder to my backup script. Here are the folders that I backup from the email server:
/home/pi/ (this one has a git repository already - maybe I just add the email settings files to this repository?)
/home/pi/Maildir
/etc/postfix
/etc/dovecot
/var/log/spamassassin.log
/etc/spamassassin
I'm working on putting Squirrelmail on the ownCloud pi, so the settings for it will be backed up from there.
Reset the required score to 5
Spam 3.4
Here is the reason:
* 3.4 RCVD_ILLEGAL_IP Received: contains illegal IP address
Failure of this test suggests that the mailout software added extra message headers to disguise the real source of the message, or else is not using a professionally run network.
I did a whois of the IP's in "Receved:" section (listed in the header) and really didn't see anything suspicious there. All from yahoo and inktomi.
I asked my friend about what device and software he used, and where he sent the reply to my message (when SpamAssassin put in the 3.4) - if this matters? Waiting for his reply.
Add new comment