The concept behind auto whitelisting is if a sender was a recipient in the past (of the current recipient), then the sender must be valid. This information is available in the hmailserver database. Why create new tables or text files to hold this info?
Here's my go at it. I recycled Soren's whitelisting function to make it work. The function deletes any reference to spam score, therefore bringing the spam score to 0 if there was a score.
All my function does is check hm_message_metadata to see if current recipient has sent N messages to the current sender in the past X days. If true, it calls Soren's whitelist function.
It does NOT modify hMailServer whitelist. Relies on MySQL database, so other databases would need a different query and connection.
Code: Select all
Sub WhiteList(oMessage, strMatch)
Dim i
If (oMessage.HeaderValue("X-hMailServer-Spam") = "YES") Then
oMessage.HeaderValue("X-hMailServer-WhiteList") = strMatch
oMessage.Headers.ItemByName("X-hMailServer-Spam").Delete
For i = 0 To 10
If (oMessage.HeaderValue("X-hMailServer-Reason-" & i) <> "") Then oMessage.Headers.ItemByName("X-hMailServer-Reason-" & i).Delete
Next
oMessage.Headers.ItemByName("X-hMailServer-Reason-Score").Delete
oMessage.Save
End If
End Sub
Function IsSenderAutoWhitelisted(m_Sender, m_Recipient) : IsSenderAutoWhitelisted = False
Dim LookBackDays : LookBackDays = 90 '<--- Number of days to look back to see if sender was recipient
Dim MinSent : MinSent = 3 '<--- Min number of messages previously sent to sender in order to whitelist sender
Dim CountSent : CountSent = 0
Dim oRecord, oConn : Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Driver={MariaDB ODBC 3.1 Driver}; Server=localhost; Database=hmailserver; User=hmailserver; Password=supersecretpassword;"
If oConn.State <> 1 Then
EventLog.Write( "Sub WhitelistSender - ERROR: Could not connect to database" )
CountSent = 0
Exit Function
End If
Set oRecord = oConn.Execute("SELECT COUNT(*) AS count FROM hm_message_metadata WHERE metadata_dateutc > NOW() - INTERVAL " & LookBackDays & " DAY AND metadata_from REGEXP '" & m_Recipient & "' AND metadata_to REGEXP '" & m_Sender & "';")
Do Until oRecord.EOF
CountSent = oRecord("count")
oRecord.MoveNext
Loop
oConn.Close
Set oRecord = Nothing
If CInt(CountSent) > MinSent Then IsSenderAutoWhitelisted = True
End Function
Sub OnAcceptMessage(oClient, oMessage)
REM - Auto Whitelisting
If oClient.Username = "" Then
If IsSenderAutoWhitelisted(oMessage.FromAddress, oMessage.Recipients(0).OriginalAddress) Then
Call WhiteList(oMessage, "AutoWhitelist From = '" & oMessage.FromAddress & "'")
Exit Sub
End If
End If
End Sub