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

Preserve the original email and save a modified copy

See more Email Object Examples

Use with-attachments.eml as a stand-in for bytes from an IMAP fetch or a message queue. Preserve those bytes first. Parse them into an Email object, add a local processing header, and serialize the result into a separate buffer. The archived input remains suitable for later byte-level inspection.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local mimeBytes = chilkat.BinData.new()
local success = mimeBytes:load_file("with-attachments.eml")
if not success then
    print("Failed: mimeBytes.LoadFile.")
    return
end

success = mimeBytes:write_file("received-original.eml")
if not success then
    print("Failed: mimeBytes.WriteFile.")
    return
end

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

email:add_header_field("X-Workflow-State", "reviewed")
-- GetMimeBd appends to its destination. Use a fresh buffer for this message.
local rendered = chilkat.BinData.new()
success = email:get_mime_bd(rendered)
if not success then
    print(email.last_error_text)
    return
end

success = rendered:write_file("reviewed.eml")
if not success then
    print("Failed: rendered.WriteFile.")
    return
end

print("Archived received-original.eml and saved reviewed.eml.")