Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Handle hoSbHtml
    Integer iNumReplaced
    Handle hoSbBodyContent
    Handle hoSbTemplate
    Boolean iBCrlf
    String sMyHtmlReplyBody
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    // 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.
    Get ComLoadEml Of hoEmail "qa_data/eml/receivedHtmlEmail.eml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // If this email doesn't have an HTML body, then do something else...
    // (that's for another example maybe?)
    Get ComHasHtmlBody Of hoEmail To bTemp1
    If (bTemp1 = False) Begin
        Showln "Email does not have an HTML body.."
        Procedure_Return
    End

    // Get the HTML body in a StringBuilder..
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbHtml
    If (Not(IsComObjectCreated(hoSbHtml))) Begin
        Send CreateComObject of hoSbHtml
    End
    Get ComGetHtmlBody Of hoEmail To sTemp1
    Get ComAppend Of hoSbHtml sTemp1 To iSuccess

    // 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.
    Get ComReplace Of hoSbHtml "<BODY" "<body" To iNumReplaced
    Get ComReplace Of hoSbHtml "</BODY" "</body" To iNumReplaced

    // Get the HTML within the "body" open/close tags
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbBodyContent
    If (Not(IsComObjectCreated(hoSbBodyContent))) Begin
        Send CreateComObject of hoSbBodyContent
    End
    Get ComGetAfterBetween Of hoSbHtml "<body" ">" "</body>" To sTemp1
    Get ComAppend Of hoSbBodyContent sTemp1 To iSuccess

    // Replace what we just extracted with a marker..
    Get ComGetAsString Of hoSbBodyContent To sTemp1
    Get ComReplace Of hoSbHtml sTemp1 "TO_BE_UPDATED" To iNumReplaced

    // Create a template for the new body:
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbTemplate
    If (Not(IsComObjectCreated(hoSbTemplate))) Begin
        Send CreateComObject of hoSbTemplate
    End
    Move True To iBCrlf
    Get ComAppendLine Of hoSbTemplate "REPLY_HTML_CONTENT" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "<br>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate '<div class="moz-signature">Best Regards,<br>' iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "  John Smith<br>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "  Chilkat Software, Inc.<br>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "  <p>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate '    <a href="https://twitter.com/chilkatsoft">Follow Chilkat on' iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "      Twitter</a>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "  </p>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "</div>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate '<div class="moz-cite-prefix">On 2/8/2017 4:25 PM, Chilkat Support' iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "  wrote:<br>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "</div>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "<blockquote>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "ORIGINAL_BODY_CONTENT" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "</blockquote>" iBCrlf To iSuccess
    Get ComAppendLine Of hoSbTemplate "<br>" iBCrlf To iSuccess

    // Replace the markers in the template with actual HTML.
    // First our reply HTML:
    Move 'This is my <font color="#6600cc"><b>reply HTML</b></font><br>' To sMyHtmlReplyBody
    Get ComReplace Of hoSbTemplate "REPLY_HTML_CONTENT" sMyHtmlReplyBody To iNumReplaced

    // Now for the original HTML body content:
    Get ComGetAsString Of hoSbBodyContent To sTemp1
    Get ComReplace Of hoSbTemplate "ORIGINAL_BODY_CONTENT" sTemp1 To iNumReplaced

    // Insert into sbHtml:
    Get ComGetAsString Of hoSbTemplate To sTemp1
    Get ComReplace Of hoSbHtml "TO_BE_UPDATED" sTemp1 To iNumReplaced

    // Use sbHtml as the new HTML body..
    Get ComGetAsString Of hoSbHtml To sTemp1
    Send ComSetHtmlBody To hoEmail sTemp1

    // Examine the result HTML.
    Get ComGetAsString Of hoSbHtml To sTemp1
    Showln sTemp1

    // Save the email.
    Get ComSaveEml Of hoEmail "qa_output/emailWithUpdatedHtmlBody.eml" To iSuccess

    // 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.
    // 


End_Procedure