Critical error when eventhandlers is running my script

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
DAHN
New user
New user
Posts: 11
Joined: 2019-04-18 23:42

Critical error when eventhandlers is running my script

Post by DAHN » 2019-08-19 13:50

Hi

I am trying to create a simple script in the "EventHandlers.vbs" file which should write the message.body to a database...
Sometimes it works when an email is received but other times i generates a critical error - and then hmailserver stops working - and I have to reboot the OS on the server to get hmailserver to work again...
The version of hmailserver is: 5.7.0-B2428 (x64)

And my simple script is:
Sub OnAcceptMessage(oClient, oMessage)
Dim e_body
Set conn = CreateObject("ADODB.Connection")
conn.Open "Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=inbound_emails;User=********;Password=********;Option=3;"
e_body=oMessage.Body
if (e_body<>"" and isnull(e_body)=False) then
e_body=replace(e_body,"'","''")
conn.execute("insert into inbound_emails.emails (v_body) values ('" & oMessage.Body & "')")
end if


conn.close
set conn=nothing
End Sub
The error it generates in the log is:
"ERROR" 4272 "2019-08-19 12:56:55.894" "Severity: 2 (High), Code: HM4208, Source: ExceptionHandler::Run, Description: An error occured while executing 'IOCPQueueWorkerTask'"
"ERROR" 4272 "2019-08-19 12:56:56.044" "Severity: 1 (Critical), Code: HM5519, Source: StackLogger::Log, Description: An error has been detected. A mini dump has been written to C:\Program Files\hMailServer\Logs\minidump_2019-08-19 125655_{28FFDC70-6714-437C-84FE-2E481AC6538D}.dmp"
It also generates a dmp-file...
But what is wrong here...? I only want this script to save the message.body to a table in the MySQL database.

Thanks.

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-19 14:57

Maybe try OnSMTPData instead of OnAcceptMessage.

DAHN
New user
New user
Posts: 11
Joined: 2019-04-18 23:42

Re: Critical error when eventhandlers is running my script

Post by DAHN » 2019-08-20 00:55

Hello palinka

Thank you for your suggestion.

But unfortunately that doesn't work either. As I see it (https://www.hmailserver.com/documentati ... onSMTPdata) oMessage.Body is not an option in the OnSMTPData Sub.

