Lua Requires Chilkat v11.0.0+
Lua
Sign an email and send the signed bytes over SMTP
See more DKIM Examples
Compose the email, render it to MIME once, add its DKIM signature, and submit those same bytes to an SMTP server. Put blind-copy recipients in the SMTP envelope only so their addresses stay out of the visible headers.
Use an authorized SMTP account and your own sender domain. Publish the selector’s matching public key first. If your relay rewrites signed content, have it apply the signature after its final edits. A successful SMTP submission means the relay accepted the message. A recipient can then verify it with the public key in DNS.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local mail = chilkat.MailMan.new()
mail.smtp_host = "smtp.example.com"
mail.smtp_port = 465
mail.smtp_ssl = true
mail.require_ssl_cert_verify = true
mail.smtp_username = "SMTP_USERNAME"
mail.smtp_password = "SMTP_PASSWORD"
local email = chilkat.Email.new()
email.from = "Workshop team <updates@example.com>"
email.subject = "Workshop registration"
email.body = "Registration opens on Monday."
local success = email:add_to("Newsletter editor", "editor@example.net")
if not success then
print(email.last_error_text)
return
end
-- Render after all headers, recipients, and content are final.
local message = chilkat.BinData.new()
success = mail:render_to_mime_bd(email, message)
if not success then
print(mail.last_error_text)
return
end
-- Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
local privateKey = chilkat.PrivateKey.new()
success = privateKey:load_any_format_file("dkim-private.pem", "")
if not success then
print(privateKey.last_error_text)
return
end
local signer = chilkat.Dkim.new()
signer.dkim_domain = "example.com"
signer.dkim_selector = "mail2026"
signer.dkim_alg = "rsa-sha256"
success = signer:set_dkim_private_key(privateKey)
if not success then
print(signer.last_error_text)
return
end
success = signer:dkim_sign(message)
if not success then
print(signer.last_error_text)
return
end
-- archive@example.com is an envelope-only blind copy.
local recipients = "editor@example.net,archive@example.com"
success = mail:send_mime_bd("updates@example.com", recipients, message)
if not success then
print(mail.last_error_text)
return
end
print("SMTP relay accepted the signed message.")
success = mail:close_smtp_connection()
if not success then
print("The SMTP session did not close cleanly.")
end