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

Ruby
require 'chilkat'

success = false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

imap = Chilkat::CkImap.new()
imap.put_Ssl(true)
imap.put_Port(993)
imap.put_RequireSslCertVerify(true)
imap.put_PeekMode(true)
imap.put_AutoDownloadAttachments(true)
success = imap.Connect("imap.example.com")
if (success != true)
    print imap.lastErrorText() + "\n";
    exit
end

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
if (success != true)
    print imap.lastErrorText() + "\n";
    exit
end

success = imap.SelectMailbox("INBOX")
if (success != true)
    print imap.lastErrorText() + "\n";
    exit
end

#  Select the first ten sequence positions, then fetch by stable UID.
messages = Chilkat::CkMessageSet.new()
success = imap.QueryMbx("1:10",true,messages)
if (success != true)
    print imap.lastErrorText() + "\n";
    exit
end

i = 0
verifier = Chilkat::CkDkim.new()
message = Chilkat::CkBinData.new()
while (i < messages.get_Count())
    uid = messages.GetId(i)
    print "Message UID " + uid.to_s() + "\n";
    success = imap.FetchSingleBd(uid,true,message)
    if (success != true)
        print imap.lastErrorText() + "\n";
    else
        count = verifier.NumDkimSigs(message)
        if (count == 0)
            print "No DKIM signatures." + "\n";
        end

        sig = 0
        while (sig < count)
            success = verifier.DkimVerify(sig,message)
            #  Read diagnostics before the next verification replaces them.
            info = Chilkat::CkJsonObject.new()
            haveInfo = info.Load(verifier.verifyInfo())
            if (success == true)
                print "Signature " + sig.to_s() + " verified." + "\n";
            else
                if ((haveInfo == true) and (info.StringOfEquals("bodyHashVerified","no",true) == true))
                    print "Signature " + sig.to_s() + ": the body hash differs." + "\n";
                else
                    print "Signature " + sig.to_s() + " could not be verified." + "\n";
                end

                print verifier.lastErrorText() + "\n";
            end

            if ((haveInfo == true) and (info.JsonTypeOf("domain") == 1))
                print "Signing domain: " + info.stringOf("domain") + "  Selector: " + info.stringOf("selector") + "\n";
            end

            sig = sig + 1
        end
    end

    i = i + 1
end
success = imap.Disconnect()