HMailServer has the possibility to attach a script to its major events, delivery accepted, failure, server error, etc.
In order to track delivery failure, like mailbox unavailable or SPAM rejection here is a script I've written to use in the OnDeliveryFailed event:
Function FailDeliveryLogToDB(oMessage, sRecipient, sErrorMessage)
Dim Conn
Dim strConn
Dim strSql
Dim bounceMsg, bounceBody, headers, rId, lId, sId, sbId, iHeader
Dim strUid
Dim strMsgId
Dim deliveryDate
Dim arr
Dim iMonth
Dim errCode
For iHeader = 0 To oMessage.Headers.Count - 1
If (oMessage.Headers(iHeader).Name = "X-UID") Then
strUid = oMessage.Headers(iHeader).Value
strUid = Replace(Replace(strUid,"<",""),">","")
End If
If (oMessage.Headers(iHeader).Name = "Message-ID") Then
strMsgId = oMessage.Headers(iHeader).Value
End If
Next
arr = split(oMessage.Date," ")
iMonth = Month(DateValue("01-" & arr(1) & "-2005"))
deliveryDate = arr(2) & LPad(iMonth,2,"0") & LPad(arr(0),2,"0") & " " & arr(3)
errCode = Trim(Mid(sErrorMessage,InStr(1,sErrorMessage,"Remote server replied:")+23,3))
Set Conn = CreateObject("ADODB.Connection")
strConn="DRIVER={SQL Server};SERVER=serverIP;UID=DbUsername;PWD=serverPwd;DATABASE=DatabaseName"
Conn.open strConn
strSql = "INSERT INTO [DeliveryFailures] ([Uid],[ErrorDescription],[Recipient],[Sender],[MsgId],[ErrorCode],[DeliveryDate]) VALUES ('" & strUid & "','" & sErrorMessage & "','" & sRecipient & "','" & oMessage.FromAddress & "','" & strMsgId & "','" & errCode & "','" & deliveryDate & "')"
Conn.Execute(strSql)
If Conn.State = adStateOpen then
Conn.Close
End If
Set Conn = Nothing
End Function
Function LPad(s, l, c)
Dim n : n = 0
If l > Len(s) Then n = l - Len(s)
LPad = String(n, c) & s
End Function
Please note that in our Mailing List application, we are using a custom header field, X-UID, to track email.
This is important to generate reports on sending, failures, if some ISP has put us on a black list, etc.
If you get some error in the script execution, in particular on the CreateObject function, try to give right permission on DCOM
- Start DCOM config by selecting Start, Run, enter dcomcnfg.exe and press OK.
- In the Component Services program, expand the Component Services folder
- Expand down to Computers | My Computer | DCOM Config
- Right-click on hMailServer and select properties
- Select the Security tab
- Under "Launch and Activation Permissions", select Customize and click on Edit
- Under "Group or user names", click Add
- For IIS6: add the built-in anonymous IIS user account
- For IIS7 >> add the builtin IIS_IUSRS user account
- For Apache: add the Apache user account
- Set the Local Launch and Local Activation permissions for this user to Allow
I've taken the idea from here: https://www.hmailserver.com/forum/viewtopic.php?t=13890