Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loEmail
LOCAL loSbHtml
LOCAL lnNumReplaced
LOCAL loSbBodyContent
LOCAL loSbTemplate
LOCAL lnBCrlf
LOCAL lcMyHtmlReplyBody

lnSuccess = 0

loEmail = CreateObject('Chilkat.Email')

* 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.
lnSuccess = loEmail.LoadEml("qa_data/eml/receivedHtmlEmail.eml")
IF (lnSuccess <> 1) THEN
    ? loEmail.LastErrorText
    RELEASE loEmail
    CANCEL
ENDIF

* If this email doesn't have an HTML body, then do something else...
* (that's for another example maybe?)
IF (loEmail.HasHtmlBody() = 0) THEN
    ? "Email does not have an HTML body.."
    RELEASE loEmail
    CANCEL
ENDIF

* Get the HTML body in a StringBuilder..
loSbHtml = CreateObject('Chilkat.StringBuilder')
loSbHtml.Append(loEmail.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.
lnNumReplaced = loSbHtml.Replace("<BODY","<body")
lnNumReplaced = loSbHtml.Replace("</BODY","</body")

* Get the HTML within the "body" open/close tags
loSbBodyContent = CreateObject('Chilkat.StringBuilder')
loSbBodyContent.Append(loSbHtml.GetAfterBetween("<body",">","</body>"))

* Replace what we just extracted with a marker..
lnNumReplaced = loSbHtml.Replace(loSbBodyContent.GetAsString(),"TO_BE_UPDATED")

* Create a template for the new body:
loSbTemplate = CreateObject('Chilkat.StringBuilder')
lnBCrlf = 1
loSbTemplate.AppendLine("REPLY_HTML_CONTENT",lnBCrlf)
loSbTemplate.AppendLine("<br>",lnBCrlf)
loSbTemplate.AppendLine('<div class="moz-signature">Best Regards,<br>',lnBCrlf)
loSbTemplate.AppendLine("  John Smith<br>",lnBCrlf)
loSbTemplate.AppendLine("  Chilkat Software, Inc.<br>",lnBCrlf)
loSbTemplate.AppendLine("  <p>",lnBCrlf)
loSbTemplate.AppendLine('    <a href="https://twitter.com/chilkatsoft">Follow Chilkat on',lnBCrlf)
loSbTemplate.AppendLine("      Twitter</a>",lnBCrlf)
loSbTemplate.AppendLine("  </p>",lnBCrlf)
loSbTemplate.AppendLine("</div>",lnBCrlf)
loSbTemplate.AppendLine('<div class="moz-cite-prefix">On 2/8/2017 4:25 PM, Chilkat Support',lnBCrlf)
loSbTemplate.AppendLine("  wrote:<br>",lnBCrlf)
loSbTemplate.AppendLine("</div>",lnBCrlf)
loSbTemplate.AppendLine("<blockquote>",lnBCrlf)
loSbTemplate.AppendLine("ORIGINAL_BODY_CONTENT",lnBCrlf)
loSbTemplate.AppendLine("</blockquote>",lnBCrlf)
loSbTemplate.AppendLine("<br>",lnBCrlf)

* Replace the markers in the template with actual HTML.
* First our reply HTML:
lcMyHtmlReplyBody = 'This is my <font color="#6600cc"><b>reply HTML</b></font><br>'
lnNumReplaced = loSbTemplate.Replace("REPLY_HTML_CONTENT",lcMyHtmlReplyBody)

* Now for the original HTML body content:
lnNumReplaced = loSbTemplate.Replace("ORIGINAL_BODY_CONTENT",loSbBodyContent.GetAsString())

* Insert into sbHtml:
lnNumReplaced = loSbHtml.Replace("TO_BE_UPDATED",loSbTemplate.GetAsString())

* Use sbHtml as the new HTML body..
loEmail.SetHtmlBody(loSbHtml.GetAsString())

* Examine the result HTML.
? loSbHtml.GetAsString()

* Save the email.
loEmail.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.
* 

RELEASE loEmail
RELEASE loSbHtml
RELEASE loSbBodyContent
RELEASE loSbTemplate