For some weird reason, our postfix+spamassassin setup failed when multiple recipients were concerned. We moved our server a few weeks ago to a new platform (from Slackware to CentOS) and all versions were updated as well. I guess the problems arose there. In a typical postfix+spamassassin setup, you let postfix send all mail through an external filter (with the -o content_filter=<filter> in master.cf). We had that same setup, with the difference, that we filter it all through a homebrew-script that also checks for a procmailrc file in the e-mails’ maildir. This procmailrc can then be programmed on a per-user basis on whether or not to throw the marked spam in a system-wide spambox, a local spambox, or in /dev/null. So far so good.

Until we found out, that e-mails that were to be delivered to more than one e-mail address, only got delivered to one. My first reaction was: “Huh?! I never changed that script and it worked fine on the old server.”. My second reaction was to login to my laptop, on the server, postpone my weekly running round and start hacking on the script. Turns out, the postfix ‘pipe’ filter will give you all the intended recipients as command line arguments. So, you’ll have to cycle over them to deliver all mail. Tricky part is that the e-mail itself (and you don’t want to lose that) is given on stdin. Oh, and we don’t want to scan an e-mail to multiple recipients, multiple times. Oh, and we certainly don’t want any mail that is deferred to the system-wide spamaccount to be scanned twice.

For now, the script is decent, but could use a few tweaks here and there. For example, instead of checking the target recipient to see if we scanned it already, we use the headers.

#!/bin/bash
# BitTouch postfixfilter
# (c) 2010 BitTouch VOF
# This script takes the postfix arguments as input with an email in stdin
#

SENDER=$2
NUMRECIPIENTS=$[$# - 3]
LOGFILE=/var/log/spamassassin

if [ $NUMRECIPIENTS -eq 1 -a "$4" = "<systemwidespamaccount>" ]; then
  SPAMRESULT="already scanned"
  THEMAIL=`cat`
else
  THEMAIL=`/usr/bin/spamc -E`
  if [ "$?" = "1" ]; then
    SPAMRESULT="yes"
  else
    SPAMRESULT="no"
  fi
fi

# The recipients start at argument 4, to the end
for ( (RECPT=4; RECPT<=$#; RECPT++) )
do
  PROCMAILRC=/data/mail/${!RECPT}/procmailrc
  if [ -f $PROCMAILRC ]; then
    echo "From: $SENDER, to: ${!RECPT} (spam: $SPAMRESULT): $PROCMAILRC exists, using that for delivery." >> $LOGFILE
    echo "$THEMAIL" | /usr/bin/procmail $PROCMAILRC
  else
    echo "From: $SENDER, to: ${!RECPT} (spam: $SPAMRESULT): $PROCMAILRC does not exist. Deliver directly." >> $LOGFILE
    echo "$THEMAIL" | /usr/sbin/sendmail.postfix -i "$@"
  fi
done

exit 0
Categories: Geekstuff

1 Comment

Postfix and Spamassassin | Ik doe er niet aan mee · Wednesday March 10th, 2010 at 06:15 AM

[…] post:  Postfix and Spamassassin | Ik doe er niet aan mee Posted in Postfix | Tags: esmtp, from-gertie-apple-com, mta, new-platform, Postfix, pst, […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.