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

PHP ActiveX
<?php

$success = 0;

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

$email = new COM("Chilkat.Email");
$success = $email->LoadEml('with-attachments.eml');
if ($success != 1) {
    print $email->LastErrorText . "\n";
    exit;
}

$signer = new COM("Chilkat.Cert");
$success = $signer->LoadPfxFile('signer.pfx','SIGNER_PASSWORD');
if ($success != 1) {
    print $signer->LastErrorText . "\n";
    exit;
}

$recipient = new COM("Chilkat.Cert");
$success = $recipient->LoadFromFile('recipient.cer');
if ($success != 1) {
    print $recipient->LastErrorText . "\n";
    exit;
}

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

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

$email->SigningHashAlg = 'sha256';
$email->Pkcs7CryptAlg = 'aes';
$email->Pkcs7KeyLength = 256;
$email->OaepPadding = 1;
$email->OaepHash = 'sha256';
$email->OaepMgfHash = 'sha256';
$email->SendSigned = 1;
$email->SendEncrypted = 1;
$mail = new COM("Chilkat.MailMan");
$rendered = new COM("Chilkat.BinData");
$success = $mail->RenderToMimeBd($email,$rendered);
if ($success != 1) {
    print $mail->LastErrorText . "\n";
    exit;
}

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

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

?>