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

PHP
<?php

use Chilkat2\BinData;
use Chilkat2\Cert;
use Chilkat2\CertStore;
use Chilkat2\Dkim;
use Chilkat2\JsonObject;
use Chilkat2\PrivateKey;

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

$store = new CertStore();
try {
    $store->OpenCurrentUserStore(true);

    $search = new JsonObject();
    $search->UpdateString('thumbprint', 'YOUR_CERTIFICATE_THUMBPRINT');
    $cert = new Cert();
    $store->FindCert($search, $cert);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

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

$privateKey = new PrivateKey();
try {
    $cert->GetPrivateKey($privateKey);

    $signer = new Dkim();
    $signer->DkimDomain = 'example.com';
    $signer->DkimSelector = 'mail2026';
    $signer->DkimAlg = 'rsa-sha256';
    $signer->SetDkimPrivateKey($privateKey);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$message = new BinData();
try {
    $message->LoadFile('message.eml');
} catch (\Chilkat2\Exception $e) {
    echo "Could not read message.eml.\n";
    exit;
}

try {
    $signer->DkimSign($message);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

try {
    $message->WriteFile('signed.eml');
} catch (\Chilkat2\Exception $e) {
    echo "Could not write signed.eml.\n";
    exit;
}

echo "Saved signed.eml.\n";