PHP
Chilkat2 PHP
S/MIME Encrypt .eml without Sending
See more Email Object Examples
Demonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.Chilkat PHP Downloads
<?php
use Chilkat2\Cert;
use Chilkat2\Email;
use Chilkat2\MailMan;
use Chilkat2\StringBuilder;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
$email = new Email();
try {
$email->LoadEml('c:/temp/email/unencrypted.eml');
// The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption.
$email->Pkcs7CryptAlg = 'aes-gcm';
$email->Pkcs7KeyLength = 256;
$email->OaepPadding = true;
$email->OaepHash = 'sha256';
$email->OaepMgfHash = 'sha256';
$cert = new Cert();
$cert->LoadFromFile('c/temps/cert/recipient.cer');
$email->SendEncrypted = true;
$email->SetEncryptCert($cert);
$sbSmime = new StringBuilder();
// The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object.
// No email is sent.
$mailman = new MailMan();
$mailman->RenderToMimeSb($email, $sbSmime);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
try {
$sbSmime->WriteFile('c:/temp/encryptedEmail.eml', 'utf-8', false);
} catch (\Chilkat2\Exception $e) {
echo $mailman->LastErrorText, "\n";
exit;
}
echo "Success!\n";