Sample code for 30+ languages & platforms
PHP ActiveX Requires Chilkat v11.0.0+

Sign an email with a key in the Windows Current User certificate store

See more DKIM Examples

Run on Windows as the account that owns the signing certificate. Copy its thumbprint from Current User > Personal and supply it as hexadecimal digits. The thumbprint selects a certificate. The DKIM signature itself uses RSA-SHA256.

The certificate needs an accessible, exportable RSA private key for GetPrivateKey. Publish its matching public key under the selector in DNS. DKIM recipients use that DNS key to verify the message. An X.509 chain is unnecessary for this step.

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

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

$store = new COM("Chilkat.CertStore");
$success = $store->OpenCurrentUserStore(1);
if ($success != 1) {
    print $store->LastErrorText . "\n";
    exit;
}

$search = new COM("Chilkat.JsonObject");
$search->UpdateString('thumbprint','YOUR_CERTIFICATE_THUMBPRINT');
$cert = new COM("Chilkat.Cert");
$success = $store->FindCert($search,$cert);
if ($success != 1) {
    print $store->LastErrorText . "\n";
    exit;
}

if ($cert->HasPrivateKey() != 1) {
    print 'The selected certificate has no accessible private key.' . "\n";
    exit;
}

$privateKey = new COM("Chilkat.PrivateKey");
$success = $cert->GetPrivateKey($privateKey);
if ($success != 1) {
    print $cert->LastErrorText . "\n";
    exit;
}

$signer = new COM("Chilkat.Dkim");
$signer->DkimDomain = 'example.com';
$signer->DkimSelector = 'mail2026';
$signer->DkimAlg = 'rsa-sha256';
$success = $signer->SetDkimPrivateKey($privateKey);
if ($success != 1) {
    print $signer->LastErrorText . "\n";
    exit;
}

$message = new COM("Chilkat.BinData");
$success = $message->LoadFile('message.eml');
if ($success != 1) {
    print 'Could not read message.eml.' . "\n";
    exit;
}

$success = $signer->DkimSign($message);
if ($success != 1) {
    print $signer->LastErrorText . "\n";
    exit;
}

$success = $message->WriteFile('signed.eml');
if ($success != 1) {
    print 'Could not write signed.eml.' . "\n";
    exit;
}

print 'Saved signed.eml.' . "\n";

?>