Sample code for 30+ languages & platforms
Lua

Add a MIME Header Field

See more MIME Examples

Demonstrates the Chilkat Mime.AddHeaderField method, which appends a new header field. The first argument is the field name and the second is the value. Existing fields with the same case-insensitive name are preserved, so this can create duplicates.

Background: MIME headers are ordered name/value lines, and some — Received, for instance — legitimately appear more than once. AddHeaderField is the tool for that: it always appends, never replacing an existing field of the same name. When you instead want exactly one field with a given name, use SetHeaderField, which replaces rather than accumulates.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Mime.AddHeaderField method, which appends a new header field.  The 1st argument
-- is the field name and the 2nd is the value.  Existing fields with the same (case-insensitive)
-- name are preserved, so this can create duplicate fields.

local mime = chilkat.Mime.new()

local success = mime:set_body_from_plain_text("Hello.")
if not success then
    print(mime.last_error_text)
    return
end

-- Append header fields.  AddHeaderField does not replace existing fields of the same name.
success = mime:add_header_field("X-Custom-Header", "value1")
if not success then
    print(mime.last_error_text)
    return
end

success = mime:add_header_field("X-Priority", "3")
if not success then
    print(mime.last_error_text)
    return
end

local headers = mime:get_entire_head()
if not mime.last_method_success then
    print(mime.last_error_text)
    return
end

print(headers)