Lua Requires Chilkat v11.0.0+
Lua
Reply to an email and preserve its thread
See more Email Object Examples
Create a reply to with-attachments.eml. Address it to Reply-To when supplied, or to the original author otherwise. Link the reply to the original Message-ID, replace its body with a short answer, and print the destination before saving the draft.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local original = chilkat.Email.new()
local success = original:load_eml("with-attachments.eml")
if not success then
print(original.last_error_text)
return
end
local reply = chilkat.Email.new()
success = original:to_reply(reply)
if not success then
print(original.last_error_text)
return
end
reply.from = "Alex Rivera <alex@example.net>"
-- This is a reply to the author, not a reply-all. Set the destination explicitly.
reply:clear_to()
reply:clear_cc()
reply:clear_bcc()
local destination = original:get_header_field("Reply-To")
if destination == "" then
destination = original.from
end
success = reply:add_multiple_to(destination)
if not success then
print(reply.last_error_text)
return
end
-- Link this answer to its parent and preserve the earlier conversation references.
local parentId = original:get_header_field("Message-ID")
if parentId ~= "" then
reply:add_header_field("In-Reply-To", parentId)
local refs = chilkat.StringBuilder.new()
refs:append(original:get_header_field("References"))
if refs.length > 0 then
refs:append(" ")
end
refs:append(parentId)
reply:add_header_field("References", refs:get_as_string())
end
-- A short text reply has no use for the original inline artwork or nested correspondence.
reply:remove_attached_messages()
reply:remove_html_alternative()
reply:drop_related_items()
reply:set_text_body("Thank you for your message.", "text/plain")
if reply.num_to < 1 then
print("Reply has no destination. Review the original sender headers.")
return
end
print("Reply goes to: " .. reply:get_to_addr(0))
print("In-Reply-To: " .. reply:get_header_field("In-Reply-To"))
success = reply:save_eml("reply.eml")
if not success then
print(reply.last_error_text)
return
end
print("Saved reply.eml.")