Sample code for 30+ languages & platforms
Lua

Attach a File to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPartFromFile method, which reads a local file, creates an attachment part, and appends it. The only argument is the file path; the content headers are chosen from the filename extension.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the one-call way to add an attachment: Chilkat reads the file, infers its Content-Type from the extension, base64-encodes binary content, and appends it as a child part — typically inside a multipart/mixed. It is the building block behind adding attachments to an email, saving you from constructing the part and setting its headers by hand.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Mime.AppendPartFromFile method, which reads a local file, creates an attachment
-- part, and appends it.  The only argument is the file path; the content headers are chosen from
-- the filename extension.

local mime = chilkat.Mime.new()
local success = mime:new_multipart_mixed()
if not success then
    print(mime.last_error_text)
    return
end

-- A message body part.
local bodyPart = chilkat.Mime.new()
success = bodyPart:set_body_from_plain_text("Please see the attached files.")
if not success then
    print(bodyPart.last_error_text)
    return
end

success = mime:append_part(bodyPart)
if not success then
    print(mime.last_error_text)
    return
end

-- Attach files directly.
success = mime:append_part_from_file("qa_data/report.pdf")
if not success then
    print(mime.last_error_text)
    return
end

success = mime:append_part_from_file("qa_data/photo.jpg")
if not success then
    print(mime.last_error_text)
    return
end

print("Number of parts: " .. mime.num_parts)