The idea came from my play/toy MailServer (Postfix, Dovecut & MailScanner) on my Synology DS209+II NAS. There it is basically a no-brainer to set up as everything works off of MailDirs.NB! wrote:How to obtain SpamAssassin and the installation and configuration of SpamAssassin is NOT described here! Search elsewhere on this forum to obtain this information.
So how to do ... on a hMailServer 5.4.2-B1964 system ...
1 -- Build a script using the COM api to find and extract relevant emails and what could be more natural than to assume INBOX is good and SPAM is bad. Also, if HAM end up in SPAM (or visa versa) you move it to the respective folder (INBOX or SPAM) and at the next scheduled run, the email will be classified differently. I execute this script using Windows Schedule at 04:00 in the morning when everyone is (supposed to be) at sleep.
2 -- A global rule in hMailServer to move emails tagged as SPAM into a SPAM folder. Setting this rule as a global rule will ensure that ALL users of hMailServer are covered. If the SPAM folder do not exist, it will be created by hMailServer automatically.
Rule name: sa-learn
Code: Select all
Criteria -> Custom header field -> X-hMailServer-Spam = YES
Action -> Move to IMAP folder -> IMAP folder = SPAM
Rule name: sa-learn (BigBrother version) :: Use AND
Code: Select all
Criteria -> Custom header field -> X-hMailServer-Spam = YES
Criteria -> Custom header field -> X-hMailServer-LoopCount < 1
Action -> Move to IMAP folder -> IMAP folder = SPAM
Action -> Forward email -> To = spam@my-domain.tld
3 -- Now that we have both good and bad emails defined, we need to pull them off of the server. For that I have choosen VBScript to interact with the hMailServer COM API.
sa-learn.vbsFunctional Description: wrote: The VBScript (sa-learn.vbs) will work with ONE domain at present as I only have one domain. Using the COM API it will locate and process all account addresses for that domain - except those addresses listed in the exception list.
The script will do two passes, one for INBOX and one for SPAM, and generate two .cmd files (HAMCopy.cmd & SPAMCopy.cmd) to be run by the script.
During the two passes, the number of messages in the respective folders are checked and only the last (max.) 20 messages are processed. This procedure is based on the assumption that the COM API will return data sorted by table ID and from examining the database it appears that hMailServer simply adds new message ID's to the table in favor of reusing deleted message ID's.
HAMCopy.cmd and SPAMCopy.cmd simply copies the selected .eml files to a HAM or SPAM directory.
A third .cmd file (sa-learn.cmd) is also executed by the script and this .cmd file contains the commands to execute sa-learn --spam, sa-learn --ham, sa-learn --sync and sa-learn --backup as it is customary on Unix type systems.
On a Dual-Core 3GHz, 4GB RAM, SATA, System w/ Windows Server 2003R2 it took almost 20 minutes to process ~4.200 HAM and ~5.600 SPAM emails.
Code: Select all
Option Explicit
'
' Version 0.1.0 09-08-2014, Soren Rathje - Initial version.
'
Dim hmAdmin, hmPassword, hmDomain, hmSPAMFolder, hmSPAMDir, hmHAMFolder, hmHAMDir, hmExcludeAddress
Dim i, j, s, objApp, objDomain, objAccount, objIMAPFolder, objMessage
Dim fsoSPAM, fsoHAM, fsoSALearn, objFSO, objSPAM, objHAM, objShell
'
' Configuration parameters - BEGIN
'
hmAdmin = "Administrator" ' hMailServer Administrator user
hmPassword = "********" ' hMailServer Administrator password
hmDomain = "my-domain.tld" ' Domain name
hmSPAMFolder = "SPAM" ' SPAM IMAP folder
hmSPAMDir = "C:\hMailServer\SPAM" ' You need to create this directory!
hmHAMFolder = "INBOX" ' HAM IMAP Folder
hmHAMDir = "C:\hMailServer\HAM" ' You need to create this directory!
hmExcludeAddress = "spam@my-domain.tld, surveillance@my-domain.tld"
fsoSPAM = "C:\hMailServer\Events\SPAMCopy.cmd"
fsoHAM = "C:\hMailServer\Events\HAMCopy.cmd"
fsoSALearn = "C:\hMailServer\Events\sa-learn.cmd"
'
' Configuration parameters - END
'
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objApp = CreateObject("hMailServer.Application")
Call objApp.Authenticate(hmAdmin, hmPassword)
Set objDomain = objApp.Domains.ItemByName(hmDomain)
'
' Find SPAM messages
'
Set objSPAM = objFSO.CreateTextFile(fsoSPAM,True)
For i = 0 to objDomain.Accounts.Count -1
Set objAccount = objDomain.Accounts.Item(i)
' DO NOT process excluded and non-active accounts.
If (NOT InStr(hmExcludeAddress, objAccount.Address)) * objAccount.Active Then
Set objIMAPFolder = objAccount.IMAPFolders.ItemByName(hmSPAMFolder)
' If no messages - skip
If objIMAPFolder.Messages.Count > 0 Then
s = 0
If objIMAPFolder.Messages.Count - 20 > 0 Then s = objIMAPFolder.Messages.Count - 20
For j = s to objIMAPFolder.Messages.Count -1
Set objMessage = objIMAPFolder.Messages.Item(j)
objSPAM.Write "COPY " & objMessage.FileName & " " & hmSPAMDir & " /Y" & vbCrLf
Next
End If
End If
Next
objSPAM.Close
'
' Find HAM messages
'
Set objHAM = objFSO.CreateTextFile(fsoHAM,True)
For i = 0 to objDomain.Accounts.Count -1
Set objAccount = objDomain.Accounts.Item(i)
' DO NOT process excluded and non-active accounts.
If (NOT InStr(hmExcludeAddress, objAccount.Address)) * objAccount.Active Then
Set objIMAPFolder = objAccount.IMAPFolders.ItemByName(hmHAMFolder)
' If no messages - skip
If objIMAPFolder.Messages.Count > 0 Then
s = 0
If objIMAPFolder.Messages.Count - 20 > 0 Then s = objIMAPFolder.Messages.Count - 20
For j = s to objIMAPFolder.Messages.Count -1
Set objMessage = objIMAPFolder.Messages.Item(j)
objHAM.Write "COPY " & objMessage.FileName & " " & hmHAMDir & " /Y" & vbCrLf
Next
End If
End If
Next
objHAM.Close
'
' Execute file copy and sa-learn.exe - sequentially - no StdOut.
'
objShell.Run fsoSPAM, 0, true
objShell.Run fsoHAM, 0, true
objShell.Run fsoSALearn, 0, true
Code: Select all
C:\SpamAssassin\sa-learn.exe --siteconfigpath="C:\SpamAssassin\etc\spamassassin" --dbpath "C:\Documents and Settings\Default User\.spamassassin\bayes" --spam "C:\hMailServer\SPAM\*.eml"
C:\SpamAssassin\sa-learn.exe --siteconfigpath="C:\SpamAssassin\etc\spamassassin" --dbpath "C:\Documents and Settings\Default User\.spamassassin\bayes" --ham "C:\hMailServer\HAM\*.eml"
C:\SpamAssassin\sa-learn.exe --siteconfigpath="C:\SpamAssassin\etc\spamassassin" --dbpath "C:\Documents and Settings\Default User\.spamassassin\bayes" --sync
C:\SpamAssassin\sa-learn.exe --siteconfigpath="C:\SpamAssassin\etc\spamassassin" --dbpath "C:\Documents and Settings\Default User\.spamassassin\bayes" --backup > "C:\Documents and Settings\Default User\.spamassassin\bayes_backup"
REM DELETE C:\hMailServer\SPAM\*.eml /Q
REM DELETE C:\hMailServer\HAM\*.eml /Q
Disclaimer: wrote: I take no responsibility for what you may or may not do with the above script/shell script. It works for me - it may not work for you. I DO NOT GUARANTEE THIS CODE TO BE BUG-FREE! USE AT YOUR OWN RISK! WHATEVER YOU DO - IT'S NOT MY FAULT!
AND remember; Real men do NOT backup - but they CRY a lot!
Please feel free to adopt and modify.