VBScript Regex to Grab Status Code and Test If 500 or higher.

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
cblaze22
Normal user
Normal user
Posts: 165
Joined: 2011-08-30 20:16

VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by cblaze22 » 2019-05-06 03:09

I need a VBScript to Grab Status Code and Test If 500 or higher. I can do this in .NET no problem but VBScript syntax is a little harder for me. The pattern is below but how would i test the string below to grab the 421, then do an IF statement on it (statusCode >= 500)?

Code: Select all

^.*Remote server replied: (?<statuscode>\d*).*$
"Error Type: SMTP[nl] Remote server (35.173.65.153) issued an error.[nl] hMailServer sent: [nl] Remote server replied: 421 spr-ibgw-5001a.stratus.cloudmark.com cmsmtp SMTP server not available AUP#1330[nl]"

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

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by SorenR » 2019-05-06 14:06

cblaze22 wrote:
2019-05-06 03:09
I need a VBScript to Grab Status Code and Test If 500 or higher. I can do this in .NET no problem but VBScript syntax is a little harder for me. The pattern is below but how would i test the string below to grab the 421, then do an IF statement on it (statusCode >= 500)?

Code: Select all

^.*Remote server replied: (?<statuscode>\d*).*$
"Error Type: SMTP[nl] Remote server (35.173.65.153) issued an error.[nl] hMailServer sent: [nl] Remote server replied: 421 spr-ibgw-5001a.stratus.cloudmark.com cmsmtp SMTP server not available AUP#1330[nl]"
VBScript RegEx is based on ECMAScript syntax... hMailServer Rule Regex is based on PCRE syntax... Welcome to the brotherhood ;-)

I believe this should do it. Working with Non-capturing Groups in VBScript is not plain logic :mrgreen:
It's my first go at the Non-capturing Group thing and I would not be surprised if I can cook this down even further when I get more time.

Code: Select all

Sub OnDeliveryFailed(oMessage, sRecipient, sErrorMessage)
   Dim regex, matches, match, strResult

   Set regex = New RegExp
   regex.IgnoreCase = True
   regex.Pattern = "^(?:.*Remote server replied:\s)([0-9]{3})(?:\s.*)$"
 
   Set matches = regex.Execute(sErrorMessage)

   If matches.Count >= 1 Then
      Set match = matches(0)
      If match.SubMatches.Count >= 1 Then
         strResult = match.SubMatches(0)
      Else
         strResult = ""
      End If
   Else
      strResult = ""
   End If

   EventLog.Write( "strResult: " & strResult & vbTab & " sRecipient: " & sRecipient )

   Set match = Nothing
   Set matches = Nothing
   Set regex = Nothing
End Sub
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

cblaze22
Normal user
Normal user
Posts: 165
Joined: 2011-08-30 20:16

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by cblaze22 » 2019-05-06 14:40

Thanks but is the strResult an integer where I can do

Code: Select all

IF strResult  >= 500

' run code

END IF

cblaze22
Normal user
Normal user
Posts: 165
Joined: 2011-08-30 20:16

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by cblaze22 » 2019-05-06 15:37

I assume this will work

Code: Select all

   Sub OnDeliveryFailed(oMessage, sRecipient, sErrorMessage)

	Dim regex, matches, match, strResult, valInt

	Set regex = New RegExp
   	regex.IgnoreCase = True
   	regex.Pattern = "^(?:.*Remote server replied:\s)([0-9]{3})(?:\s.*)$"
 
   	Set matches = regex.Execute(sErrorMessage)

   	If matches.Count >= 1 Then
      	Set match = matches(0)
      	If match.SubMatches.Count >= 1 Then
         	strResult = match.SubMatches(0)
      	Else
         	strResult = ""
      	End If
   	Else
      		strResult = ""
   	End If

	If Not IsEmpty(strResult) Then
            If IsNumeric(valString) Then
                valInt = CInt(valString)
		If valInt >= 500 Then
			' call http
            		End With
		End If
            End If
        End If
   End Sub

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

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by SorenR » 2019-05-06 16:38

Code: Select all