But I have another question now... I have looked in the documentation about OnAcceptMessage again (https://www.hmailserver.com/documentati ... eptmessage) - and is it required that it returns the value "Result.Value=0" or 1 or 2..?
If it is - will this problem be solved if I just add "Result.Value=0" to my script before End Sub..?

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Critical error when eventhandlers is running my script

Post by SorenR » 2019-08-20 01:46

DAHN wrote:
2019-08-20 00:55
Hello palinka

Thank you for your suggestion.

But unfortunately that doesn't work either. As I see it (https://www.hmailserver.com/documentati ... onSMTPdata) oMessage.Body is not an option in the OnSMTPData Sub.

But I have another question now... I have looked in the documentation about OnAcceptMessage again (https://www.hmailserver.com/documentati ... eptmessage) - and is it required that it returns the value "Result.Value=0" or 1 or 2..?
If it is - will this problem be solved if I just add "Result.Value=0" to my script before End Sub..?
Try this...

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
    Dim MyDBDrv, oConn, e_body

    MyDBDrv = "DRIVER={MySQL ODBC 8 Unicode Driver};Database=inbound_emails;User=********;Password=********;FOUND_ROWS=1;"
    Set oConn = CreateObject("ADODB.Connection")

    oConn.Open MyDBDrv
    If oConn.State <> 1 Then
        EventLog.Write( "ERROR: Could not connect to database" )
        Exit Sub
    End If

    If Not IsEmpty(oMessage.Body) Then
       e_body = Replace(oMessage.Body, "'", "''")
    Else
       e_body = Replace(oMessage.HTMLBody, "'", "''")
    End If

    oConn.Execute "INSERT INTO emails (v_body) VALUES ('" & e_body & "');"
    Set oConn = Nothing
End Sub
Result.Value=0 does absolutely nothing. Result.Value=1 or 2 or 3 will instruct hMailServer to deliver a specific SMTP fault code and terminate the IP session with the sender.

In the above sample I use "Exit Sub" to exit the "Sub" if the script cannot connect to the database. Similarly there is "Exit Function", "Exit Do" and a few others. The script is never run as a whole, the pre-defined Sub's are called at different stages of processing email.
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

User avatar
RvdH
Senior user
Senior user
Posts: 797
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Critical error when eventhandlers is running my script

Post by RvdH » 2019-08-20 21:51

SorenR wrote:
2019-08-20 01:46
DAHN wrote:
2019-08-20 00:55
Hello palinka

Thank you for your suggestion.

But unfortunately that doesn't work either. As I see it (https://www.hmailserver.com/documentati ... onSMTPdata) oMessage.Body is not an option in the OnSMTPData Sub.

But I have another question now... I have looked in the documentation about OnAcceptMessage again (https://www.hmailserver.com/documentati ... eptmessage) - and is it required that it returns the value "Result.Value=0" or 1 or 2..?
If it is - will this problem be solved if I just add "Result.Value=0" to my script before End Sub..?
Try this...

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
    Dim MyDBDrv, oConn, e_body

    MyDBDrv = "DRIVER={MySQL ODBC 8 Unicode Driver};Database=inbound_emails;User=********;Password=********;FOUND_ROWS=1;"
    Set oConn = CreateObject("ADODB.Connection")

    oConn.Open MyDBDrv
    If oConn.State <> 1 Then
        EventLog.Write( "ERROR: Could not connect to database" )
        Exit Sub
    End If

    If Not IsEmpty(oMessage.Body) Then
       e_body = Replace(oMessage.Body, "'", "''")
    Else
       e_body = Replace(oMessage.HTMLBody, "'", "''")
    End If

    oConn.Execute "INSERT INTO emails (v_body) VALUES ('" & e_body & "');"
    Set oConn = Nothing
End Sub
Result.Value=0 does absolutely nothing. Result.Value=1 or 2 or 3 will instruct hMailServer to deliver a specific SMTP fault code and terminate the IP session with the sender.

In the above sample I use "Exit Sub" to exit the "Sub" if the script cannot connect to the database. Similarly there is "Exit Function", "Exit Do" and a few others. The script is never run as a whole, the pre-defined Sub's are called at different stages of processing email.
Result.Value=3 :?:

:mrgreen:
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Critical error when eventhandlers is running my script

Post by SorenR » 2019-08-20 23:54

RvdH wrote:
2019-08-20 21:51
Result.Value=3 :?:

:mrgreen:
viewtopic.php?p=211186#p211186
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

DAHN
New user
New user
Posts: 11
Joined: 2019-04-18 23:42

Re: Critical error when eventhandlers is running my script

Post by DAHN » 2019-08-21 02:17

I have tried the following script - and it still generates Critical errors - and hmailserver stops running - and then I have to reboot the server...

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)

Dim MyDBDrv, oConn, e_body
MyDBDrv = "Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=inbound_emails;User=**********;Password=*********;Option=3;"
Set oConn = CreateObject("ADODB.Connection")
oConn.Open MyDBDrv
If oConn.State <> 1 Then
EventLog.Write( "ERROR: Could not connect to database" )
Exit Sub
End If
If Not IsEmpty(oMessage.Body) Then
e_body = Replace(oMessage.Body, "'", "''")
oConn.Execute "INSERT INTO emails (v_body) VALUES ('" & e_body & "');"  
End If
Set oConn = Nothing

End Sub
The error-log is:
"ERROR" 4252 "2019-08-21 02:01:01.365" "Severity: 2 (High), Code: HM4208, Source: ExceptionHandler::Run, Description: An error occured while executing 'IOCPQueueWorkerTask'"
"ERROR" 4252 "2019-08-21 02:01:01.575" "Severity: 1 (Critical), Code: HM5519, Source: StackLogger::Log, Description: An error has been detected. A mini dump has been written to C:\Program Files\hMailServer\Logs\minidump_2019-08-21 020101_{7F444481-41E1-4F87-94B0-5CCE8AEC2945}.dmp"
"ERROR" 4252 "2019-08-21 02:01:02.582" "Severity: 2 (High), Code: HM4208, Source: ExceptionHandler::Run, Description: An error occured while executing 'IOCPQueueWorkerTask'"
"ERROR" 4252 "2019-08-21 02:01:02.666" "Severity: 1 (Critical), Code: HM5519, Source: StackLogger::Log, Description: An error has been detected. A mini dump has been written to C:\Program Files\hMailServer\Logs\minidump_2019-08-21 020102_{F635AF25-5187-4B64-98B1-F78DB7D90D1F}.dmp"
- and it generates 2 dmp-files as well...

Can it be something about that MySQL is 64bit - and i have both installed MySQL ODBC 32bit and 64bit..?
- or somehting about that the anti-spam filter and anti-virus filter are both enabled..?

Thanks.

User avatar
mattg
Moderator
Moderator
Posts: 20026
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Critical error when eventhandlers is running my script

Post by mattg » 2019-08-21 02:26

Probably about your ODBC driver

hMailserver is 32 bit, so you need the 32 bit connector, even to a 64 bit MySQL
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-21 14:25

mattg wrote:
2019-08-21 02:26
Probably about your ODBC driver

hMailserver is 32 bit, so you need the 32 bit connector, even to a 64 bit MySQL
He said he was using the 64 version.
The version of hmailserver is: 5.7.0-B2428 (x64)

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-21 14:34

DAHN wrote:
2019-08-21 02:17
Can it be something about that MySQL is 64bit - and i have both installed MySQL ODBC 32bit and 64bit..?
- or somehting about that the anti-spam filter and anti-virus filter are both enabled..?

Thanks.
I bet you're on thr right track with those notions. The second one is easy to test - just disable all antivirus and try again. Definitely worth trying.

Also, have a look at this: https://www.hmailserver.com/forum/viewtopic.php?t=31519

Could also be certificate related.

Error codes should always be googled. :mrgreen:

User avatar
mattg
Moderator
Moderator
Posts: 20026
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Critical error when eventhandlers is running my script

Post by mattg » 2019-08-21 15:12

palinka wrote:
2019-08-21 14:25
mattg wrote:
2019-08-21 02:26
Probably about your ODBC driver

hMailserver is 32 bit, so you need the 32 bit connector, even to a 64 bit MySQL
He said he was using the 64 version.
The version of hmailserver is: 5.7.0-B2428 (x64)
That particular version was made over a year and a half ago, and a LOT of changes have happened since then
and as we know, hMailserverv v5.7 is ALPHA at best, even in the newest build #2484
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-21 18:39

mattg wrote:
2019-08-21 15:12
palinka wrote:
2019-08-21 14:25
mattg wrote:
2019-08-21 02:26
Probably about your ODBC driver

hMailserver is 32 bit, so you need the 32 bit connector, even to a 64 bit MySQL
He said he was using the 64 version.
The version of hmailserver is: 5.7.0-B2428 (x64)
That particular version was made over a year and a half ago, and a LOT of changes have happened since then
and as we know, hMailserverv v5.7 is ALPHA at best, even in the newest build #2484
Then maybe a simple upgrade will solve the whole thing. Good catch.

DAHN
New user
New user
Posts: 11
Joined: 2019-04-18 23:42

Re: Critical error when eventhandlers is running my script

Post by DAHN » 2019-08-22 23:17

Hello

I have now added "Provider=MSDASQL;" - so now it looks like this..:

Code: Select all

MyDBDrv = "Provider=MSDASQL;Driver={MySQL ODBC 8.0 Unicode Driver};Server=localhost;Database=inbound_emails;User=**********;Password=*********;Option=3;"
- to ensure it uses the correct 32bit driver.

It still generate errors and dmp-files - and after about 2 errors hmailservers stops running - and I have to reboot the server.
I have also tried with disabled AV and anti-spam filter - but unfortunately with the same result.

So I would like to upgrade hmailserver to the newest/best version. Where do I find the newest for download..? (when I look in the download-section here the newest version is from 2017...

Thanks.

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-23 14:47

There is a brand new official 5.7 x64 beta but i don't know where the releases are. I'm interested in undertaking this myself. I looked in the development threads but couldn't find anything so im assuming you have to build it yourself? Org maybe i overlooked a link to the releases?

Hopefully someone "in the know" will chime in.

User avatar
mattg
Moderator
Moderator
Posts: 20026
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Critical error when eventhandlers is running my script

Post by mattg » 2019-08-24 00:06

https://build.hmailserver.com/

Use your forum login details to access
Look under artifacts, and grab the exe


This is ALPHA not BETA though, and really shouldn't be used in production
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

DAHN
New user
New user
Posts: 11
Joined: 2019-04-18 23:42

Re: Critical error when eventhandlers is running my script

Post by DAHN » 2019-08-25 17:31

I have now tried with the old hmailserver 5.6.7 and also with the new 5.7 - but it still generates the same errors and dmp-files and still causes hmailserver stops working...
"ERROR" 4284 "2019-08-25 16:51:32.097" "Severity: 2 (High), Code: HM4208, Source: ExceptionHandler::Run, Description: An error occured while executing 'IOCPQueueWorkerTask'"
"ERROR" 4284 "2019-08-25 16:51:32.320" "Severity: 1 (Critical), Code: HM5519, Source: StackLogger::Log, Description: An error has been detected. A mini dump has been written to C:\Program Files (x86)\hMailServer\Logs\minidump_2019-08-25 165132_{D9096FB7-E42B-4ED9-8F45-0C91F9BBFC18}.dmp"
I have also tried where AV software and Windows Defender are disabled - and with the spam-filter disabled as well.

I have tried changing the script to - just for a test:

Code: Select all

   oMessage.Subject = "[4000] " & oMessage.Subject
   oMessage.Save
(so it doesn't have to connect to MySQL 8.0.16 database) - and that doesn't generate any errors so far - but it is not the script I want either...

Does anyone know why these errors above happens..?

User avatar
mattg
Moderator
Moderator
Posts: 20026
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Critical error when eventhandlers is running my script

Post by mattg » 2019-08-25 23:23

DAHN wrote:
2019-08-25 17:31
I have tried changing the script to - just for a test:

Code: Select all

   oMessage.Subject = "[4000] " & oMessage.Subject
   oMessage.Save
(so it doesn't have to connect to MySQL 8.0.16 database) - and that doesn't generate any errors so far - but it is not the script I want either...

Does anyone know why these errors above happens..?
So clearly this is an issue with your script...

My guess is it is the ADODB connection
What user do you have the hMailserver service running under? DOes that user have access to the ADODB connection?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

User avatar
jimimaseye
Moderator
Moderator
Posts: 8094
Joined: 2011-09-08 17:48

Re: Critical error when eventhandlers is running my script

Post by jimimaseye » 2019-08-26 10:55

Is trying to save the entire contents of an email body in to a single SQL column/field a wise thing to do? My guess is its a content or quantity issue.

[Entered by mobile. Excuse my spelling.]
HMS 5.6.6 B2383 on Win Server 2008 R2 Foundation, + 5.6.7-B2415 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-26 13:09

jimimaseye wrote:
2019-08-26 10:55
Is trying to save the entire contents of an email body in to a single SQL column/field a wise thing to do? My guess is its a content or quantity issue.

[Entered by mobile. Excuse my spelling.]
Good clue.

Try removing html elements first. Also, vbs + sql don't play nicely together with quotations and double quotes, among other things. I found a "remove html" function on the interwebs that's pretty exhaustive and has worked well for me. I'll paste it later if you can't find a good one, but there are many results for this in a Google search.

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-26 13:13

Also, have you looked to see what, if anything, was inserted into the db?

DAHN
New user
New user
Posts: 11
Joined: 2019-04-18 23:42

Re: Critical error when eventhandlers is running my script

Post by DAHN » 2019-08-26 14:30

Thank you for your answer.

My job is that I have a server that receives a mail every night from another server outside the network, which contains a link to something my server needs to download and load into the database.

The URL in the email is always different - with different query string.
So my idea is to put hmailserver to store the contents (Message.Body) in the database - and then run a script that will take the URL and then download the file.

So, of course, the email address is set up to only receive emails from that server outside the network.

There are a number of other vb scripts running on my server every day that use the MySQL database on a large scale - but have nothing to do with hmailserver.

I've checked if it writes in the database - and it does when it receives my test emails - until the mail server goes down after about 5-10 minutes and starts writing errors in the log and making dmp files.
 - as described.

The emails I've tested have just contained the word 'test' in Body and the data type is 'text (0)' in the database - so there should be plenty of space.

When I check which user the service is running under is marked 'Local System Account'. I can easily change that to something else - if necessary ... But how do I see if that user has access to ADODB ..?

Message.Body in the mails to be saved does not contain html - but just plain text with the URL in.

I also created a new Sub in EventHandlers - which now contains my script instead of 'OnAcceptMessage' - and set up a rule on the mailing address that will receive the mail, which should then run that Sub.
 - But the fact that it is now in a homemade Sub - unfortunately makes no difference to the fact that the mail server goes down and makes errors ...

Thanks.

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-26 15:42

So it works SOMETIMES? Or it crashes on EVERY INSTANCE the script is called. It seems as though that is important information.

Also, based on your description, is recording the url to the database even necessary? Why not simply run your downloader script directly?

DAHN
New user
New user
Posts: 11
Joined: 2019-04-18 23:42

Re: Critical error when eventhandlers is running my script

Post by DAHN » 2019-08-26 15:56

Yes - it works shortly after reboot.

After restarting the server (OS) the script seems ok. I manage to send about 2-3 test emails to that email address with the content 'test' written in the database of the script - and a little later the mentioned errors occur and the mail server goes down and no longer responds. It also no longer writes in the log files after it crashes.
  - If I then choose to restart the server (OS) - it works again for a short time - before errors and dmp files come again.

I prefer to run my download script at certain times of the day - and not necessarily when the email with the URL comes due to performance ...

palinka
Senior user
Senior user
Posts: 986
Joined: 2017-09-12 17:57

Re: Critical error when eventhandlers is running my script

Post by palinka » 2019-08-26 19:12

DAHN wrote:
2019-08-26 15:56
I prefer to run my download script at certain times of the day - and not necessarily when the email with the URL comes due to performance ...
I know this is not a fix, but may be a workable solution. You could write the url to a text file to be read anytime.

User avatar
SorenR
Senior user
Senior user
Posts: 3169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Critical error when eventhandlers is running my script

Post by SorenR » 2019-08-26 22:27

DAHN wrote:
2019-08-26 14:30
Thank you for your answer.

My job is that I have a server that receives a mail every night from another server outside the network, which contains a link to something my server needs to download and load into the database.

...
...
Emails are stored in C:\[hmailserver folder]\Data\[domain]\[username]\[list of folders ...

The folder name that the email is stored into is determined by the first two letters of the email filename.

Monitor the list of folders for new files since 24 hours and you will find your email. If the body text is "plain text" it is simply a matter of reading the file and pluck the information you need.
SørenR.

The quantum rule of insecurity which states that the act of observing how vulnerable a host or service is changes the insecurity level of the service.

Post Reply