Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.

local email = chilkat.Email.new()
local success = email:load_eml("with-attachments.eml")
if not success then
    print(email.last_error_text)
    return
end

local preview = chilkat.Email.new()
success = email:make_copy(preview)
if not success then
    print(email.last_error_text)
    return
end

-- Keep related items: the HTML still refers to them.
preview:drop_attachments()
print("Files in original: " .. email.num_attachments .. "  Files in preview: " .. preview.num_attachments)
if not preview:has_html_body() then
    print("This message has no HTML preview.")
    return
end

local html = chilkat.StringBuilder.new()
success = preview:get_html_body_sb(true, html)
if not success then
    print(preview.last_error_text)
    return
end

success = html:write_file("preview.html", "utf-8", false)
if not success then
    print("Failed: html.WriteFile.")
    return
end

print("Saved preview.html.")