Sample code for 30+ languages & platforms
DataFlex

Load an Email Body from a File

See more Email Object Examples

Demonstrates the Chilkat Email.LoadBodyFromFile method, which loads the email body from a file. The first argument is the file path, the second is true to create an HTML body or false for a plain-text body, and the third is the charset used to interpret the file's bytes. This example loads a plain-text body as utf-8.

Background: Message bodies are often authored and stored as separate files — a marketing team might maintain an HTML template, or a script might generate a text body on the fly. LoadBodyFromFile reads such a file straight into the email, with the isHtml flag telling Chilkat whether to treat it as an HTML or plain-text body and the charset ensuring non-ASCII characters decode correctly.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the LoadBodyFromFile method, which loads the email body from a file.
    //  The first argument is the file path, the second is true to create an HTML body or false for a
    //  plain-text body, and the third is the charset used to interpret the file's bytes.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Body loaded from a file"

    //  Load a plain-text body (isHtml = false) using the utf-8 charset.
    Get ComLoadBodyFromFile Of hoEmail "qa_data/txt/body.txt" False "utf-8" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBody Of hoEmail To sTemp1
    Showln sTemp1

    //  Note: The path "qa_data/..." is a relative local filesystem path,
    //  relative to the current working directory of the running application.


End_Procedure