Scanning User Mailboxes for Spam and Training the Bayes Filter

The script below requires a mail transfer agent such as Dovcot that is set up to place mailboxes under the user's home directories in /home. Upon referencing this script from crontab, it will periodically scan all user mailboxes for the Junk folder and then automatically train the Spamassassin bayes filter.

sa-learn-junk-folders.sh
#!/bin/sh
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3      ##
###########################################################################
# A script to make amavis / spamassassin learn spam mail through bayes    #
# from all the mailboxes present on the system.                           #
#                                                                         #
# For this script to work, a mail-server must be configured by enabling   #
# IMAP mailboxes stored in the user's home directory, users should have a #
# virtual mailbox "Junk" created into which they can place spam E-Mails.  #
# After that, you can configure the script and reference it in crontab.   #
###########################################################################
 
###########################################################################
#                             CONFIGURATION                               #
###########################################################################
 
# The name of the Junk folder - under Dovecot, this is commonly "Junk"
JUNK_FOLDER="Junk"
# The path to the spamassassin database.
SPAMASSASSIN_DATABASE="/var/lib/amavis/.spamassassin"
# The mailbox type can be either: Maildir, mail or sdbox
MAILBOX_TYPE="sdbox"
 
###########################################################################
#                                INTERNALS                                #
###########################################################################
 
# Train spam from junk folder.
for i in `find /home/*/$MAILBOX_TYPE/ -type d -name "$JUNK_FOLDER"`; do
    HOME_FOLDER=`echo $i | awk -F"/" '{ print $3 }'`
    sa-learn --use-ignores \
        --dbpath "$SPAMASSASSIN_DATABASE" \
        -p "/home/$HOME_FOLDER/.spamassassin/user_prefs" \
        --spam "$i" > /dev/null 2>&1
done
 
# Sync databases
sa-learn --dbpath "$SPAMASSASSIN_DATABASE" --sync > /dev/null 2>&1

fuss/spamassassin.txt ยท Last modified: 2017/02/22 18:30 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.