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

Load .eml and Examine the Structure, Attachments, and Related Items

See more Email Object Examples

Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local emlPath = "C:/AAWorkarea/beatrix/roesner.eml"

local mime = chilkat.Mime.new()

local success = mime:load_mime_file(emlPath)
if not success then
    print(mime.last_error_text)
    return
end

print("---- MIME structure ----")
print(mime:get_structure("text"))
print("------------------------")

local email = chilkat.Email.new()
success = email:load_eml(emlPath)

-- Was this a signed and/or encrypted email?
-- If so, then loading the .eml automatically unwraps
-- (i.e. verifies signatures and decrypts) and the resultant
-- email is what existed prior to signing/encrypting.
print("Email was Signed: " .. tostring(email.received_signed))
print("Email was Encrypted: " .. tostring(email.received_encrypted))
if email.received_signed then
    print("Signature(s) valid = " .. tostring(email.signatures_valid))
end

if email.received_encrypted then
    print("Decrypted successfully = " .. tostring(email.decrypted))
end

local i = 0
local numAttach = email.num_attachments
print("Number of attachments = " .. numAttach)

while i < numAttach do
    print("---- Attachment " .. i)

    -- Examine the filename (if any)
    print("filename: " .. email:get_attachment_filename(i))
    -- Examine the content-ID (if any)
    print("Content-ID: " .. email:get_attachment_content_id(i))
    -- Examine the content-type
    print("Content-Type: " .. email:get_attachment_content_type(i))
    -- Examine the content-disposition
    print("Content-Disposition" .. email:get_attachment_header(i, "content-disposition"))
    -- Examine the attachment size:
    print("Size (in bytes) of the attachment: " .. email:get_attachment_size(i))

    i = i + 1
end

print("--")

-- Now for the related items.

-- Note: A MIME sub-part can potentially be both a related item AND an attachment.
-- The typical case is when the item is contained under the multipart/related enclosure and
-- the item also has a "Content-Disposition" header indicating "attachment".
-- The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
-- Related items and attachments are not necessarily mutually exclusive.

local numRelated = email.num_related_items
print("Number of related items = " .. numRelated)
i = 0
while i < numRelated do
    print("---- Related Item " .. i)

    -- Examine the filename (if any)
    print("filename: " .. email:get_related_filename(i))
    -- Examine the content-ID (if any)
    print("Content-ID: " .. email:get_related_content_id(i))
    -- Examine the content-type
    print("Content-Type: " .. email:get_related_content_type(i))
    -- Examine the content-location (if any)
    print("Content-Location" .. email:get_related_content_location(i))

    i = i + 1
end

-- The email could also have attached messages.
-- An attached message is another email that was attached to this email.
local em = chilkat.Email.new()
local numAttachedMessages = email.num_attached_messages
print("Number of attached messages = " .. numAttachedMessages)
i = 0
while i < numAttachedMessages do
    print("---- Attached message " .. i)

    -- Examine the attached email
    email:get_attached_email(i, em)
    print("from: " .. em.from)
    print("subject: " .. em.subject)
    i = i + 1
end

-- An email could also be a multipart/report email.
-- This is a DSN (Delivery Status Notification)
-- The NumReports property indicates how many "reports" exist.
local numReports = email.num_reports
print("Number of reports = " .. numReports)
i = 0
while i < numReports do
    print("---- Report " .. i)
    -- Get the raw report data...
    print(email:get_report(i))
    i = i + 1
end

-- If the email is a multipart/report, then the information
-- from the message/delivery-status part of the email can be retrieved:
if email:is_multipart_report() then
    print("--- Delivery Status Information:")
    print("Status: " .. email:get_delivery_status_info("Status"))
    print("Action: " .. email:get_delivery_status_info("Action"))
    print("Reporting-MTA: " .. email:get_delivery_status_info("Reporting-MTA"))

    local jsonDsnInfo = chilkat.JsonObject.new()
    email:get_dsn_info(jsonDsnInfo)
    jsonDsnInfo.emit_compact = false
    print(jsonDsnInfo:emit())
end