• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Spamassassin Training through IMAP

I

inc595

Guest
This script creates ham_learn and spam_learn directories in all mailboxes. You can then use move spam messages to spam_learn directory to train spamassassin just as you would in the Plesk control. Then do the same to non-spam or "ham" mail by moving some mail to the ham_learn.

The first time the script is run it will check to see if the spam and ham directories exist. If they do not then it will create them. If they do exist it will then learn from the mail in them. Ham mail is moved back to the inbox and spam mail is deleted.

Simply copy to your /etc/cron.d directory and edit your crontab
Code:
crontab -e

and add the following to run every 6 hours (adjust to your needs):
Code:
0       */6     *       *       *       sh   /etc/cron.d/psa-sa_learn.sh -n >/dev/null 2>&1


Code:
#!/bin/bash
version="Plesk Spamassassin Learn Version 0.2-a --writen by inc595"
maildir=/var/qmail/mailnames

if [ "$2" != "" ]; then
	domainlist=$2
        echo $domainlist
else
	domainlist=`ls $maildir | grep '[0-9a-zA-z]\{1,\}\.[0-9a-zA-z]\{2,\}' | grep -v auto-whitelist | grep -v .spamassassin`
fi

mode=$1
function learn ()       {

for domain in $domainlist; do
	for mailname in `ls $maildir/$domain | grep -v .spamassassin | grep -v .qmail-default | grep -v user_prefs | grep -v auto-whitelist | grep -v bayes* | grep -v "@mbox.quota" | grep -v *qmail*`; do
   	for type in spam ham ; do
      	mpath=$maildir/$domain/$mailname/Maildir/.$type\_learn
			if [ -d $mpath ]; then
				case $mode in
				"-t")
					echo "Directory exists."
					if [ `ls -l $mpath/cur/ | wc -l` != 1 ]; then
						echo "Directory not empty; learning from $type."
						echo "/usr/local/psa/admin/sbin/spammng --bayes --mailname=$mailname@$domain --$type=$mpath/cur/*"
						if [ $type == spam ] ; then
							echo "rm $mpath/cur/*"
							echo "$mpath/cur/* for $mailname@$domain has been deleted"
						elif [ $type == ham ] ; then
							echo "mv $mpath/cur/*  $maildir/$domain/$mailname/Maildir/cur"
							echo "$mpath/cur/* for $mailname@$domain has been moved to the inbox"
						else
							echo "You should not see me"
						fi
					else
						echo "Directory empty; skipping $type."
					fi
					;;
				"-n")
					if [ `ls -l $mpath/cur/ | wc -l` != 1 ]; then
						/usr/local/psa/admin/sbin/spammng --bayes --mailname=$mailname@$domain --$type=$mpath/cur/*
						if [ $type == spam ] ; then
							rm $mpath/cur/*
						elif [ $type == ham ] ; then
							mv $mpath/cur/*  $maildir/$domain/$mailname/Maildir/cur
						else
							echo "You should not see me"
						fi
					fi
					;;
				esac	
			else
				case $mode in
				"-t")
					echo "Directory does not exist; Creating $mpath"
					;;
				"-n")
					mkdir $mpath
					mkdir $mpath/cur
					mkdir $mpath/new
 					mkdir $mpath/tmp
					chmod -R 700 $mpath
					touch $mpath/maildirfolder
					chmod 600 $mpath/maildirfolder
					echo "owner aceilrstwx" > $mpath/courierimapacl
					chmod 644 $mpath/courierimapacl
					chown -R popuser:popuser $mpath
					;;
				esac
			fi
		done
	done
done
}

case $1 in
"-n" | "-t")
	learn
	;;
"-v" | "-V")
	echo $version
	;;
"--help")
	echo	"Description:"	
	echo	"This script creates ham_learn and spam_learn directories in all mailboxes."
	echo	"If users are using webmail or IMAP they can train spamassassin by moving"
	echo	"mail to ham_learn to for good mail and spam_learn for spam mail."
	echo 	""
   echo  "Usage: psa-sa_learn.sh [OPTION] # All domains"
   echo  "       psa-sa_learn.sh [OPTION] example.com # Single domain"
   echo  "-n             Normal mode     Run live and silent."
   echo  "-t             Test mode       Run without making change"
   echo  "                               and display what would happen."
   echo  "-v                             Display Version information."
	;;
*)
	echo "psa-sa_learn.sh: missing file argument"
	echo "Try 'psa-sa_learn.sh --help' for more information."
	;;
 esac

Hope this helps other out. I found that thunderbird is pretty good at catching spam. I set it to move the spam it finds to spam_learn in IMAP and spamassassin is becoming more accurate.
 
Plesk 8.1

Looks great, but does it work for Plesk 8.1 as well?
 
inc595, will the learned spam be available for all mailboxes or just for that individual?
 
I haven't tested it on 8.1 so i'm not sure. Give it a go and let me know. :D

Due to the way Plesk has their spamassassin setup, everyone trains their own bayesian filter. There is a way to have everyone use one master bayes filter but what you may not consider spam someone else might so I think this maybe one reason Plesk has it this way.
 
Back
Top