Metadata by Message-ID
-
- Senior user
- Posts: 390
- Joined: 2016-12-08 02:21
Metadata by Message-ID
I get a callback POST from SendGrid for delivery events. The data provided by SendGrid is incomplete notify the message originator, so I need a way to look up the message in hMailServer metadata. I do get the Message-ID of the message in the POST. I don't see a method in the API to lookup a message globally by Message-ID.
Could I get this data from the hMailServer database? How long after a message is delivered (as far as HMS is concerned) would this data persist in the HMS DB?
Another way I could handle this is with a script procedure to write records to a table keyed on Message-ID and consisting of the other metadata I need to generate an NDR (i.e. originator).
Any ideas?
Could I get this data from the hMailServer database? How long after a message is delivered (as far as HMS is concerned) would this data persist in the HMS DB?
Another way I could handle this is with a script procedure to write records to a table keyed on Message-ID and consisting of the other metadata I need to generate an NDR (i.e. originator).
Any ideas?
Re: Metadata by Message-ID
The metadata table only hold emails stored in IMAP folders so if you don't store sent mail it won't be there.mikedibella wrote: ↑2021-01-04 20:14I get a callback POST from SendGrid for delivery events. The data provided by SendGrid is incomplete notify the message originator, so I need a way to look up the message in hMailServer metadata. I do get the Message-ID of the message in the POST. I don't see a method in the API to lookup a message globally by Message-ID.
Could I get this data from the hMailServer database? How long after a message is delivered (as far as HMS is concerned) would this data persist in the HMS DB?
Another way I could handle this is with a script procedure to write records to a table keyed on Message-ID and consisting of the other metadata I need to generate an NDR (i.e. originator).
Any ideas?
Making a sub to store outgoing metadata in a custom table is simple if you can accept a new table in the HM database.
Code: Select all
Function GetDatabaseObject()
Dim oApp : Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(ADMIN, PASSWD)
Set GetDatabaseObject = oApp.Database
Set oApp = Nothing
End Function
Function MyMetaData(oMessage)
Dim strSQL, oDB : Set oDB = GetDatabaseObject
strSQL = "INSERT INTO my_metadata (timestamp,key,data1,data2) VALUES (NOW(),'bla', 'bla', 'bla') ON DUPLICATE KEY UPDATE timestamp=NOW();"
Call oDB.ExecuteSQL(strSQL)
Set oDB = Nothing
End Function
AND... you need to build the table to match your finished script

