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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

var imap = new CkImap();
imap.Ssl = true;
imap.Port = 993;
imap.RequireSslCertVerify = true;
imap.PeekMode = true;
imap.AutoDownloadAttachments = true;
success = imap.Connect("imap.example.com");
if (success !== true) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD");
if (success !== true) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.SelectMailbox("INBOX");
if (success !== true) {
    console.log(imap.LastErrorText);
    return;
}

//  Select the first ten sequence positions, then fetch by stable UID.
var messages = new CkMessageSet();
success = imap.QueryMbx("1:10",true,messages);
if (success !== true) {
    console.log(imap.LastErrorText);
    return;
}

var i = 0;
var verifier = new CkDkim();
var message = new CkBinData();
while ((i < messages.Count)) {
    var uid = messages.GetId(i);
    console.log("Message UID " + uid);
    success = imap.FetchSingleBd(uid,true,message);
    if (success !== true) {
        console.log(imap.LastErrorText);
    }
    else {
        var count = verifier.NumDkimSigs(message);
        if (count == 0) {
            console.log("No DKIM signatures.");
        }

        var sig = 0;
        while ((sig < count)) {
            success = verifier.DkimVerify(sig,message);
            //  Read diagnostics before the next verification replaces them.
            var info = new CkJsonObject();
            var haveInfo = info.Load(verifier.VerifyInfo);
            if (success == true) {
                console.log("Signature " + sig + " verified.");
            }
            else {
                if ((haveInfo == true) && (info.StringOfEquals("bodyHashVerified","no",true) == true)) {
                    console.log("Signature " + sig + ": the body hash differs.");
                }
                else {
                    console.log("Signature " + sig + " could not be verified.");
                }

                console.log(verifier.LastErrorText);
            }

            if ((haveInfo == true) && (info.JsonTypeOf("domain") == 1)) {
                console.log("Signing domain: " + info.StringOf("domain") + "  Selector: " + info.StringOf("selector"));
            }

            sig = sig+1;
        }

    }

    i = i+1;
}

success = imap.Disconnect();