Automatically Train Bayes In Spam Assassin

Discussion for developers using MailEnable.
Post Reply
Posts: 2
Joined: Thu Jan 07, 2016 3:13 pm

Automatically Train Bayes In Spam Assassin

Post by crgarrett93 » Sun Feb 28, 2016 5:08 am

Good Evening Everyone,

This will be my first contribution to the Development Community so go easy on me.


I have written a script that can be executed on a Task Scheduler or whatever your choosing may be that will pragmatically go through your entire Mail Server, all possibly existing post offices, and all possibly existing mailboxes, and train your Bayesian Dictionary with Spam and Ham based on the folders the emails are stored in.

As of now all it does is look for key words such as "Junk" and "Deleted" in the folder name and anything that doesn't match these two attributes is automatically trained as Ham.

Since we have no way of distinguishing "Junk" email from "Regular" email in the deleted folder I choose not to take the risk and avoid it entirely.


Code: Select all

@echo off
SET FOLDER=C:\Program Files (x86)\Mail Enable\Postoffices
FOR /D %%I in ("%FOLDER%\*") DO CALL :PostOffice %%I
goto END

FOR /D %%I IN ("%*\MAILROOT\*") DO CALL :InboxParse %%I
goto END

FOR /D %%I IN ("%*\*") DO CALL :Logic %%I
goto END

echo "%*"|findstr /C:"Junk" >nul 2>&1
if not errorlevel 1 ( 
   set TRAINTYPE=spam
) else (
   echo "%*"|findstr /C:"Delete" >nul 2>&1
   if not errorlevel 1 (
	 goto END
   ) else (
      set TRAINTYPE=ham

echo Checking if SpamAssassin daemon (spamd) is available on local host...
spamc -K > nul
if %errorlevel%==0 (
	echo Spamd is available. Using spamc for training.
	set METHOD=spamc
) else (
	echo Spamd is not available. Using sa-learn for training.
	set METHOD=sa-learn

if %METHOD% == spamc (
	goto spamc
) else (
	goto sa-learn

goto END

echo "%*"
for %%X in ("%*\*") do spamc -L %TRAINTYPE% < "%%X"
if %errorlevel%==74 (
	echo Learning is not allowed by spamd, please start spamd with --allow-tell switch.
	goto end
goto end

echo "%*"
for %%X in ("%*\*") do sa-learn --%TRAINTYPE% < "%%X"
goto end

PasteBin Link:


You must be running JAM Software's version of Spam Assassin and it can be the free or paid version.

I personally use the Free Version with a daemon.

If you have any questions or concerns feel free to leave a response.

If you have an urgent request regarding this and want more information, in the event I don't see a response in my inbox, my email is cody at garrett dot ms

Cheers guys!

Posts: 183
Joined: Sat Aug 07, 2010 11:08 am

Re: Automatically Train Bayes In Spam Assassin

Post by aahq » Wed Apr 06, 2016 1:27 pm

Hey. That looks pretty cool in concept.

If I am running an established server thats been around for 8 years odd I probably should try to clean up older spam in peoples inboxes before running this.

I am running a sourceforge version of SA from a few years ago so otherwise I should be OK.

Any other gotchas here you can think of?


Posts: 1
Joined: Wed Feb 22, 2017 6:28 am

Re: Automatically Train Bayes In Spam Assassin

Post by soumyaahuja » Wed Feb 22, 2017 6:47 am

you are trying good for spam mails in mail box. your concept is nice.

Post Reply