SørenR.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
Re: Metadata by Message-ID
Why not just look up the metadata_from in hm_message_metadata?SorenR wrote: ↑2021-01-05 00:05The metadata table only hold emails stored in IMAP folders so if you don't store sent mail it won't be there.mikedibella wrote: ↑2021-01-04 20:14I get a callback POST from SendGrid for delivery events. The data provided by SendGrid is incomplete notify the message originator, so I need a way to look up the message in hMailServer metadata. I do get the Message-ID of the message in the POST. I don't see a method in the API to lookup a message globally by Message-ID.
Could I get this data from the hMailServer database? How long after a message is delivered (as far as HMS is concerned) would this data persist in the HMS DB?
Another way I could handle this is with a script procedure to write records to a table keyed on Message-ID and consisting of the other metadata I need to generate an NDR (i.e. originator).
Any ideas?
Making a sub to store outgoing metadata in a custom table is simple if you can accept a new table in the HM database.
NB ! Using hMailServer DB API is WRITE-ONLY !Code: Select all
Function GetDatabaseObject() Dim oApp : Set oApp = CreateObject("hMailServer.Application") Call oApp.Authenticate(ADMIN, PASSWD) Set GetDatabaseObject = oApp.Database Set oApp = Nothing End Function Function MyMetaData(oMessage) Dim strSQL, oDB : Set oDB = GetDatabaseObject strSQL = "INSERT INTO my_metadata (timestamp,key,data1,data2) VALUES (NOW(),'bla', 'bla', 'bla') ON DUPLICATE KEY UPDATE timestamp=NOW();" Call oDB.ExecuteSQL(strSQL) Set oDB = Nothing End Function
AND... you need to build the table to match your finished script![]()
Code: Select all
Sub LookupSender(ByVal m_ID, ByRef m_Sender)
Dim oRecord, oConn : Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Driver={MariaDB ODBC 3.1 Driver}; Server=localhost; Database=hmailserver; User=hmailserver; Password=secretpassword;"
If oConn.State <> 1 Then
EventLog.Write( "Sub LookupSender - ERROR: Could not connect to database" )
m_Sender = ""
Exit Sub
End If
Set oRecord = oConn.Execute("SELECT metadata_from FROM hm_message_metadata WHERE metadata_messageid = '" & m_ID & "';")
Do Until oRecord.EOF
m_Sender = oRecord("metadata_from")
oRecord.MoveNext
Loop
oConn.Close
Set oRecord = Nothing
End Sub
Re: Metadata by Message-ID
palinka wrote: ↑2021-01-05 05:15Why not just look up the metadata_from in hm_message_metadata?SorenR wrote: ↑2021-01-05 00:05The metadata table only hold emails stored in IMAP folders so if you don't store sent mail it won't be there.mikedibella wrote: ↑2021-01-04 20:14I get a callback POST from SendGrid for delivery events. The data provided by SendGrid is incomplete notify the message originator, so I need a way to look up the message in hMailServer metadata. I do get the Message-ID of the message in the POST. I don't see a method in the API to lookup a message globally by Message-ID.
Could I get this data from the hMailServer database? How long after a message is delivered (as far as HMS is concerned) would this data persist in the HMS DB?
Another way I could handle this is with a script procedure to write records to a table keyed on Message-ID and consisting of the other metadata I need to generate an NDR (i.e. originator).
Any ideas?
Making a sub to store outgoing metadata in a custom table is simple if you can accept a new table in the HM database.
NB ! Using hMailServer DB API is WRITE-ONLY !Code: Select all
Function GetDatabaseObject() Dim oApp : Set oApp = CreateObject("hMailServer.Application") Call oApp.Authenticate(ADMIN, PASSWD) Set GetDatabaseObject = oApp.Database Set oApp = Nothing End Function Function MyMetaData(oMessage) Dim strSQL, oDB : Set oDB = GetDatabaseObject strSQL = "INSERT INTO my_metadata (timestamp,key,data1,data2) VALUES (NOW(),'bla', 'bla', 'bla') ON DUPLICATE KEY UPDATE timestamp=NOW();" Call oDB.ExecuteSQL(strSQL) Set oDB = Nothing End Function
AND... you need to build the table to match your finished script![]()
Code: Select all
Sub LookupSender(ByVal m_ID, ByRef m_Sender) Dim oRecord, oConn : Set oConn = CreateObject("ADODB.Connection") oConn.Open "Driver={MariaDB ODBC 3.1 Driver}; Server=localhost; Database=hmailserver; User=hmailserver; Password=secretpassword;" If oConn.State <> 1 Then EventLog.Write( "Sub LookupSender - ERROR: Could not connect to database" ) m_Sender = "" Exit Sub End If Set oRecord = oConn.Execute("SELECT metadata_from FROM hm_message_metadata WHERE metadata_messageid = '" & m_ID & "';") Do Until oRecord.EOF m_Sender = oRecord("metadata_from") oRecord.MoveNext Loop oConn.Close Set oRecord = Nothing End Sub
SørenR.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
Re: Metadata by Message-ID
No you don't.
If you use SENDGRID for outbound messages then you are sending bulk. That makes a LOT of messages to save in your Send Mail folder

