Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
CkImap_put_RequireSslCertVerify $imap 1
CkImap_put_PeekMode $imap 1
CkImap_put_AutoDownloadAttachments $imap 1
set success [CkImap_Connect $imap "imap.example.com"]
if {$success != 1} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "IMAP_USERNAME" "IMAP_PASSWORD"]
if {$success != 1} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_SelectMailbox $imap "INBOX"]
if {$success != 1} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Select the first ten sequence positions, then fetch by stable UID.
set messages [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "1:10" 1 $messages]
if {$success != 1} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $messages
    exit
}

set i 0
set verifier [new_CkDkim]

set message [new_CkBinData]

while {$i < [CkMessageSet_get_Count $messages]} {
    set uid [CkMessageSet_GetId $messages $i]
    puts "Message UID $uid"
    set success [CkImap_FetchSingleBd $imap $uid 1 $message]
    if {$success != 1} then {
        puts [CkImap_lastErrorText $imap]
    }     else {
        set count [CkDkim_NumDkimSigs $verifier $message]
        if {$count == 0} then {
            puts "No DKIM signatures."
        }

        set sig 0
        while {$sig < $count} {
            set success [CkDkim_DkimVerify $verifier $sig $message]
            #  Read diagnostics before the next verification replaces them.
            set info [new_CkJsonObject]

            set haveInfo [CkJsonObject_Load $info [CkDkim_verifyInfo $verifier]]
            if {$success == 1} then {
                puts "Signature $sig verified."
            }             else {
                if {expr [$haveInfo == 1]  &&  [[CkJsonObject_StringOfEquals $info "bodyHashVerified" "no" 1] == 1]} then {
                    puts "Signature $sig: the body hash differs."
                }                 else {
                    puts "Signature $sig could not be verified."
                }

                puts [CkDkim_lastErrorText $verifier]
            }

            if {expr [$haveInfo == 1]  &&  [[CkJsonObject_JsonTypeOf $info "domain"] == 1]} then {
                puts "Signing domain: [CkJsonObject_stringOf $info domain]  Selector: [CkJsonObject_stringOf $info selector]"
            }

            set sig [expr $sig + 1]
        }
    }

    set i [expr $i + 1]
}
set success [CkImap_Disconnect $imap]

delete_CkImap $imap
delete_CkMessageSet $messages
delete_CkDkim $verifier
delete_CkBinData $message
delete_CkJsonObject $info