Train spamassassin from an IMAP mailbox
cd /opt
git clone https://github.com/magenbrot/sa_learn_from_imap.git
Debian OS:
Optional: Setup virtualenv, it should also run without that.
sudo apt install build-essential curl python3-venv
cd sa_learn_from_imap
python3 -m venv env/python3
source env/python3/bin/activate
Hint: mymailbox.org and the password are just examples.
First up you'll need an IMAP mailbox where you and your customers redirect SPAM and HAM to. I've a mailbox called [email protected]. Since I use postfix (I don't know what other MTAs do), I can use a + in my recipient address (see recipient_delimiter documentation of Postfix). So I'm redirecting all spam to [email protected] and all ham to [email protected]. I'm using the thunderbird plugin "Mail Redirect" for this.
Furthermore I have a sieve-script running on my server, which will move the incoming mails by the recipient to the correct folder (see the provided example).
Now about training the SpamAssassin: The script has 3 required, positional arguments. The IMAP-host, -user and the -password. By default it assumes that your SPAM mails are sorted in a folder called 'spam' and your HAM mails are filed in 'ham'.
This can be changed with the parameters --spam-folder
and --ham-folder
. It needs a working directory, this
is /tmp/sa-learn
by default and can be changed with the --directory
parameter. Finally you can specify the
sa-learn script it should use with --sa-learn
. This defaults to /usr/bin/sa-learn
.
Please call the script with the user you're running SpamAssassin with. In my case it's root
(I'm using the
Proxmox Mail Gateway).
Example (run with python3, either directly or in the venv from above):
./sa_learn_from_imap.py mail.mymails.org '[email protected]' 'password123$'
A cronjob could look like this, put this in /etc/cron.d/sa-learn-ham-spam
:
#MAILTO="[email protected]"
MAILTO=""
# run with bash, as sh doesn't support 'source'
SHELL=/bin/bash
15 * * * * root source /opt/sa_learn_from_imap/env/python3/bin/activate && \\
/opt/sa_learn_from_imap/sa_learn_from_imap.py mail.mymails.org '[email protected]' 'password123$'
more error handling...