Sample code for 30+ languages & platforms
Lua

Send HTML Email with Image Downloaded from URL

Demonstrates how to compose an HTML email with an embedded image where the image data is downloaded from a URL.

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.

-- First download the image we'll be adding to the HTML "img" tag.
local bdJpg = chilkat.BinData.new()
local http = chilkat.Http.new()
local success = http:quick_get_bd("https://www.chilkatsoft.com/images/starfish.jpg", bdJpg)
if not success then
    print(http.last_error_text)
    return
end

local mailman = chilkat.MailMan.new()

-- Use your SMTP server..
mailman.smtp_host = "smtp.yourserver.com"
mailman.smtp_port = 587
mailman.start_tls = true

-- Set the SMTP login/password
mailman.smtp_username = "my_login"
mailman.smtp_password = "my_password"

-- Create an HTML email.
local email = chilkat.Email.new()
email.subject = "HTML Email with Image"
email.from = "Dave <somebody@mydomain.com>"
email:add_to("Chilkat", "info@chilkatsoft.com")

local html = "<html><body><p>This is an HTML email with an embedded image.</p><p><img src=\"starfish.jpg\" /></p></body></html>"
email:set_html_body(html)

-- Note: The "starfish.jpg" here must match the name in the "img" tag's "src" attribute in the HTML above.
success = email:add_related_bd2(bdJpg, "starfish.jpg")
if not success then
    print(email.last_error_text)
    return
end

email:save_eml("qa_output/out.eml")

-- success = mailman.SendEmail(email);
-- if (success == ckfalse) {
--     println mailman.LastErrorText;
--     return;
-- }
--
-- ignore = mailman.CloseSmtpConnection();

print("Success.")