Perl Requires Chilkat v11.0.0+
Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# Assumes Chilkat is unlocked. See Global Unlock Sample.
my $email = Chilkat2::Email->new;
my $success = $email->LoadEml('with-attachments.eml');
if (!$success) {
print $email->LastErrorText, "\n";
exit;
}
my $signer = Chilkat2::Cert->new;
$success = $signer->LoadPfxFile('signer.pfx', 'SIGNER_PASSWORD');
if (!$success) {
print $signer->LastErrorText, "\n";
exit;
}
my $recipient = Chilkat2::Cert->new;
$success = $recipient->LoadFromFile('recipient.cer');
if (!$success) {
print $recipient->LastErrorText, "\n";
exit;
}
$success = $email->SetSigningCert($signer);
if (!$success) {
print $email->LastErrorText, "\n";
exit;
}
$success = $email->SetEncryptCert($recipient);
if (!$success) {
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);
my $mail = Chilkat2::MailMan->new;
my $rendered = Chilkat2::BinData->new;
$success = $mail->RenderToMimeBd($email, $rendered);
if (!$success) {
print $mail->LastErrorText, "\n";
exit;
}
$success = $rendered->WriteFile('signed-encrypted.eml');
if (!$success) {
print "Failed: rendered.WriteFile.\n";
exit;
}
print "Saved signed-encrypted.eml.\n";