I want to have SpamAssassin learn outgoing mails as ham so the recipients automatically get added to the whitelist. But I've got trouble setting that up.
Ideally, I just want to have Postfix pass the email to a script and continue processing normally.
I already came up with a solution with a content_filter
, but it doesn't seem to work reliably: especially when I send to several people some mails don't get delivered. In the logs I can see that for some recipients the script is called, but even then only for some of those the email is actually sent.
So if possible I would like to avoid using content_filter
. Is there are another way? If not, what may be wrong with my setup?
Here's what I got right now:
master.cf:
submission inet n - n - - smtpd
-o smtpd_client_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
-o content_filter=spamassassin-outgoing
spamassassin-outgoing unix - n n - - pipe
flags=u user=spamd:spamd argv=/usr/local/bin/learnoutgoing.sh ${sasl_username} ${sender} ${recipient}
learnoutgoing.sh:
#!/bin/bash
if [ -z "$3" ] ; then
echo "$0: <user> <sender> <recipient>"
exit 1
fi
/usr/bin/logger Learn outgoing: ''"$1"'' ''"$2"'' ''"$3"''
# Save the stdin into a temporary file, we need to feed it into two
# different programs.
TMPFILE=`mktemp /tmp/outgoing.XXXXXXXXXX`
cat >$TMPFILE
# The user passed in $1 is in format 'user@domain' but we just want the user
USER=`sed 's/@.*//' <<<$1`
# Learn the mail as ham.
/usr/bin/spamc -u "$USER" --learntype ham <$TMPFILE &>/dev/null || true
# Send it.
/usr/sbin/sendmail -oi -f "$2" "$3" <$TMPFILE
# Save the return value of sendmail
RETCODE=$?
# Cleanup and exit
rm -f $TMPFILE
exit $RETCODE