linux:mail_administration

Check for imap spam folders

Ages ago I was running my own mail service and even hosting it for others as well.

After some time I implemented spamassassin and needed to se if everyone had a spam folder. This script will run through all user accounts and check through Maildir of the folder .spam exists. Inside the script you can take action, eg. create necessary folders.

maildirsearch.sh
#!/bin/bash
 
MFOLDER=~/Maildir/
SPAMFOLDER=.spam
 
 
FULLPATH="$MFOLDER$SPAMFOLDER"
 
 
 
 
echo $FULLPATH
 
if  [[ ! -d $MFOLDER ]]; then
        echo "MAILDIR does not exist exist"
                if [[ -d $FULLPATH ]]; then
                echo "spam folder exist"        
        else    
                echo "no spam folder"
        fi
 
 
else
        echo "Maildir exist"
 
fi
 
exit 0

spamassassin and bayessian training

For optimizing spamassassin and each users own filter I had it run through every users mail to train spamassassin for better acuracy.

However I didn't want one bayesian filter used on all email profiles. The below script runs through every users mailbox, as the user, and trains each users own bayesian filter based on the inbox folder and the spam folder in each users mailbox.

The limit for each mailsize is 300kbytes and the script is currently adapted for ISPConfig. However it shouldn't take long to encompas the script for other configurations.

trainspamfilter.sh
#!/bin/bash
 
FILENAME=/tmp/passwdlist
stduser=1000
system=20000
SALEARN=/home/admispconfig/ispconfig/tools/spamassassin/usr/bin/sa-learn
 
MFOLDER=Maildir
SPAMFOLDER=.spam
 
getent passwd > $FILENAME
 
 
for input in $(cut -d: -f1,3,6 $FILENAME)
do   
    userid=$(echo $input|cut -d: -f2)
    username=$(echo $input|cut -d: -f1)
    userdir=$(echo $input|cut -d: -f3)
 
 
       if (("$userid" >= "$stduser" && "$userid" <= "$system" ))
       then     
 
 
        FULLPATH="/$userdir/$MFOLDER/$SPAMFOLDER"
        if [[ ! -d $FULLPATH ]]
        then
                echo "Creating spam trap folder for user: $username at location: $FULLPATH"
                  /bin/su -s /bin/sh $username --command="maildirmake $FULLPATH"
 
        fi
    # Insert here what you want the scrip to do...
                #echo $userid
                echo processing sa-learn for user: $username
                #echo $userdir
                echo Learning ham mails
                su -s /bin/sh $username --command='find ~/Maildir/cur/* -size -300k > ~/.hambox && /home/admispconfig/ispconfig/tools/spamassassin/usr/bin/sa-learn --showdots --ham -f ~/.hambox && rm ~/.hambox'
 
 
 
 
                echo Learning spam mails
             su -s /bin/sh $username --command='find ~/Maildir/.spam/cur/* -size -300k > ~/.spambox && /home/admispconfig/ispconfig/tools/spamassassin/usr/bin/sa-learn --showdots --spam -f ~/.spambox && rm ~/.spambox'
            echo end processing for user        
       fi
 
 
done
 
rm $FILENAME
 
exit 0