SørenR.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
-
- Senior user
- Posts: 390
- Joined: 2016-12-08 02:21
Re: Metadata by Message-ID
I only need ephemeral storage for those messages that don't get relayed by SendGrid on the first attempt due to transient failure. All other messages will be cleared immediately via either a delivered notification, or a bounce notification. Those that are simply delayed are the messages I want to notify the originator.
So I'm going to just use the registry:
In my POST handler, one of three actions happens for these temporary registry entries:
1. A delivery notification event deletes the key.
2. A bounce notification event deletes the key (SendGrid generates an NDR for these and HMS relays it to the originator).
3. A delayed notification event uses this metadata to create a delay notification to the originator and sends it via HMS.
So I'm going to just use the registry:
Code: Select all
Sub NewPendingDelivery(oMessage)
const REGKEY = "HKEY_LOCAL_MACHINE\SOFTWARE\hMailServer\Pending"
set oShell = CreateObject("WScript.Shell")
' new key
sNewKey = REGKEY & "\" & oMessage.HeaderValue("Message-ID")
oShell.RegWrite sNewKey, ""
' write metadata to key
oShell.RegWrite sNewKey & "\From", oMessage.HeaderValue("From"), "REG_SZ"
oShell.RegWrite sNewKey & "\Date", oMessage.HeaderValue("Date"), "REG_SZ"
oShell.RegWrite sNewKey & "\To", oMessage.HeaderValue("To"), "REG_SZ"
oShell.RegWrite sNewKey & "\Subject", oMessage.HeaderValue("Subject"), "REG_SZ"
set oShell = nothing
End Sub
1. A delivery notification event deletes the key.
2. A bounce notification event deletes the key (SendGrid generates an NDR for these and HMS relays it to the originator).
3. A delayed notification event uses this metadata to create a delay notification to the originator and sends it via HMS.
Re: Metadata by Message-ID
mikedibella wrote: ↑2021-01-05 20:47I only need ephemeral storage for those messages that don't get relayed by SendGrid on the first attempt due to transient failure. All other messages will be cleared immediately via either a delivered notification, or a bounce notification. Those that are simply delayed are the messages I want to notify the originator.
So I'm going to just use the registry:
In my POST handler, one of three actions happens for these temporary registry entries:Code: Select all
Sub NewPendingDelivery(oMessage) const REGKEY = "HKEY_LOCAL_MACHINE\SOFTWARE\hMailServer\Pending" set oShell = CreateObject("WScript.Shell") ' new key sNewKey = REGKEY & "\" & oMessage.HeaderValue("Message-ID") oShell.RegWrite sNewKey, "" ' write metadata to key oShell.RegWrite sNewKey & "\From", oMessage.HeaderValue("From"), "REG_SZ" oShell.RegWrite sNewKey & "\Date", oMessage.HeaderValue("Date"), "REG_SZ" oShell.RegWrite sNewKey & "\To", oMessage.HeaderValue("To"), "REG_SZ" oShell.RegWrite sNewKey & "\Subject", oMessage.HeaderValue("Subject"), "REG_SZ" set oShell = nothing End Sub
1. A delivery notification event deletes the key.
2. A bounce notification event deletes the key (SendGrid generates an NDR for these and HMS relays it to the originator).
3. A delayed notification event uses this metadata to create a delay notification to the originator and sends it via HMS.

SørenR.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.
Re: Metadata by Message-ID
Good point.

However, an alternative could be to save the messages and then just clear them out sometime after the last delivery attempt time period. Many ways to skin this cat.
Actually, I think that would generate a different message ID now that I think about it, which would also obviate my first post above.


Re: Metadata by Message-ID
This is an interesting concept. Is it faster/lighter than a database?mikedibella wrote: ↑2021-01-05 20:47I only need ephemeral storage for those messages that don't get relayed by SendGrid on the first attempt due to transient failure. All other messages will be cleared immediately via either a delivered notification, or a bounce notification. Those that are simply delayed are the messages I want to notify the originator.
So I'm going to just use the registry:
In my POST handler, one of three actions happens for these temporary registry entries:Code: Select all
Sub NewPendingDelivery(oMessage) const REGKEY = "HKEY_LOCAL_MACHINE\SOFTWARE\hMailServer\Pending" set oShell = CreateObject("WScript.Shell") ' new key sNewKey = REGKEY & "\" & oMessage.HeaderValue("Message-ID") oShell.RegWrite sNewKey, "" ' write metadata to key oShell.RegWrite sNewKey & "\From", oMessage.HeaderValue("From"), "REG_SZ" oShell.RegWrite sNewKey & "\Date", oMessage.HeaderValue("Date"), "REG_SZ" oShell.RegWrite sNewKey & "\To", oMessage.HeaderValue("To"), "REG_SZ" oShell.RegWrite sNewKey & "\Subject", oMessage.HeaderValue("Subject"), "REG_SZ" set oShell = nothing End Sub
1. A delivery notification event deletes the key.
2. A bounce notification event deletes the key (SendGrid generates an NDR for these and HMS relays it to the originator).
3. A delayed notification event uses this metadata to create a delay notification to the originator and sends it via HMS.