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

Sign and encrypt an email with S/MIME

See more Email Object Examples

Use the sender’s signing PFX and the recipient’s public RSA certificate with with-attachments.eml, which has one recipient. Supply your own signing-key password from a credential store. Render to bytes to apply both S/MIME operations without connecting to SMTP.

Chilkat CkPhp Downloads

CkPhp
<?php

include("chilkat.php");

$success = false;

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

$email = new CkEmail();
$success = $email->LoadEml('with-attachments.eml');
if ($success != true) {
    print $email->lastErrorText() . "\n";
    exit;
}

$signer = new CkCert();
$success = $signer->LoadPfxFile('signer.pfx','SIGNER_PASSWORD');
if ($success != true) {
    print $signer->lastErrorText() . "\n";
    exit;
}

$recipient = new CkCert();
$success = $recipient->LoadFromFile('recipient.cer');
if ($success != true) {
    print $recipient->lastErrorText() . "\n";
    exit;
}

$success = $email->SetSigningCert($signer);
if ($success != true) {
    print $email->lastErrorText() . "\n";
    exit;
}

$success = $email->SetEncryptCert($recipient);
if ($success != true) {
    print $email->lastErrorText() . "\n";
    exit;
}

$email->put_SigningHashAlg('sha256');
$email->put_Pkcs7CryptAlg('aes');
$email->put_Pkcs7KeyLength(256);
$email->put_OaepPadding(true);
$email->put_OaepHash('sha256');
$email->put_OaepMgfHash('sha256');
$email->put_SendSigned(true);
$email->put_SendEncrypted(true);
$mail = new CkMailMan();
$rendered = new CkBinData();
$success = $mail->RenderToMimeBd($email,$rendered);
if ($success != true) {
    print $mail->lastErrorText() . "\n";
    exit;
}

$success = $rendered->WriteFile('signed-encrypted.eml');
if ($success != true) {
    print 'Failed: rendered.WriteFile.' . "\n";
    exit;
}

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

?>