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

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

imap = chilkat.CkImap()
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())
    sys.exit()

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

success = imap.SelectMailbox("INBOX")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

#  Select the first ten sequence positions, then fetch by stable UID.
messages = chilkat.CkMessageSet()
success = imap.QueryMbx("1:10",True,messages)
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

i = 0
verifier = chilkat.CkDkim()
message = chilkat.CkBinData()
while (i < messages.get_Count()) :
    uid = messages.GetId(i)
    print("Message UID " + str(uid))
    success = imap.FetchSingleBd(uid,True,message)
    if (success != True):
        print(imap.lastErrorText())
    else:
        count = verifier.NumDkimSigs(message)
        if (count == 0):
            print("No DKIM signatures.")

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

                print(verifier.lastErrorText())

            if ((haveInfo == True) and (info.JsonTypeOf("domain") == 1)):
                print("Signing domain: " + info.stringOf("domain") + "  Selector: " + info.stringOf("selector"))

            sig = sig + 1

    i = i + 1

success = imap.Disconnect()