Use this forum if you have installed hMailServer and want to ask a question related to a production release of hMailServer. Before posting, please read the troubleshooting guide. A large part of all reported issues are already described in detail here.

although this thread is full of valuable information, it is very hard for a beginner to set up Spamassassin properly.

Espeacially setting up the event handler script in hMailServer can become very annoying if you are looking for certain features spread over two or more code examples.

Therefore I reviewed all the code snippets from this thread (and a few others) and wrote a (hopefully) well documented event handler script that is highly configurable to fit at least the needs of low volume users. It does have the following features:

Configurable Bayesian learning

Configurable for scanning outgoing and/or incoming mails

Auto discarding of mails above configurable spam score

Freely configurable for using spamc, spamassassin, sa-learn, etc.

Scans incoming mails over SMTP or POP

Configurable logging to hMailServers hmailserver_events.log

The script assumes you have configured your Spamassassin by following the six steps outlined by john25uk in the first message of this thread:

Start by downloading and saving no's 2, 4, and 5 - you will need them for the spamassassin install tutorial (6)

Follow the instructions on link 6. HINTS: type the names of the perl modules exactly as shown, including lowercase and capitals. Ignore from Part X onwards for now, but download the Windows GUI in Part XI, its good. Install everything to the default directories also.

7. After that follow the installation instructions included in the script.

I hope you'll like it... Please reply here in this forum to any issues with the script or feature recommendations. If you feel the need to edit this script, make your changes configurable and share the result to the hMailServer forum.

Thanks goes to all contributors to this thread and this forum. Without you, this script would not have been possible.

Update 2007-08-07: Fixed bug where SAHI_auto_discard_score wasn't causing the mail to be deleted.

