Sample code for 30+ languages & platforms
DataFlex

Define a Mail-Merge Replacement Pattern

See more Email Object Examples

Demonstrates the Chilkat Email.SetReplacePattern method, which defines a mail-merge replacement pair. The first argument is the pattern to find and the second is the replacement text. When the email is sent, the patterns are replaced in the bodies and header fields. This example defines two pairs and prints the count.

Background: This is Chilkat's built-in mail merge. You compose one message with placeholder tokens (like FIRST_NAME or CITY), register each token and its value with SetReplacePattern, and the substitutions are applied at send time — personalizing a bulk mailing without building a separate message for every recipient. Read the pairs back with GetReplacePattern and GetReplaceString.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    Boolean iSuccess
    Integer iTemp1

    //  Demonstrates the SetReplacePattern method, which defines a mail-merge replacement pair.
    //  The first argument is the pattern to find and the second is the replacement text.  When the email is sent,
    //  the patterns are replaced in the bodies and header fields.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Hello FIRST_NAME"
    Set ComBody Of hoEmail To "Dear FIRST_NAME, welcome to CITY."

    //  Define two mail-merge replacement pairs.
    Get ComSetReplacePattern Of hoEmail "FIRST_NAME" "John" To iSuccess
    Get ComSetReplacePattern Of hoEmail "CITY" "Denver" To iSuccess

    Get ComNumReplacePatterns Of hoEmail To iTemp1
    Showln "NumReplacePatterns = " iTemp1


End_Procedure