Lua Requires Chilkat v11.0.0+
Lua
Add an attachment and an inline image
See more Email Object Examples
Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local email = chilkat.Email.new()
local success = email:load_eml("text-and-html.eml")
if not success then
print(email.last_error_text)
return
end
local data = chilkat.BinData.new()
success = data:append_string("Time,Activity\r\n09:00,Welcome\r\n10:00,Field session\r\n", "utf-8")
if not success then
print("Failed: data.AppendString.")
return
end
success = email:add_attachment_bd("agenda.csv", data, "text/csv")
if not success then
print(email.last_error_text)
return
end
-- AddRelatedBd returns the identifier that must appear after cid: in the HTML.
local image = chilkat.BinData.new()
success = image:append_encoded("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==", "base64")
if not success then
print("Failed: image.AppendEncoded.")
return
end
local cid = email:add_related_bd("confirmed.png", image)
if not email.last_method_success then
print(email.last_error_text)
return
end
local html = chilkat.StringBuilder.new()
html:append("<p><img alt='Confirmed' src=\"cid:")
html:append(cid)
html:append("\"></p>")
html:append(email:get_html_body())
success = email:add_html_alternative_body(html:get_as_string())
if not success then
print(email.last_error_text)
return
end
success = email:save_eml("with-attachments.eml")
if not success then
print(email.last_error_text)
return
end
print("Saved with-attachments.eml.")