'-----------------------------------------------------------------------------'Script:' Spamassassin hMailServer Integration (SAHI)''Synopsis:' A configurable script to integrate Spamassassin into hMailServer''Script version:' 1.1''Release date:' 2007-08-07''Features:' - Configurable Bayesian learning' - Configurable for scanning outgoing and/or incoming mails' - Auto discarding of mails above configurable spam score' - Freely configurable for using spamc, spamassassin, sa-learn, etc.' - Scans incoming mails over SMTP or POP' - Configurable logging to hMailServers hmailserver_events.log''Limitations:' - Not meant for use in high volume environments' - No builtin batch support for Bayesian learning' - No redirection of spam into different IMAP folder (this must be' configured in hMailServer)' - No general userprefs support for scanning/learning' - No general support for checksum services such as dcc, pyzor, or razor''Requirements:' - Requires hMailServer, Perl and Spamassassin' to be successfully installed and the pathes to Perl and' Spamassassin to be assigned in your PATH variable;' In the following Spamassassin stands as a synonym for' either spamc/spamd, spamassassin or some other' spamassassin derivate, you can configure your prefered' Spamassassin flavour in the config part below;' - Requires .NET to be installed - otherwise' command output redirection will not work and render this' script useless; tested with .NET 2.0 but other versions may' work also' - Requires hMailServer 4.4''Installation:' 1. Append this code to the end of your EventHandlers.vbs' file of hMailServer' 2. Configure the script with the variables below' 3. Change the OnAcceptMessage handler as follows (obviously' without the comment quotes):'' Sub OnDeliverMessage(oMessage)' SAHI_processMessage oMessage' End Sub'' 4. Reload your hMailServer scripts afterwards or restart hMailServer''Tested with:' hMailServer 4.4 (B270) (polling external POP accounts)' Perl 5.8.8 (820)' Spamassassin 3.2.2' Windows XP (latest patch level as at time of writing; .NET framework 2.0)''History:' 2007-08-05 - 1.0 - Soeren Weber:' Initial version inspired by the hmailserver.com forum' discussion "HOWTO: Use SpamAssassin with hMailServer"'' 2007-08-07 - 1.1 - Soeren Weber:' Fixed bugs were SAHI_auto_discard_score wasn't causing the mail to be ' deleted''-----------------------------------------------------------------------------'Start configure the script here

Const SAHI_domain_received_header = "from myserver ([127.0.0.1]) by example.com with hMailServer ;"'Possible values:' Any string that uniquely identifies an outgoing mail'This string must uniquely identify any outgoing mail that is sent'from your local domain to an arbitrary recipient by its "Received"'mail header

Const SAHI_add_spam_check_status_header = 0'Possible values:' 0 - Don't add an additional header field' 1 - Add additional mail header field'In case this value is set to 1, an additional X-Spam-Check-Status will be'added to the mail, signaling the result of this script; Possible header'values are:' Skipped - This will be set if the spam check was skipped due to' having set SAHI_scan_outgoing_mails or SAHI_scan_incoming_mails' respectivly to 1' Checked - This is set if the spam check could be successfully' applied' Error - This is set in case the spam check was aborted due to errors' during processing of the mail by this script'NOTE: Due to how IMAP works, this header field as also'Spamassassins own additional header fields will be added to all'incoming mails unconditionally. For outgoing mails, the headers will be'added for the mail that was sent from hMailServer to the destination server'but not to the copy that may be stored in your "Sent" folder;'It is recommended to better set this value to 0 in case you have set'SAHI_use_learn_mailboxes to 1. This may negativly influence the results'of the Bayesian filter rules if you don't add this header field to your'Spamassasin configuration using the bayes_ignore_header parameter;'Besides the above mentioned keyword values for this header field, some'further information may be appended to the field

Const SAHI_auto_discard_score = 12'Possible values:' 0 - No auto discarding will be used' 1 .. n - Integer spam score needed to cause the mail to be discarded'If the spam check results are above this threshold value, the mail will'automatically discarded by this script; Usually you should set this to'the same value as Spamassassins config item bayes_auto_learn_threshold_spam;'If you are uncertain about the reliability of your Spamassassin rules'you should avoid discarding mails, so that they are still be delivered'to your recipients; If you don't want to discard the spam but move it'to a different IMAP folder, use hMailServer rules; See documentation and'forum of hMailServer for how this works

Const SAHI_alternate_scan_command_line = ""'Possible values:' Command line used to start Spamassassin'Alternate command line that will be used to check emails with'Spamassassin in case the SAHI_default_scan_command_line failed with an error;'You can leave this empty if you don't need alternate command processing;'In- and outfile parameter will be added by this script, so avoid setting'them; This parameter is useful eg. if you are using spamc for the default'and running into an error. In this case you may run Spamassassin directly

Const SAHI_use_learn_mailboxes = 1'Possible values:' 0 - Don't use ham/spam/forget mailboxes' 1 - Use ham/spam/forget mailboxes'If set to 1 this script will cause any mail that will be sent internally to'the SAHI_learn_ham_mailbox, SAHI_learn_spam_mailbox or'SAHI_learn_forget_mailbox to be processed by your Bayesian learning tool;'There may be differnt ways how to feed your tool, therefore this option is'set to 0 by default; If you use this feature, the mail that should be learned'is expected to be attached to a forwarding mail; In case you are using'SquirrelMail, I recommend using the spam_buttons" plugin

Const SAHI_learn_ham_mailbox = "report_ham@example.com"'Possible values:' Fully quallified local email address'You only need to set this if you have SAHI_use_learn_mailboxes set to 1;'Replace at least the <example.com> with the domain hMailServer is'running on; You are free to change the mailbox name aswell; There must'be a mailbox configured in hMailServer server with this name, because'otherwise mails sent to this address will not be accepted by hMailServer.'The hMailServer mailbox will not fill with mails if SAHI_use_learn_mailboxes'is set to 1

Const SAHI_learn_spam_mailbox = "report_spam@example.com"'Possible values:' Fully quallified local email address'You only need to set this if you have SAHI_use_learn_mailboxes set to 1;'Replace at least the <example.com> with the domain hMailServer is'running on; You are free to change the mailbox name aswell; There must'be a mailbox configured in hMailServer server with this name, because'otherwise mails sent to this address will not be accepted by hMailServer.'The hMailServer mailbox will not fill with mails if SAHI_use_learn_mailboxes'is set to 1

Const SAHI_learn_forget_mailbox = "report_purgative@example.com"'Possible values:' Fully quallified local email address'You only need to set this if you have SAHI_use_learn_mailboxes set to 1;'Replace at least the <example.com> with the domain hMailServer is'running on; You are free to change the mailbox name aswell; There must'be a mailbox configured in hMailServer server with this name, because'otherwise mails sent to this address will not be accepted by hMailServer.'The hMailServer mailbox will not fill with mails if SAHI_use_learn_mailboxes'is set to 1

Const SAHI_learn_command_line = "%comspec% /c sa-learn --"'Possible values:' Command line used to start sa-learn'Command line that will be used to run the Bayesian learning tool in case'SAHI_use_learn_mailboxes is set to 1; The learn type (ham, spam,'forget) will be appended at the end of this command during runtime; Due to'the way how hMailServer works, it is 'not wise to use userprefs for the'Bayesian learning tool

Const SAHI_trace_level = 4'Possible values:' One of the below defined SAHI trace levels'You can adjust the amount of trace that is written to the trace file'hmailserver_events.log by setting this value; Eg. if you set the'variable to SAHI_trace_warning, all traces are written that are either'emergency, alert, critical, error or warning but no traces for notice,'information and debug; To turn tracing off, set this variable to 0;'For production you should set this value a lesser equal of 4'(SAHI_trace_error) or 5 (SAHI_trace_warning) especially if you are'running high volume

'Stop configure the script here; No more changes should be necessary after'this line; If you feel the need to edit this script, make your changes'configurable and share the result to the hMailServer forum'-----------------------------------------------------------------------------

I think it has to do with the '\' in the paths. when I drop to a command prompt and execute this:
%comspec% /c sa-learn --spam "E:\Program Files\hMailServer\Data\_BABA7A77-D6CB-44FB-9105-8674D7F35BF6_.eml.dat"

To be honest, this was my first VBScript program but I am seriously coding C/C++ for years and also know some other languages very well - like JScript, PHP, bash shell, etc. Usually in all of these languages you are in need of doubling the backslashes if you want to print out a backslash in a hardcoded string like:

Therefore once you have a properly escaped string inside a variable there is no need to escape them any further for output. In the scripts case this should be handled properly - in case VBScript behaves like the other languages I know...

So it seems like a different issue to me. Question: Does only sa-learn fail? What about the call of the spam scanning command? If you are right with your assumption, this must also fail.

The only difference I can see is the space in your path. But this should be handled properly by quoting the file parameter... Strange... If the space in the path is the problem here, this should also fail in case of spam scanning.

Anyway: Do you fullfil the scripts requirements as mentioned at the beginning of the script in the documention part? Which version of Spamassassin are you using? Is this downloaded directly from the Spamassassins homepage or is it some packaged distribution?

just before the call for spamc, I see that the file with the mail is fine.

The other strange thing is, that it's lightning fast. Normally that's a goot thing, but performing the command on a command line with d:\mail_file.txt, needs a second or two. also, when I call the main sub with a quick and dirty:

But when there is a new mail comming in then I don't get this email in my mailbox. When I check the hmailserver\data directory then I see that the script is running, I see a .eml file comming in and then changing to .eml.tmp and then again to .eml and then the file disapears. But when I look into the log-file af hmailserver I get (I have changed the domain mailbox by xxxxxx.com:

Are you looking in the right log file? It's NOT "hmailserver_YYYY-MM-DD.log" but "hmailserver_events.log". The script was executed when the mails in you mailbox do contain additional Spamassassin headers like X-Spam-Level or X-Spam-Status.

I can not find any hmailserver_event.log file.
The script is executed by hmailserver can not transfert the email to the right mailbox (see hmailserver_xxxx-xx-xx.log file). I never get the email inside of my mailbox.

When I do a manual test (in command-prompt) than I see that he has added the extra headers. But when I try to implement it inside of hmailserver I don't get the email in my mailbox. It is just like hmailserver is not waiting until spamassassin is done to move the mail to the right mailbox.

I run a small hosting server for a few of my clients that also handles a (relatively) small volume of mail. Unfortunately, the volume of spam through the server was getting to a point where my clients really wanted something done so I decided to install SpamAssassin.

After trying to follow all the information on here and getting a bit confused, I decided to try and "roll-my-own" to a certain extent.

I downloaded the VBS provided at the beginning of the thread and customised it a bit so it now works (in a slightly convoluted way, but it does work) with the Win32 binaries of SpamAssassin.

This made my life much easier as it means no compiling of code or anything. Just download the binaries and point the VBS script to them.

Don't get me wrong, it's most definitely not great code as I was hacking it together but it does work and hopefully some people here will be able to suggest some improvements.

2.) Download the Win32 Command Line Tool Binaries for SpamAssasssin, these can be found at http://sawin32.sourceforge.net/ You may need to scroll down a little till you see the ones you need

3.) Extract all these files to c:\spamassassin. This should mean that the executable spamassassin.exe is now located at c:\spamassassin\spamassassin.exe

