Lua
Lua
Send Email to Distribution List
See more SMTP Examples
Sends the same email to a list of 1000 email addresses in 50 sends where each email has 20 recipients.Note: Chilkat is not intended nor designed for mass emailing. A solution such as this might be used for a corporate emailing to employees, or an emailing to newsletter subscribers.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local mailman = chilkat.MailMan.new()
mailman.smtp_host = "smtp.mymailserver.com"
mailman.smtp_port = 465
mailman.smtp_ssl = true
mailman.smtp_username = "myUsername"
mailman.smtp_password = "myPassword"
-- Create a new email object
local email = chilkat.Email.new()
email.subject = "This is a test"
email.body = "This is a test"
email.from = "Senders Name <sender@example.com>"
email:add_to("Subscribers", "subscribers@example.com")
local bdMime = chilkat.BinData.new()
mailman:render_to_mime_bd(email, bdMime)
-- Load a file containing one email address per line.
local distList = chilkat.StringTable.new()
local success = distList:append_from_file(1000, "utf-8", "qa_data/distList.txt")
if not success then
print(distList.last_error_text)
return
end
local sbRecipients = chilkat.StringBuilder.new()
local i = 0
local szDistList = distList.count
local j = 0
while i < szDistList do
-- Build a list of comma-separated recipients.
if j > 0 then
sbRecipients:append(",")
end
sbRecipients:append(distList:string_at(i))
i = i + 1
j = j + 1
-- If we have 20 recipients, or we have the final recipient in the final chunk, then send.
if (j == 20) or (i == szDistList) then
success = mailman:send_mime_bd("sender@example.com", sbRecipients:get_as_string(), bdMime)
if not success then
print(mailman.last_error_text)
return
end
j = 0
sbRecipients:clear()
end
end
success = mailman:close_smtp_connection()
if not success then
print("Connection to SMTP server not closed cleanly.")
end
print("Email sent to distirbution list.")