Teaching Mailcow how to deal with Ham/Spam
The good must be put in the dish, the bad you may eat if you wish.
Cinderella
Mailcow is a groupware solutions, that is mainly used for email messaging. With Mailcow, you can setup your own Docker-based Mail-Server + Addons.
Mailcow uses rspamd to filter out Spam Messages.
However, after some time, there is a need to fine-tune the Spam (Spam Messages)/Ham (“good” Messages) filtering.
There is a documented method to learn Spam from existing emails within a directory, but especially for non-technical users, that might be hard to understand.
So I updated this method a little bit:
- every user hast two folders
rspamd/spam
andrspamd/ham
in their home directory. - Every user can new just drop new spam messages into the
spam
and false spam messages into theham
folder. - A cron Jobs runs every hour to parse the user directories for new files and updates the rspamd behaviour.
The script for SPAM learning looks like this (assumed that mailcow is installed in /opt/mailcow-dockerized
):
#!/bin/bash
cd /opt/mailcow-dockerized
for u in $(ls /home); do
""mv /home/$u/rspamd/spam/* ./data/rspamd/spam/""
done
for file in ""./data/rspamd/spam/*""; do
docker exec -i $(docker-compose ps -q rspamd-mailcow) rspamc learn_spam < "$file"
done
""rm -Rf ./data/rspamd/spam/*""
There is also a similar script for HAM learning:
#!/bin/bash
cd /opt/mailcow-dockerized
for u in $(ls /home); do
""mv /home/$u/rspamd/ham/* ./data/rspamd/ham/""
done
for file in ""./data/rspamd/ham/*""; do
docker exec -i $(docker-compose ps -q rspamd-mailcow) rspamc learn_ham < "$file"
done
""rm -Rf ./data/rspamd/ham/*""
Both scripts will produce some output, so a good way of running it via cron, is to pipe the output into a log file.