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

Verify each DKIM signature in email downloaded over IMAP

See more DKIM Examples

Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.

Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.PeekMode = True
$oImap.AutoDownloadAttachments = True
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login("IMAP_USERNAME","IMAP_PASSWORD")
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.SelectMailbox("INBOX")
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Select the first ten sequence positions, then fetch by stable UID.
$oMessages = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("1:10",True,$oMessages)
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

Local $i = 0
$oVerifier = ObjCreate("Chilkat.Dkim")
$oMessage = ObjCreate("Chilkat.BinData")
While ($i < $oMessages.Count)
Local $iUid = $oMessages.GetId($i)
    ConsoleWrite("Message UID " & $iUid & @CRLF)
    $bSuccess = $oImap.FetchSingleBd($iUid,True,$oMessage)
    If ($bSuccess <> True) Then
        ConsoleWrite($oImap.LastErrorText & @CRLF)
    Else
Local $iCount = $oVerifier.NumDkimSigs($oMessage)
        If ($iCount = 0) Then
            ConsoleWrite("No DKIM signatures." & @CRLF)
        EndIf

Local $iSig = 0
        While ($iSig < $iCount)
            $bSuccess = $oVerifier.DkimVerify($iSig,$oMessage)
            ;  Read diagnostics before the next verification replaces them.
            $oInfo = ObjCreate("Chilkat.JsonObject")
Local $bHaveInfo = $oInfo.Load($oVerifier.VerifyInfo)
            If ($bSuccess = True) Then
                ConsoleWrite("Signature " & $iSig & " verified." & @CRLF)
            Else
                If (($bHaveInfo = True) And ($oInfo.StringOfEquals("bodyHashVerified","no",True) = True)) Then
                    ConsoleWrite("Signature " & $iSig & ": the body hash differs." & @CRLF)
                Else
                    ConsoleWrite("Signature " & $iSig & " could not be verified." & @CRLF)
                EndIf

                ConsoleWrite($oVerifier.LastErrorText & @CRLF)
            EndIf

            If (($bHaveInfo = True) And ($oInfo.JsonTypeOf("domain") = 1)) Then
                ConsoleWrite("Signing domain: " & $oInfo.StringOf("domain") & "  Selector: " & $oInfo.StringOf("selector") & @CRLF)
            EndIf

            $iSig = $iSig + 1
        Wend
    EndIf

    $i = $i + 1
Wend
$bSuccess = $oImap.Disconnect()