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

PureBasic
IncludeFile "CkDkim.pb"
IncludeFile "CkBinData.pb"
IncludeFile "CkImap.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkMessageSet.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    CkImap::setCkRequireSslCertVerify(imap, 1)
    CkImap::setCkPeekMode(imap, 1)
    CkImap::setCkAutoDownloadAttachments(imap, 1)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"IMAP_USERNAME","IMAP_PASSWORD")
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckSelectMailbox(imap,"INBOX")
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Select the first ten sequence positions, then fetch by stable UID.
    messages.i = CkMessageSet::ckCreate()
    If messages.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"1:10",1,messages)
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messages)
        ProcedureReturn
    EndIf

    i.i = 0
    verifier.i = CkDkim::ckCreate()
    If verifier.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    message.i = CkBinData::ckCreate()
    If message.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    While (i < CkMessageSet::ckCount(messages))
        uid.i = CkMessageSet::ckGetId(messages,i)
        Debug "Message UID " + Str(uid)
        success = CkImap::ckFetchSingleBd(imap,uid,1,message)
        If success <> 1
            Debug CkImap::ckLastErrorText(imap)
        Else
            count.i = CkDkim::ckNumDkimSigs(verifier,message)
            If count = 0
                Debug "No DKIM signatures."
            EndIf

            sig.i = 0
            While (sig < count)
                success = CkDkim::ckDkimVerify(verifier,sig,message)
                ;  Read diagnostics before the next verification replaces them.
                info.i = CkJsonObject::ckCreate()
                If info.i = 0
                    Debug "Failed to create object."
                    ProcedureReturn
                EndIf

                haveInfo.i = CkJsonObject::ckLoad(info,CkDkim::ckVerifyInfo(verifier))
                If success = 1
                    Debug "Signature " + Str(sig) + " verified."
                Else
                    If (haveInfo = 1) AND (CkJsonObject::ckStringOfEquals(info,"bodyHashVerified","no",1) = 1)
                        Debug "Signature " + Str(sig) + ": the body hash differs."
                    Else
                        Debug "Signature " + Str(sig) + " could not be verified."
                    EndIf

                    Debug CkDkim::ckLastErrorText(verifier)
                EndIf

                If (haveInfo = 1) AND (CkJsonObject::ckJsonTypeOf(info,"domain") = 1)
                    Debug "Signing domain: " + CkJsonObject::ckStringOf(info,"domain") + "  Selector: " + CkJsonObject::ckStringOf(info,"selector")
                EndIf

                sig = sig + 1
            Wend
        EndIf

        i = i + 1
    Wend
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(messages)
    CkDkim::ckDispose(verifier)
    CkBinData::ckDispose(message)
    CkJsonObject::ckDispose(info)


    ProcedureReturn
EndProcedure