Sample code for 30+ languages & platforms
Lua

Add S/MIME Signature using PFX

See more MIME Examples

Add a digital signature to a MIME message using the certificate + private key from a PFX file.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local mime = chilkat.Mime.new()

-- Load a PFX file into a certificate object.
local cert = chilkat.Cert.new()
local pfxFilepath = "pfxFiles/something.pfx"
local pfxPassword = "secret"
local success = cert:load_pfx_file(pfxFilepath, pfxPassword)
if not success then
    print(cert.last_error_text)
    return
end

success = mime:set_body_from_plain_text("This is the plain-text MIME body.")

mime.charset = "utf-8"
mime.encoding = "quoted-printable"

-- Sign the MIME (adds a PKCS7 detached signature)
success = mime:add_detached_signature(cert)
if not success then
    print(mime.last_error_text)
    return
end

-- Save the S/MIME to a file.
success = mime:save_mime("/temp/signedMime.txt")
if not success then
    print(mime.last_error_text)
    return
end

print("success!")