Run function with regexp processing doesn't work

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
Darxton
New user
New user
Posts: 2
Joined: 2019-07-24 19:19

Run function with regexp processing doesn't work

Post by Darxton » 2019-07-24 19:28

Hi everyone,

I'm trying to replace From field with value taken from body of message. I wrote the code below

Code: Select all

Function Email(strRegEx, strMatch)
     Email = ""
     With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = strRegEx
        .IgnoreCase = True

	Set objMatches = .Execute(strMatch)
	if objMatches.Count > 0 then
		For i=0 To objMatches.Count-1
		    Set objMatch = objMatches.Item(i)
		    Email = "" & objMatch.SubMatches(0)
		Next
	End If
      End With
End Function

Sub ForOnlineShopOrders(oMessage)

	Dim NewEmail
	Dim Pattern

	Pattern = "Email:\s*([^ ""(\,>:</\\@¶]+@[^, >¶]+\.[0-9a-zA-ZрфРФ-]{2,8})"
	NewEmail = Email(Pattern, oMessage.Body)
'	NewEmail = "KLSHLAHF"

	oMessage.From = NewEmail
	oMessage.FromAddress = NewEmail
	oMessage.Save()
End Sub
but the result is only errors in log when calling this function in rule:
"ERROR" 12988 "2019-07-24 20:26:33.269" "Script Error: Source:


It definetely do not like the string With CreateObject("VBScript.RegExp"), but i don't understand why, because following script saved as .vbs works fine on the same machine.

Code: Select all

Function Email(strRegEx, strMatch)
      Email = ""
      With CreateObject("VBScript.RegExp")
         .Global = True
         .Pattern = strRegEx
         .IgnoreCase = True

	Set objMatches = .Execute(strMatch)
	if objMatches.Count > 0 then
		For i=0 To objMatches.Count-1
		    Set objMatch = objMatches.Item(i)
		    Email = "" & objMatch.SubMatches(0)
		Next
	End If
      End With
End Function

Dim BodyText
Dim NewEmail
Dim Pattern

Pattern = "Email:\s*([^ ""(\,>:</\\@¶]+@[^, >¶]+\.[0-9a-zA-ZрфРФ-]{2,8})"
NewEmail = Email(Pattern, "Email: 	 email@gmail.com")
MsgBox NewEmail
Any ideas?

Darxton
New user
New user
Posts: 2
Joined: 2019-07-24 19:19

Re: Run function with regexp processing doesn't work

Post by Darxton » 2019-07-24 23:45

Found the issue. In hmail script ALL variables must be declared before use, and execution fails at
Set objMatches = .Execute(strMatch)

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

Re: Run function with regexp processing doesn't work

Post by jimimaseye » 2019-07-25 08:13

Darxton wrote:
2019-07-24 23:45
Found the issue. In hmail script ALL variables must be declared before use, and execution fails at
Set objMatches = .Execute(strMatch)
Only if you happen to have OPTION EXPLICIT set at the top of the script. As you didnt show us your full eventshandler, we couldnt have advised on that.

But well done on figuring it out.
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

Post Reply