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

Lua
local chilkat = require("chilkat")

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

local email = chilkat.Email.new()
local success = email:load_eml("with-attachments.eml")
if not success then
    print(email.last_error_text)
    return
end

local signer = chilkat.Cert.new()
success = signer:load_pfx_file("signer.pfx", "SIGNER_PASSWORD")
if not success then
    print(signer.last_error_text)
    return
end

local recipient = chilkat.Cert.new()
success = recipient:load_from_file("recipient.cer")
if not success then
    print(recipient.last_error_text)
    return
end

success = email:set_signing_cert(signer)
if not success then
    print(email.last_error_text)
    return
end

success = email:set_encrypt_cert(recipient)
if not success then
    print(email.last_error_text)
    return
end

email.signing_hash_alg = "sha256"
email.pkcs7_crypt_alg = "aes"
email.pkcs7_key_length = 256
email.oaep_padding = true
email.oaep_hash = "sha256"
email.oaep_mgf_hash = "sha256"
email.send_signed = true
email.send_encrypted = true
local mail = chilkat.MailMan.new()
local rendered = chilkat.BinData.new()
success = mail:render_to_mime_bd(email, rendered)
if not success then
    print(mail.last_error_text)
    return
end

success = rendered:write_file("signed-encrypted.eml")
if not success then
    print("Failed: rendered.WriteFile.")
    return
end

print("Saved signed-encrypted.eml.")