Using SpamAssassin on Postfix
Judge spam mail and move to the Jank folder.
When the mail is received, it is passed to procmail and can be processed by a custom script.
apt install procmail spamassassin spamc -y
main.cf
vi /etc/postfix/main.cf
virtual_transport = procmail
procmailrc
If it is judged as spam, move to the Junk folder.
vi /etc/procmailrc
SHELL=/bin/bash
PATH=/usr/bin:/bin
DROPPRIVS=yes
USERDIR=/home/vmail/$DOMAIN/$USER
MAILDIR=$USERDIR/Maildir
DEFAULT=$MAILDIR/
LOGFILE=/home/vmail/$DOMAIN/$USER/.procmail.log
#VERBOSE=ON
:0fw: spamassassin.lock
*!^X-Spam-Status.*
| /usr/bin/spamc
:0
*^X-Spam-Status: YES
$MAILDIR/Junk/
Add spam prefix to email subject.
vi /etc/spamassassin/local.cf
rewrite_header Subject *****SPAM*****
master.cf
vi /etc/postfix/master.cf
# Add tail
spamassassin unix - n n - - pipe
flags=R user=spamd argv=/usr/bin/spamc -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
procmail unix - n n - - pipe
flags=R user=vmail argv=/usr/bin/procmail -t -m USER=${user} DOMAIN=${nexthop} /etc/procmailrc
Test for spamassassin
vi /etc/hosts
127.0.0.1 mail.mydomain.com mail.mydomain2.com
echo "XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X" | \
mail -s "test1" -r "ast@mydomain.com" "test2@mydomain.com"
Relearn from the misjudgment result.
Relearn emails in the Junk folder as SPAM and in the cur folder as non-SPAM.
vi /etc/cron.d/spamassassin
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
13 3 * * * root /spamassassin-learn.sh
spamassassin-learn.sh
vi /spamassassin-learn.sh
#!/bin/sh
# Let spamassassin relearn the emails in the spam folder.
/usr/bin/sa-learn --spam /home/vmail/*/*/Maildir/Junk/cur 2>&1 >> /var/log/spamassassin-lean.log
# Let spamassassin relearn the emails in the cur folder. /usr/bin/sa-learn --ham /home/vmail///Maildir/cur 2>&1 >> /var/log/spamassassin-lean.log