Perl Requires Chilkat v11.0.0+
Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# Assumes Chilkat is unlocked. See Global Unlock Sample.
my $imap = Chilkat2::Imap->new;
$imap->Ssl(1);
$imap->Port(993);
$imap->RequireSslCertVerify(1);
$imap->PeekMode(1);
$imap->AutoDownloadAttachments(1);
my $success = $imap->Connect('imap.example.com');
if (!$success) {
print $imap->LastErrorText, "\n";
exit;
}
$success = $imap->Login('IMAP_USERNAME', 'IMAP_PASSWORD');
if (!$success) {
print $imap->LastErrorText, "\n";
exit;
}
$success = $imap->SelectMailbox('INBOX');
if (!$success) {
print $imap->LastErrorText, "\n";
exit;
}
# Select the first ten sequence positions, then fetch by stable UID.
my $messages = Chilkat2::MessageSet->new;
$success = $imap->QueryMbx('1:10', 1, $messages);
if (!$success) {
print $imap->LastErrorText, "\n";
exit;
}
my $i = 0;
my $verifier = Chilkat2::Dkim->new;
my $message = Chilkat2::BinData->new;
while ($i < $messages->Count) {
my $uid = $messages->GetId($i);
print 'Message UID ', $uid, "\n";
$success = $imap->FetchSingleBd($uid, 1, $message);
if (!$success) {
print $imap->LastErrorText, "\n";
} else {
my $count = $verifier->NumDkimSigs($message);
if ($count == 0) {
print "No DKIM signatures.\n";
}
my $sig = 0;
while ($sig < $count) {
$success = $verifier->DkimVerify($sig, $message);
# Read diagnostics before the next verification replaces them.
my $info = Chilkat2::JsonObject->new;
my $haveInfo = $info->Load($verifier->VerifyInfo);
if ($success) {
print 'Signature ', $sig, " verified.\n";
} else {
if (($haveInfo) && ($info->StringOfEquals('bodyHashVerified', 'no', 1))) {
print 'Signature ', $sig, ": the body hash differs.\n";
} else {
print 'Signature ', $sig, " could not be verified.\n";
}
print $verifier->LastErrorText, "\n";
}
if (($haveInfo) && ($info->JsonTypeOf('domain') == 1)) {
print 'Signing domain: ', $info->StringOf('domain'), ' Selector: ', $info->StringOf('selector'), "\n";
}
$sig = $sig + 1;
}
}
$i = $i + 1;
}
$success = $imap->Disconnect();