Sample code for 30+ languages & platforms
Lua

Enumerate MIME Header Field Names

See more MIME Examples

Demonstrates the Chilkat Mime.GetHeaderFieldName method, which returns the name of the header-field occurrence at a zero-based index. The only argument is the index; use NumHeaderFields for the count. Original order and casing are preserved.

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

Background: When you need to walk all the headers — to display them, copy them, or handle duplicates that GetHeaderField would hide — index-based access is the way. NumHeaderFields bounds the loop, and this returns each field's name in its original order and casing, which matters when the exact header sequence is significant.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local mime = chilkat.Mime.new()
local success = mime:load_mime_file("qa_data/message.eml")
if not success then
    print(mime.last_error_text)
    return
end

-- Iterate the header fields by index.  NumHeaderFields gives the count; GetHeaderFieldName and
-- GetHeaderFieldValue return the name and value at each zero-based index.  Original order and
-- casing are preserved.
local n = mime.num_header_fields
print("Header fields: " .. n)
for i = 0, n - 1 do
    local fieldName = mime:get_header_field_name(i)
    if not mime.last_method_success then
        print(mime.last_error_text)
        return
    end

    print(fieldName)
end