3.) Make a new folder in the root of your c drive called batch

4.) Modify your hMailServer External Scanner to run the following command (without the double quotes!): "c:\windows\system32\cscript.exe //nologo c:\SaCheck.vbs" and set the return value to 55.

That should be it and your incoming emails should now be scanned with hMailServer.

All there is to be done now is to start using spamc and spamd to speed things up but I've got to look into this a little bit further and the performance currently isn't bad enough to warrant spending much time on it!

Hopefully this will help someone as it means you don't have to worry about compiling code!

If you need to get hold of me personally, email me at adam@picatnet.co.uk as I check my email more than I check forums!

First off awesome script I have been looking for something like this, just got it working, and it is processing spam correctly, quick question, I have set the the email to be automatically deleted if it reaches a score of 10 or above.

Const SAHI_auto_discard_score = 10

and I also set Const SAHI_add_spam_check_status_header = 0 as I thought that was the line to make sure the score wasnt printed as part of the email since i only wanted it in the mail header, however the email isnt deleting for scores over 10 and it is giving me a full description in the mail rather than just the mail header, where could i possibly be going wrong?!

for e.g.

Spam detection software, running on the system "Eleanor", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see the administrator of that system for details.

Content preview: VIRUS DETECTED: The attachment(s) of this message was removed since a virus was detected in at least one of them. [...]

