Sample code for 30+ languages & platforms
Lua

Create HTML Email Reply

See more Email Object Examples

The goal of this example is to create a new HTML body where the new reply HTML is at the top, and the original HTML body is below.

The .eml files for this example are available at HTML Reply Email Samples (.eml)

Note: This is only one of a million ways to do it..

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local email = chilkat.Email.new()

-- To create this example, I used Mozilla Thunderbird (with a GMail accont)
-- to first send a simple HTML email to my chilkatsoft.com email address.  I then
-- used Thunderbird to reply with more HTML.
-- I then saved both emails as .eml files.  These are available at
-- http://chilkatdownload.com/example_data/htmlreplyemail.zip

-- Load the .eml for the 1st email that was received. This is the original
-- HTML email before the reply HTML is added.
local success = email:load_eml("qa_data/eml/receivedHtmlEmail.eml")
if not success then
    print(email.last_error_text)
    return
end

-- If this email doesn't have an HTML body, then do something else...
-- (that's for another example maybe?)
if not email:has_html_body() then
    print("Email does not have an HTML body..")
    return
end

-- Get the HTML body in a StringBuilder..
local sbHtml = chilkat.StringBuilder.new()
sbHtml:append(email:get_html_body())

-- The HTML body contains this:
-- <html><head>
-- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
--   </head>
--   <body bgcolor="#FFFFFF" text="#000000">
--     <font face="Calibri"><br>
--       This is a test <font color="#3366ff"><i>HTML email</i></font>...<br>
--       <br>
--       <br>
--     </font>
--   </body>
-- </html>

-- Make sure the BODY tags are lowercase.
local numReplaced = sbHtml:replace("<BODY", "<body")
numReplaced = sbHtml:replace("</BODY", "</body")

-- Get the HTML within the "body" open/close tags
local sbBodyContent = chilkat.StringBuilder.new()
sbBodyContent:append(sbHtml:get_after_between("<body", ">", "</body>"))

-- Replace what we just extracted with a marker..
numReplaced = sbHtml:replace(sbBodyContent:get_as_string(), "TO_BE_UPDATED")

-- Create a template for the new body:
local sbTemplate = chilkat.StringBuilder.new()
local bCrlf = true
sbTemplate:append_line("REPLY_HTML_CONTENT", bCrlf)
sbTemplate:append_line("<br>", bCrlf)
sbTemplate:append_line("<div class=\"moz-signature\">Best Regards,<br>", bCrlf)
sbTemplate:append_line("  John Smith<br>", bCrlf)
sbTemplate:append_line("  Chilkat Software, Inc.<br>", bCrlf)
sbTemplate:append_line("  <p>", bCrlf)
sbTemplate:append_line("    <a href=\"https://twitter.com/chilkatsoft\">Follow Chilkat on", bCrlf)
sbTemplate:append_line("      Twitter</a>", bCrlf)
sbTemplate:append_line("  </p>", bCrlf)
sbTemplate:append_line("</div>", bCrlf)
sbTemplate:append_line("<div class=\"moz-cite-prefix\">On 2/8/2017 4:25 PM, Chilkat Support", bCrlf)
sbTemplate:append_line("  wrote:<br>", bCrlf)
sbTemplate:append_line("</div>", bCrlf)
sbTemplate:append_line("<blockquote>", bCrlf)
sbTemplate:append_line("ORIGINAL_BODY_CONTENT", bCrlf)
sbTemplate:append_line("</blockquote>", bCrlf)
sbTemplate:append_line("<br>", bCrlf)

-- Replace the markers in the template with actual HTML.
-- First our reply HTML:
local myHtmlReplyBody = "This is my <font color=\"#6600cc\"><b>reply HTML</b></font><br>"
numReplaced = sbTemplate:replace("REPLY_HTML_CONTENT", myHtmlReplyBody)

-- Now for the original HTML body content:
numReplaced = sbTemplate:replace("ORIGINAL_BODY_CONTENT", sbBodyContent:get_as_string())

-- Insert into sbHtml:
numReplaced = sbHtml:replace("TO_BE_UPDATED", sbTemplate:get_as_string())

-- Use sbHtml as the new HTML body..
email:set_html_body(sbHtml:get_as_string())

-- Examine the result HTML.
print(sbHtml:get_as_string())

-- Save the email.
email:save_eml("qa_output/emailWithUpdatedHtmlBody.eml")

-- Note: To finish creating the reply email, modify the To/From/CC headers,
-- the subject, etc.  You may also with to iterate over the headers (from 1 to NumHeaderFields)
-- and selectively remove all except the particular headers you wish to keep.
-- You should keep: MIME-Version, Content-Type, Message-ID.  Chilkat will automaticaly
-- generate and replace the Message-ID at the time of sending.  You'll want to replace
-- the Date header with the current date/time.
--