Sample code for 30+ languages & platforms
Lua

Create a multipart/alternative MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.NewMultipartAlternative method, which initializes the object as an empty multipart/alternative entity. It takes no arguments; the alternative representations are then appended.

Background: multipart/alternative holds several renderings of the same content and lets the client pick the best one it can display — the standard way to send an email as both plain text and HTML. Order matters: parts go from least to most preferred, so the plain-text version comes first and the HTML last, and a client shows the last one it understands.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Demonstrates the Mime.NewMultipartAlternative method, which initializes the object as an empty
-- multipart/alternative entity, clearing any existing content.  It takes no arguments.

local mime = chilkat.Mime.new()

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

-- multipart/alternative holds two representations of the SAME content; the client displays the
-- last one it can render.  Add a plain-text part, then an HTML part.
local textPart = chilkat.Mime.new()
success = textPart:set_body_from_plain_text("Hello (plain text version).")
if not success then
    print(textPart.last_error_text)
    return
end

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

local htmlPart = chilkat.Mime.new()
success = htmlPart:set_body_from_html("<html><body><b>Hello</b> (HTML version).</body></html>")
if not success then
    print(htmlPart.last_error_text)
    return
end

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

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