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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

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 filename = chilkat.StringBuilder.new()
local mediaType = chilkat.StringBuilder.new()
local found = false
local i = 0
while i < email.num_attachments do
    filename:set_string(email:get_attachment_filename(i))
    mediaType:set_string(email:get_attachment_content_type(i))
    if (filename:contents_equal("agenda.csv", false)) and ((mediaType:contents_equal("text/csv", false)) and (not found)) then
        local data = chilkat.BinData.new()
        success = email:get_attachment_bd(i, data)
        if not success then
            print(email.last_error_text)
            return
        end

        success = data:write_file("received-agenda.csv")
        if not success then
            print("Failed: data.WriteFile.")
            return
        end

        print("Saved attachment: " .. data.num_bytes .. " bytes.")
        found = true
    end

    i = i + 1
end

if not found then
    print("No matching attachment.")
end

i = 0
while i < email.num_related_items do
    print("Related: " .. email:get_related_content_type(i) .. "  Content-ID: " .. email:get_related_content_id(i))
    i = i + 1
end