Automatically Delete Emails Marked SPAM by Spamassassin

Great, so you have another service running on your Linux box. It’s main purpose is to intercept an email, run rules against it, and either mark it as spam or not. Either way, it sends the email through and you still have to deal with the message on the email client. From the email client, you could make the necessary changes to handle messages marked with some predetermined marker as [SPAM] or ***[SPAM]*** or the like. After too much research, there doesn’t seem to be any native support to Spamassassin to automatically delete the spam that it identifies. That may be by design to protect the end user from themselves by not deleting false-positives. With that said, I have decided to look at this from another perspective, a bash script to delete those unwanted files.

Basically, a Spamassassin instance is up and running and is setup to mark the offending email messages as [SPAM]. Go to the home directory where the emails are located. Maybe /home/user1 /home/user2 etc. While there check to see if there are any messages that contain the string Subject: [SPAM] and get a count of the number of messages that contain that string.

find -type f -exec grep 'Subject\: \[SPAM\]' '{}' \; | wc -l

Now, maybe you want to know what the message subjects may look like.
find -type f -exec grep 'Subject\: \[SPAM\]' '{}' \; -print

This could result with something like…

Subject: [SPAM] Hello
Subject: [SPAM] NEW JOBS

If you think you are comfortable with those results, here is the command to delete them.

find -type f -exec grep 'Subject\: \[SPAM\]' '{}' \; -delete

If you want to get creative maybe run it as a cron job. You see where this is going. Be careful. Use at your own risk. There may need to be some additional tweaking to the command.

Taking this a step further, edit the /etc/sysconfig/spamass-milter configuration file by appending the following code to basically move the files with ie. [SPAM] in the subject line to /dev/null. This could be dangerous if you really care about all of your emails. When getting 100s of emails daily, you may get to a point where it doesn’t matter if a few false positives should disappear into oblivion.

EXTRA_FLAGS="-r 15 -b devnull"

Restart the services (I’m not completely convinced all of these services need to be restarted).

service spamassassin restart
service spamass-milter restart
service dovecot restart

Even though Spamassassin by default does a pretty good job in deleting those unwanted spam messages, your situation may vary where additional messages continue to haunt your mailbox, your very existence. Well there is relief? Train Spamassassin to mark additional messages as [SPAM]. While there are many resources to be found online on this subject, I’ll touch on this for just a moment to at least acquaint with the commands.

Basically in this example below, sa-learn will add to the Spamassassin database whatever content to be found in the folder .Spam. The .Spam folder is a hidden one. This folder is where you could move your unwanted messages that are not either already identified as [SPAM] by Spamassassin or regular trash.

sa-learn --no-sync --spam /home/user1/Maildir/.Spam/{cur,new}
sa-learn --sync
sa-learn --dump magic
service spamassassin restart
service spamass-milter restart

Again, use this to your own devise.