wacko and me figured this out by Private Messaging. In the end it came all down to an outdated hMailserver version. After upgrading to the current stable version (build 270) , everything works fine for wacko.

Shelby wrote:wacko and me figured this out by Private Messaging. In the end it came all down to an outdated hMailserver version. After upgrading to the current stable version (build 270) , everything works fine for wacko.

Hi Shelby

I the same issue - used build 270 and latested beta still errors with:

Well, I must have done something wrong because now SA considers all email outgoing, and skips checks on everything!

I believe the trouble is that I have removed the first part of the template value for SAHI_domain_received_header: the "Received: from HOSTNAME (IP)" part, and left only the "by MAILSERVER with hMailServer ;" part.

I did this because the received from part always changes - depending on who is the sender and where is he sending from (locally within our office, or from outside, etc).

Is there any other way for it to distinguish outgoing messages from incoming? Or any other way to set this variable?

OK, I successfully migrated from Argosoft to hMailServer and got Spamassassin up and running, with the script on this page.

Now, what I would like to do, is to add some code to the script, in order to copy all *.eml files after Spam processing to a certain folder, depending if they are Ham or Spam. So all Ham-eml should be copied to a certain folder, and all the Spam-eml should be copied to another folder, in order to archive all messages and to be able to inspect it afterwards to use it for SA-LEARN.

