Sample code for 30+ languages & platforms
Lua

Save Email Attachments to Filesystem

Saves email attachments to a directory.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local email = chilkat.Email.new()

-- Load an email object containing attachments.
-- This .eml can be downloaded from:
-- http://www.example-code.com/testData/HtmlEmail.eml

local success = email:load_eml("HtmlEmail.eml")
if not success then
    print(email.last_error_text)
    return
end

-- If OverwriteExisting is turned on, files with the same
-- name are overwritten.  If turned off, new/unique filenames
-- are automatically generated.  The filenames actually saved
-- are accessible via the GetAttachmentFilename method.
email.overwrite_existing = true

-- Save all attachments to the "myAttachments" subdirectory
-- found under the calling process's current working directory.
-- This directory is automatically created if it does not already
-- exist.
success = email:save_all_attachments("myAttachments")
if not success then
    print(email.last_error_text)
    return
end

-- List the attachment filenames:
for i = 0, email.num_attachments - 1 do
    print(email:get_attachment_filename(i))
end