For years, I’ve been using mutt as my main mail program. I love being able to use mutt in screen and thereby having access to my email from anywhere, with just ssh or PuTTY. Back when I first created this setup, all opensource webmail software then available was still very poor in functionality, which I why I didn’t use webmail for the “access from anywhere” part.
I have a hosted machine, and the MX records of my domains point to it. My mail is being handled by postfix, and spamfiltered by SpamAssassin. Procmail helps me sort mail into different folders upon arrival.
This setup has some disadvantages. Some spam leaks through, which is annoying – since I only receive a few emails every day, a relatively large portion of the mail I see is spam. Also, turning off or reinstalling my hosted machine means emails don’t arrive.
I recently got a smartphone, and wanted to be able to use it to read my email. I briefly considered installing dovecot on my hosted machine to provide IMAP, but decided to try Google Apps instead.
I followed the Google Apps setup document, and set the MX records of one of my domains to Google’s mailservers. That worked fine. Since the free version of Google Apps doesn’t support more than one domain, I then configured postfix on my hosted machine to forward mail sent to my other domains to gmail.
This turned out to have one big disadvantage: with that setup, all spam received by my hosted machine is sent to Google anyway. Sure, SpamAssassin puts spam score headers in the message, but the message is sent to Google whether it’s spam or not. This results in my hosted machine sending 90% spam and 10% real mail to Google.
I was afraid that Google might start seeing my hosted machine’s IP address as evil because of the relatively large amounts of spam, so I looked for a way to prevent most or all of the spam from being forwarded to Google.
This is what I found: http://www.akadia.com/services/postfix_spamassassin.html
See the section called “Create your own Content Filter“. This was exactly what I was looking for.
Steps 1, 3, 4 and 5 worked fine on my Ubuntu server, although there is a typo in the first step (“filer” instead of “filter”). Oh, and I decided to use /var/spamchk as sideline dir, so I created that dir and chowned it to the filter user. I changed the script in step 2 to the following:
#!/bin/sh # ----------------------------------------------------------------- # File: spamchk # # Purpose: SPAMASSASIN shell-based filter # # Location: /usr/local/bin # # Usage: Call this script from master.cf (Postfix) # # Certified: Ubuntu Linux, Spamassassin 3.3.x, Postfix 2.7.x # ----------------------------------------------------------------- # Variables SENDMAIL="/usr/sbin/sendmail -i" EGREP=/bin/egrep TMPFILE=/tmp/spamchk.$$ SIDELINE_DIR=/var/spamchk # Number of *'s in X-Spam-level header needed to sideline message: # (Eg. Score of 5.5 = "*****" ) SPAMLIMIT=10 # Clean up when done or when aborting. trap "rm -f $TMPFILE" 0 1 2 3 15 # Pipe message to spamc and store in $TMPFILE cat | /usr/bin/spamc -u filter | sed 's/^\.$/../' > $TMPFILE # Are there more than $SPAMLIMIT stars in X-Spam-Level header? if $EGREP -q "^X-Spam-Level: \*{$SPAMLIMIT,}" < $TMPFILE then # Option 1: Move high scoring messages to sideline dir so # a human can look at them later: mv $TMPFILE $SIDELINE_DIR/`date +%Y-%m-%d_%R`-$$ # Option 2: Divert to an alternate e-mail address: # $SENDMAIL xyz@xxxx.xx < $TMPFILE # Option 3: Delete the message # rm -f $TMPFILE else $SENDMAIL "$@" < $TMPFILE fi # Postfix returns the exit status of the Postfix sendmail command. exit $?
I followed these steps and got what I wanted: 90% of the spam my mailserver receives has a score of 10+, and that’s now stored on my server instead of forwarded to Google!