Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
imap.require_ssl_cert_verify = true
imap.peek_mode = true
imap.auto_download_attachments = true
local success = imap:connect("imap.example.com")
if not success then
print(imap.last_error_text)
return
end
success = imap:login("IMAP_USERNAME", "IMAP_PASSWORD")
if not success then
print(imap.last_error_text)
return
end
success = imap:select_mailbox("INBOX")
if not success then
print(imap.last_error_text)
return
end
-- Select the first ten sequence positions, then fetch by stable UID.
local messages = chilkat.MessageSet.new()
success = imap:query_mbx("1:10", true, messages)
if not success then
print(imap.last_error_text)
return
end
local i = 0
local verifier = chilkat.Dkim.new()
local message = chilkat.BinData.new()
while i < messages.count do
local uid = messages:get_id(i)
print("Message UID " .. uid)
success = imap:fetch_single_bd(uid, true, message)
if not success then
print(imap.last_error_text)
else
local count = verifier:num_dkim_sigs(message)
if count == 0 then
print("No DKIM signatures.")
end
local sig = 0
while sig < count do
success = verifier:dkim_verify(sig, message)
-- Read diagnostics before the next verification replaces them.
local info = chilkat.JsonObject.new()
local haveInfo = info:load(verifier.verify_info)
if success then
print("Signature " .. sig .. " verified.")
else
if (haveInfo) and (info:string_of_equals("bodyHashVerified", "no", true)) then
print("Signature " .. sig .. ": the body hash differs.")
else
print("Signature " .. sig .. " could not be verified.")
end
print(verifier.last_error_text)
end
if (haveInfo) and (info:json_type_of("domain") == 1) then
print("Signing domain: " .. info:string_of("domain") .. " Selector: " .. info:string_of("selector"))
end
sig = sig + 1
end
end
i = i + 1
end
success = imap:disconnect()