Sample code for 30+ languages & platforms
Lua

Send a Bundle of Emails via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendBundle method, which sends each email in an EmailBundle. This is equivalent to calling SendEmail once for each email in the bundle. This example builds a bundle of two messages and sends them.

Background: Sending a bundle lets Chilkat reuse a single SMTP connection for all the messages, avoiding the connect/authenticate overhead of sending them one at a time — the same efficiency as the explicit open/send-loop/close pattern, in one call. It is handy for dispatching a queued batch of distinct messages (as opposed to one message to many recipients).

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the MailMan.SendBundle method, which sends each email in an EmailBundle.
-- This is equivalent to calling SendEmail once for each email in the bundle.

local mailman = chilkat.MailMan.new()

-- Configure the SMTP server connection.
mailman.smtp_host = "smtp.example.com"
mailman.smtp_port = 465
mailman.smtp_ssl = true
mailman.smtp_username = "user@example.com"
mailman.smtp_password = "myPassword"

-- Build a bundle of emails to send.
local bundle = chilkat.EmailBundle.new()

local email1 = chilkat.Email.new()
email1.subject = "Message 1"
email1.from = "alice@example.com"
email1:add_to("Bob", "bob@example.com")
email1.body = "This is the first message."
bundle:add_email(email1)

local email2 = chilkat.Email.new()
email2.subject = "Message 2"
email2.from = "alice@example.com"
email2:add_to("Carol", "carol@example.com")
email2.body = "This is the second message."
bundle:add_email(email2)

-- Send every email in the bundle.

local success = mailman:send_bundle(bundle)
if not success then
    print(mailman.last_error_text)
    return
end

print("Sent " .. bundle.message_count .. " emails.")

-- Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
-- connects and authenticates -- using the property settings above -- whenever a server
-- operation requires it.  Calling the explicit connect/authenticate methods can still be
-- helpful to determine whether a failure occurs while connecting or while authenticating.