Since I am not a VBS Coder, I ask you for help. Can anybody provide me with the VBS code to copy the eml Message to another folder (before it's being delivered by hMailServer)?

Add one section for Ham, Spam and Error right after the lines, where it says "Spam Found" "Ham Found" "Error Found", and the *.eml files will be copied automatically into the according archive folders.

If you have an alternate command line you have to add the sections another three times.

I am following the installation instructions for SpamAssassin and I am getting an error in the NMAKE. It is failing with an error U1077 and mentions CMD.EXE. I have installed all optional PERL modules as discussed and the makefile appears to work. I am answering N to the C question. Anyone have any thoughts? Thanks.

Hello! I'm using the VBScript from "Adamant" and in the Settings/AntiVirus/External Virus Scanner I put the code "c:\windows\system32\cscript.exe //nologo c:\SaCheck.vbs" BUT when the actual hMailServer calls it, it doesn't put quotes around the path such as "DEBUG" 5884 "2008-03-07 17:44:47.374" "CustomVirusScanner: can() - c:\windows\system32\cscript.exe //nologo c:\SaCheck.vbs C:\Program Files\hMailServer\Data\{A859BD2E-07F2-42E7-8448-A4AC89D355D8}.eml - Returned 89"

I need to either know how to put quotes around the parameter OR if there is a different place I need to call the script from.

... so, your text comment says put this in one event, and your commented out code says a different one.I've tried it both ways. Both invoke spamc just fine - but if SAHI gets run at OnDeliver, modified email doesn't get "caught" by server based rules, but it does when it's run at OnAccept.

Was there some problem with running it at OnAccept? If there was, I suppose whatever it it'll bite me in the backside eventually, but it'd be nice to hear about it in advance...(?)Best

simondsmason wrote:I am following the installation instructions for SpamAssassin and I am getting an error in the NMAKE. ... Anyone have any thoughts? Thanks.

Maybe just grab a precompiled-for-win32 version at SAwin32? I've been using this (well, successive versions of this) for three or four years, now - no problems.I've been using the spamc/spamd combo (spamd stays resident, so there's less overhead on each call to SA) but there's a spamassassin.exe as well if you just want to see if it works...

bobkoure, you are right with your observation. Smells fishy. I only remember having troubles with the OnAcceptMessage event and therefore used OnDeliverMessage at some point during development. Nevertheless I do not remember what the problem was... hmm. Anyway, the script works fine for me by using the OnDeliverMessage event.

In advance apologies for the n00b questions. I have had a good read through this thread and its all still about as clear as mud...!!

I have opted for the somewhat easier approach, or so i thought, of using the pre-compliled executables for spamassassin the source forge SpamAssassin-3.2.3.5-win32.zip version for command line win32. Im running hmail 4.4 B270.

I have used your script shelby and its just not really doing a great deal. To check i have done this...

1: i assume you copy the script to the end of the: EventHandlers.vbs

2: In hmail>settings>advanced i have 'active' and tghe syntax checks fine.

3: I have not added spamassassin into environment/PATH location instead i have called it by its location on the line. "Const SAHI_default_scan_command_line = "%comspec% /c c:\spamassassin\spamc.exe -E -s 512000"" minus the quotes.

4: I have uncommented thus: Sub OnDeliverMessage(oMessage)SAHI_processMessage oMessage End Sub

5: I have set the learn line as follows:Const SAHI_learn_command_line = "%comspec% /c c:\spamassassin\sa-learn.exe --"

I just wondered if it was ok for someone to post a: "this is how its done" with the pre-copiled bundle, start to finish, or a quicker one just point me in the general direction so i can get SA implemented ion my 2k3 box.

Thanks for the reply and apologies for the inconcise post, it was a bit of a rush at the end of my day... It takes another person to read your question for you then to really realise what you missed and i missed out a fair bit.

Thanks for your time on this, I know how frustrating it can be trying to explain how to get it up an running. As soon as i do get mine running there is a bunch of posts asking for a 'how to guide' on 2k3>SA>Hmail4.4[B270] on the forum so i'll get that all answered for everyone.

I have also just checked my mailboxes in thunderbird and they are set up as IMAP, just so i can keep all the mail on the server, I check it from all over with squirrelmail which is installed on the box. Not too sure if this impacts the situation but thought I'd give the heads up. As the test for the mail i'm using the thunderbird 'redirect' and 'redirecting' from one local account to another.

Interestingly (or not) when I redirect a mail to the test account I get a log entry but when I just send it a mail I don't. Also the 'sent' mail has its headers altered by spamassassin: (so it does seem to be running without the PATH entry)

The only (and maybe problematic) difference is that the 1 spam e-mail I have that I am testing the redirect with, had a previous edited header and has the appended line: X-Spam-Check-Status: Checked on incoming

Mike, I need all the log lines for this specific mail. This means: More than one. Many. About 10 to 20. Not the whole log. The above was just a sample how you could identify a log line. I need something like:

line so that I can avoid having forwarded mails to local accounts double-checked with spamassassin? Right now, its set so mails originating from the local lan are recognized, i.e. 192.168.xxx. Forwarded mails originate from localhost, though.

I haven't figured out a solution to my problem from the above post, and already I have another question:

How can I control into which directory the Bayes data is written to? I have the SAWin32 executables (spamc.exe, spamd.exe, sa-learn.exe) and have spamd installed as Windows service which starts under 'LocalSystem' account.

Now, there are several '.spamassassin' directories in the 'Documents and Settings/[USER]' directory tree, were [USER] is all the local users plus NetworkService and LocalService. When I first installed SA and Shelby's script all the Bayes data was written into the 'Documents and Settings/NetworkService/.spamassin' directory, that is data from the SA auto-learning AND the data from when I sent emails from my email client to the learn_mailboxes configured in Shelby's script.

Now, and I am really confused about this: After rebooting the computer the auto-learn data gets written into another directory (one of the local users) and the data from the learn_mailboxes feature in Shelby's script (which uses sa-learn.exe) gets written into yet another directory: 'Docu...Settings/LocalService/.spamassassin'

I am not sure if this is the right forum for this question, but maybe someone has already had this problem, too, and can help.

After a bit of research and getting familiar with SpamAssassin I figured out how to solve my problems from above two posts (these were really SpamAssassin related and have nothing to do with hMailServer or Shelby's Script):

Avoid have SpamAssassin double-check internally forwarded messages:

This can be done via a header rule in local.cf to identify the unique header line of a locally forwarded mail and then shortcircuiting the other SA checks.

I do this because I have all email from my several email accounts which is classified as spam forwarded to an extra account, e.g. SPAM@domain.com. It is really fruitless to have these messages spam-checked a second time.

I'm trying to find a tutorial for install like is in the first message if this topic, but some links don't exist anymore. Please, if someone can show me any tutorial about how to install it, I will thjank you.

HELP...this is so confusing...Is SAwin32 the easiest way? I think i want to use it but what do i do the links to the scripts ealier in this post don't work after reading all this my head is spinning can someone please give me a updated breakdown of implementing SAwin32??? PLEASE...I have never used SA at all...AKA noob, somewhat.

The SA for Windows I've mentioned has a setup which installs SA and downloads the latest rules. Then you just have to edit the \etc\spamassassin\local.cf configuration file to suit your needs, start SpamD and enable the support in hMS. Tooms SpamDloaderService only checks if SpamD runs and if not, restarts it. However, SpamAssassin isn't a out-of-the box solution in any case. If you want it to deliver good results then you have to get at least a little bit familiar with it.

mattfox27 wrote:So i downloaded and installed the jam-software version but it fails to start what are the basic parameters that need to be in conf file to get it to run?

What fails to start? SpamD? It should tell you in it's window why it failed to start. There is nothing in the local.cf config file that could cause SpamD to not start. There you should adjust the trusted_networks entry to match your LAN/whatever IP's and configure the 'shortcircuit' parameters to save CPU time but all settings are well documented inside the config file(s).

I got it to work...i think. Im using the tooms.dk windows service software...is that a good approach? Does anyone have experience using that program? It seems to be working but it won't allow updates. DO i need to enable updates in conf file? I know i need to read the manuals and all that and i'am doing so i was just looking for some quick setup info so sorry if im being a pain