PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Email
oleobject loo_SbHtml
integer li_NumReplaced
oleobject loo_SbBodyContent
oleobject loo_SbTemplate
integer li_BCrlf
string ls_MyHtmlReplyBody
li_Success = 0
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
destroy loo_Email
MessageBox("Error","Connecting to COM object failed")
return
end if
// 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.
li_Success = loo_Email.LoadEml("qa_data/eml/receivedHtmlEmail.eml")
if li_Success <> 1 then
Write-Debug loo_Email.LastErrorText
destroy loo_Email
return
end if
// If this email doesn't have an HTML body, then do something else...
// (that's for another example maybe?)
if loo_Email.HasHtmlBody() = 0 then
Write-Debug "Email does not have an HTML body.."
destroy loo_Email
return
end if
// Get the HTML body in a StringBuilder..
loo_SbHtml = create oleobject
li_rc = loo_SbHtml.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbHtml.Append(loo_Email.GetHtmlBody())
// 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.
li_NumReplaced = loo_SbHtml.Replace("<BODY","<body")
li_NumReplaced = loo_SbHtml.Replace("</BODY","</body")
// Get the HTML within the "body" open/close tags
loo_SbBodyContent = create oleobject
li_rc = loo_SbBodyContent.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbBodyContent.Append(loo_SbHtml.GetAfterBetween("<body",">","</body>"))
// Replace what we just extracted with a marker..
li_NumReplaced = loo_SbHtml.Replace(loo_SbBodyContent.GetAsString(),"TO_BE_UPDATED")
// Create a template for the new body:
loo_SbTemplate = create oleobject
li_rc = loo_SbTemplate.ConnectToNewObject("Chilkat.StringBuilder")
li_BCrlf = 1
loo_SbTemplate.AppendLine("REPLY_HTML_CONTENT",li_BCrlf)
loo_SbTemplate.AppendLine("<br>",li_BCrlf)
loo_SbTemplate.AppendLine("<div class=~"moz-signature~">Best Regards,<br>",li_BCrlf)
loo_SbTemplate.AppendLine(" John Smith<br>",li_BCrlf)
loo_SbTemplate.AppendLine(" Chilkat Software, Inc.<br>",li_BCrlf)
loo_SbTemplate.AppendLine(" <p>",li_BCrlf)
loo_SbTemplate.AppendLine(" <a href=~"https://twitter.com/chilkatsoft~">Follow Chilkat on",li_BCrlf)
loo_SbTemplate.AppendLine(" Twitter</a>",li_BCrlf)
loo_SbTemplate.AppendLine(" </p>",li_BCrlf)
loo_SbTemplate.AppendLine("</div>",li_BCrlf)
loo_SbTemplate.AppendLine("<div class=~"moz-cite-prefix~">On 2/8/2017 4:25 PM, Chilkat Support",li_BCrlf)
loo_SbTemplate.AppendLine(" wrote:<br>",li_BCrlf)
loo_SbTemplate.AppendLine("</div>",li_BCrlf)
loo_SbTemplate.AppendLine("<blockquote>",li_BCrlf)
loo_SbTemplate.AppendLine("ORIGINAL_BODY_CONTENT",li_BCrlf)
loo_SbTemplate.AppendLine("</blockquote>",li_BCrlf)
loo_SbTemplate.AppendLine("<br>",li_BCrlf)
// Replace the markers in the template with actual HTML.
// First our reply HTML:
ls_MyHtmlReplyBody = "This is my <font color=~"#6600cc~"><b>reply HTML</b></font><br>"
li_NumReplaced = loo_SbTemplate.Replace("REPLY_HTML_CONTENT",ls_MyHtmlReplyBody)
// Now for the original HTML body content:
li_NumReplaced = loo_SbTemplate.Replace("ORIGINAL_BODY_CONTENT",loo_SbBodyContent.GetAsString())
// Insert into sbHtml:
li_NumReplaced = loo_SbHtml.Replace("TO_BE_UPDATED",loo_SbTemplate.GetAsString())
// Use sbHtml as the new HTML body..
loo_Email.SetHtmlBody(loo_SbHtml.GetAsString())
// Examine the result HTML.
Write-Debug loo_SbHtml.GetAsString()
// Save the email.
loo_Email.SaveEml("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.
//
destroy loo_Email
destroy loo_SbHtml
destroy loo_SbBodyContent
destroy loo_SbTemplate