Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.0.0+

Check a published DKIM selector against a signed message

See more DKIM Examples

After publishing a selector, run this check with a message signed by its matching private key. Start with a fresh verifier, fetch the public key from DNS, and then verify the message with that same object. DNS lookup alone shows only that a usable key was returned. Signature verification also checks that it matches what the signer used.

Set the signing domain and selector exactly as they appear in the signature. Applications processing many messages can reuse the verifier for that selector. After a key change, use a new verifier and allow for the DNS resolver’s TTL.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oMessage = ObjCreate("Chilkat.BinData")
$bSuccess = $oMessage.LoadFile("signed.eml")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read signed.eml." & @CRLF)
    Exit
EndIf

$oVerifier = ObjCreate("Chilkat.Dkim")
Local $sDomain = "example.com"
Local $selector = "mail2026"
$bSuccess = $oVerifier.PrefetchPublicKey($selector,$sDomain)
If ($bSuccess <> True) Then
    ConsoleWrite($oVerifier.LastErrorText & @CRLF)
    Exit
EndIf

;  signed.eml from the signing example has our newly added signature at index zero.
If ($oVerifier.NumDkimSigs($oMessage) = 0) Then
    ConsoleWrite("No DKIM signature in signed.eml." & @CRLF)
    Exit
EndIf

$bSuccess = $oVerifier.DkimVerify(0,$oMessage)
If ($bSuccess <> True) Then
    ConsoleWrite($oVerifier.LastErrorText & @CRLF)
    ConsoleWrite($oVerifier.VerifyInfo & @CRLF)
    Exit
EndIf

$oInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oInfo.Load($oVerifier.VerifyInfo)
If (($bSuccess <> True) Or ($oInfo.StringOfEquals("domain",$sDomain,True) <> True) Or ($oInfo.StringOfEquals("selector",$selector,True) <> True)) Then
    ConsoleWrite("The first signature is not from the selected domain and selector." & @CRLF)
    Exit
EndIf

ConsoleWrite("The published key verifies this message for " & $selector & "._domainkey." & $sDomain & @CRLF)