Sub OnDeliveryFailed(oMessage, sRecipient, sErrorMessage)
   Dim regex, matches, match, strResult, sURL

   Set regex = New RegExp
   regex.IgnoreCase = True
   regex.Pattern = "^(?:.*Remote server replied:\s)([0-9]{3})(?:\s.*)$"
 
   Set matches = regex.Execute(sErrorMessage)

   If matches.Count >= 1 Then
      Set match = matches(0)
      If match.SubMatches.Count >= 1 Then
         strResult = match.SubMatches(0)
      Else
         strResult = ""
         exit sub ' nothing we can do here...
      End If
   Else
      strResult = ""
      exit sub ' nothing we can do here...
   End If

   If IsNumeric(strResult) And CInt(strResult) > 499 Then
      sURL = "http://www.test.com/index.htm?retcode=" & CInt(strResult) & "&recipient= " & sRecipient
      On Error Resume Next
      With CreateObject("MSXML2.ServerXMLHTTP.6.0")
         .setoption(2) = (.getoption(2) & " - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS")
         .open "GET", sURL, False
         .setrequestheader "User-Agent", "hMailServer EventHandlers.vbs"
         .send ("")
      End With
      On Error Goto 0
   End If

   Set match = Nothing
   Set matches = Nothing
   Set regex = Nothing
End Sub
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

cblaze22
Normal user
Normal user
Posts: 165
Joined: 2011-08-30 20:16

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by cblaze22 » 2019-05-07 05:14

Not working
3544 "2019-05-06 23:09:51.609" "Running"
3544 "2019-05-06 23:09:51.609" "Error Type: SMTP[nl] Remote server (166.216.149.129) issued an error.[nl] hMailServer sent: RCPT TO:<8583390609@txt.att.net>[nl] Remote server replied: 550 5.1.1 <8583390609@txt.att.net> recipient does not exist here.[nl]"
3544 "2019-05-06 23:09:51.609" "No Match"

Code: Select all

Sub OnDeliveryFailed(oMessage, sRecipient, sErrorMessage)

	Dim regex, matches, match, strResult

	EventLog.Write("Running")

	EventLog.Write(sErrorMessage)

	Set regex = New RegExp
   	regex.IgnoreCase = True
   	regex.Pattern = "^(?:.*Remote server replied:\s)([0-9]{3})(?:.*)$"
 
   	Set matches = regex.Execute(sErrorMessage)

   	If matches.Count >= 1 Then
      		Set match = matches(0)
      		If match.SubMatches.Count >= 1 Then
         		strResult = match.SubMatches(0)
			EventLog.Write(strResult)
      		Else
EventLog.Write("NO SUBMATCHES")
         		strResult = ""
         		exit sub
      		End If
   	Else
EventLog.Write("No Match")
      		strResult = ""
      		exit sub
   	End If
	EventLog.Write(strResult) 
	If IsNumeric(strResult) And CInt(strResult) >= 500 Then
		EventLog.Write(strResult)
      		On Error Resume Next
      		With CreateObject("MSXML2.ServerXMLHTTP")
               		.setoption(2) = (.getoption(2) & " - SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS")
               		.open "GET", "https://test.com/subscriptions/bounce?email=" + sRecipient + "&statuscode=" + strResult + "&message=" + sErrorMessage, False
               		.setrequestheader "User-Agent", "Test SMTP"
               		.send ("")
      		End With
      		On Error Goto 0
   	End If

   	Set match = Nothing
   	Set matches = Nothing
   	Set regex = Nothing
   End Sub

cblaze22
Normal user
Normal user
Posts: 165
Joined: 2011-08-30 20:16

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by cblaze22 » 2019-05-07 06:54

Missing

Code: Select all

regex.MultiLine = True

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

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by SorenR » 2019-05-07 10:58

cblaze22 wrote:
2019-05-07 06:54
Missing

Code: Select all

regex.MultiLine = True
of course it does... sorry.
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

fatman45
New user
New user
Posts: 19
Joined: 2015-06-02 17:22
Location: Nevada Test Site, USA

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by fatman45 » 2019-06-11 00:15

Your code could be shortened like this using a capture group.

Code: Select all

If regex.Test(sErrorMessage) Then
	valInt = CInt(regex.Replace(sErrorMessage, "$2"))
End If

glenluo
Normal user
Normal user
Posts: 194
Joined: 2011-07-03 12:10

Re: VBScript Regex to Grab Status Code and Test If 500 or higher.

Post by glenluo » 2019-09-06 07:38

Getting the error code is so easy,just one line enough:

Left(Split(sErrorMessage,"Remote server replied: ")(1),3)

Post Reply