Postfix and SpamAssassin on OS X Tiger

I wrote about setting up Postfix on Tiger before. But after quite a while of procrastination I decided I also wanted to do something about the flood of spam that was sent to hohndel.org. SpamAssassin seems to be the preferred method to go (if you are in the open source camp). It’s bundled with Mac OS X server - but why spend that money… it’s easy enough to set up from scratch.

These instructions are based on a posting by Kalinga Athulathmudali where he describes a similar setup, but not for OS X.

First make sure you have your CPAN setup straight. Some hints to make sure all is well are here.

Next, install SpamAssasin.
$ sudo perl -MCPAN -e shell
cpan[1]> install Mail::SpamAssassin
quit

Use the System Preferences of OS X to create a user named spamfilter. Give it a random password and make sure the user isn’t allowed to administer the system.

To work around a couple of issues with the way postfix deals with the return values of filters we’ll create a little script that will do the filtering.

# Clean up when done or when aborting.
trap "rm -f /tmp/out.$$" 0 1 2 3 15
# Pipe message to spamc
cat | /usr/bin/spamc -u spamfilter > /tmp/out.$$
/usr/sbin/sendmail -i "$@" < /tmp/out.$$
# Postfix returns the exit status of the Postfix sendmail command.
exit $?

Next we make sure that spamd is started whenever the system boots. For this we simply create an entry in the StartupItems - a tar file with the necessary instructions can be found on the SpamAssassin Wiki.

Now you need to make sure that SpamAssassin is called from postfix; edit /etc/postfix/master.cf with the following two changes. First make sure that smtp over inet looks like this

smtp inet n - n - - smtpd
    -o content_filter=spamfilter:dummy


and then add an entry for the spamfilter:

spamfilter unix - n n - - pipe
    flags=Rq user=spamfilter argv=/usr/local/bin/spamfilter -f ${sender} -- ${recipient}


the last line starting with flags=Rq is actually one line; this WordPress theme makes it a little hard to render this correctly.

Run postfix reload to force postfix to read the new configuration and watch your logfiles to make sure that spamd is called correctly (remember that you need to start spamd as root in the background - or you can just reboot which will take care of that as well). Your /var/log/mail.log file should contain entries like this:

spamd[???]: spamd: connection from localhost [127.0.0.1] at port 49671
spamd[???]: spamd: setuid to spamfilter succeeded
spamd[???]: spamd: processing message for spamfilter:???

The final step is now to filter the spam mail from your normal mail flow. I prefer to use procmail for that. A simple entry like this in your .procmailrc file should do the trick (but that depends on the folder layout of your preferred mail client… as I mentioned before, I prefer mutt. In that case this should work:

PATH=/usr/bin:/bin:/usr/local/bin:.
SPAMMAIL=$HOME/Mail/spam

:0:
* ^X-Spam-Level: \*\*\*\*\*
$SPAMMAIL

With this all mail with a Spam-Level of five or more will not be in your normal inbox but instead in a mailbox named spam in your mail folder.

As always, corrections and suggestions for improvement are welcome.

Thanks for visiting!
I hope this was helpful - if not, please leave a comment and let me know why! Were you searching for something else? Did I miss an important aspect?

No Comment

No comments yet

Leave a reply

FireStats icon Powered by FireStats