[Hampshire] Bash script for forwarding spam from an mbox

Top Page

Reply to this message
Author: Graeme Hilton
Date:  
To: Hampshire LUG Discussion List
Subject: [Hampshire] Bash script for forwarding spam from an mbox
Hi all,

I'm part way through revamping my email setup and am now relying more
heavily on my ISPs spam/virus detection. Their spam detection uses
DSPAM and tags each spam with [-SPAM-] in the subject as well as several
header fields.

My own setup runs mail through SpamAssassin and another virus check. In
order to help my ISP get better spam detection I want to forward back
to them anything that they missed and I detected as spam. They prefer
emails forwarded inline with all headers intact.

Currently I manually check the Junk folder once a day and move all
spammy mails to JunkSpam, and non-spammy mails to the JunkNotSpam.
Overnight I have a cron script that feeds the mails to SpamAssassin[1].
I'd also like this script to detect which mails have been missed by the
ISP (don't have [-SPAM-] in the subject or have DSPAM: INNOCENT as a
header), and then forward them individually, inline to the ISPs training
address.

Does anyone have any pointers to a shell scripting tutorial that might
be able to help me split out the emails from an mbox? Googling for
various combinations of "shell script mail forward mbox" doesn't give me
much to work with.

Thanks,


Graeme


[1] Shell Script
#!/bin/sh
# Script to train SpamAssassin.
# usage spamtrain <username> <root mail folder>
# e.g. spamtrain.sh john Junk

THISUSER=$1
MAILDIR=/home/$THISUSER/mail/$2
SPAM=$MAILDIR/JunkSpam
HAM=$MAILDIR/JunkNotSpam

# nice 19 to avoid CPU hogging
# learn the SPAM first, don't sync the database yet
/bin/nice -n 19 /usr/bin/sa-learn --mbox --spam --no-sync $SPAM
# Remove the Spam and then replace the file and change owner
/bin/rm -rdf $SPAM && /bin/touch $SPAM \
&& /bin/chown $THISUSER:$THISUSER $SPAM

# Now do the same for the ham
/bin/nice -n 19 /usr/bin/sa-learn --mbox --ham --no-sync $HAM
/bin/rm -rdf $HAM && /bin/touch $HAM \
&& /bin/chown $THISUSER:$THISUSER $HAM


# Now we can sync the database
/usr/bin/sa-learn --sync