Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Using Replace Patterns in Email

See more Email Object Examples

Demonstrates how to use the replace patterns (mail-merge) feature in Chilkat MailMan.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vEmailTemplate
    Handle hoEmailTemplate
    Handle hoMailman
    String sMime
    Integer iCount
    Integer i
    String sName
    Handle hoJson
    String sEmailAddr
    String sFirstName
    String sProduct
    String sTemp1
    String sTemp2

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // ---------------------------------------------------------------------
    // Create an email template for sending.
    Get Create (RefClass(cComChilkatEmail)) To hoEmailTemplate
    If (Not(IsComObjectCreated(hoEmailTemplate))) Begin
        Send CreateComObject of hoEmailTemplate
    End

    // We're going to replace "FIRST_NAME" with an actual name.
    // We arbitrarily chose "FIRST_NAME".  We can choose anything, such as "CUSTOMER_NAME" or "THE_RECIPIENT_NAME"...
    Set ComSubject Of hoEmailTemplate To "Hello FIRST_NAME,"

    Set ComFrom Of hoEmailTemplate To "john@example.com"
    Get ComAddTo Of hoEmailTemplate "FIRST_NAME" "RECIPIENT_EMAIL" To iSuccess

    Send ComSetHtmlBody To hoEmailTemplate "<html><body><h2>Hello FIRST_NAME,</h2><p>Your order for PRODUCT_NAME has been shipped.</p></body></html>"

    // If the email is saved to a file, we can see what it contains:
    Get ComSaveEml Of hoEmailTemplate "qa_output/emailTemplate.eml" To iSuccess

    // For example:

    // MIME-Version: 1.0
    // Date: Tue, 26 Apr 2022 07:10:52 -0500
    // Message-ID: <715CF231A9F07B0B9FDB073518CD94138D791866@XYZ>
    // Content-Type: text/html; charset=us-ascii
    // Content-Transfer-Encoding: 7bit
    // X-Priority: 3 (Normal)
    // Subject: Hello FIRST_NAME,
    // From: john@example.com
    // CKX-Bounce-Address: john@example.com
    // To: FIRST_NAME <RECIPIENT_EMAIL>
    // 
    // <html><body><h2>Hello FIRST_NAME,</h2><p>Your order for PRODUCT_NAME has been shipped.</p></body></html>

    // Note: Ignore the CKX-Bounce-Address header.  It is automatically removed by Chilkat prior to sending.
    // All "CKX-" headers are automatically removed prior to sending.

    // ---------------------------------------------------------------------
    // Demonstrate replace patterns by setting and then rendering to MIME.

    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End

    Get ComSetReplacePattern Of hoEmailTemplate "FIRST_NAME" "Elon" To iSuccess
    Get ComSetReplacePattern Of hoEmailTemplate "RECIPIENT_EMAIL" "elon.musk@example.com" To iSuccess
    Get ComSetReplacePattern Of hoEmailTemplate "PRODUCT_NAME" "Twitter Corporation" To iSuccess

    // Render to MIME to see what we get.
    // Note: When the MailMan sends an email, it renders the email to MIME and then sends.
    // The rendering process is to do replacements, or possibly sign, encrypt, etc.
    // When MailMan.SendEmail is called, internally the email is rendered, and the rendered email is sent.
    // The equivalent to MailMan.Send email is to call email.RenderToMime followed by MailMan.SendMime.
    Get pvComObject of hoEmailTemplate to vEmailTemplate
    Get ComRenderToMime Of hoMailman vEmailTemplate To sMime
    Showln sMime

    // This is the rendered MIME:

    // MIME-Version: 1.0
    // Date: Tue, 26 Apr 2022 07:25:49 -0500
    // Message-ID: <750582BCDC891C67B48CEE2293C08B902C3891E9@XYZ>
    // Content-Type: text/html; charset=us-ascii
    // Content-Transfer-Encoding: 7bit
    // X-Priority: 3 (Normal)
    // Subject: Hello Elon,
    // From: john@example.com
    // To: Elon <elon.musk@example.com>
    // 
    // <html><body><h2>Hello Elon,</h2><p>Your order for Twitter Corporation has been shipped.</p></body></html>

    // Note: When rendering, the Date and Message-ID headers are automatically regenerated.

    // ---------------------------------------------------------------------
    // An application can see what replacement patterns it previously set by calling SetReplacePattern multiple times.
    Get ComNumReplacePatterns Of hoEmailTemplate To iCount
    Showln "Number of replace patterns: " iCount

    Move 0 To i
    While (i < iCount)
        // Note: The GetReplaceString method was found to not be working correctly.  It was returning the same value as GetReplacePattern.
        // It is fixed in Chilkat v9.5.0.91
        Get ComGetReplacePattern Of hoEmailTemplate i To sTemp1
        Get ComGetReplaceString Of hoEmailTemplate i To sTemp2
        Showln sTemp1 ": " sTemp2
        Move (i + 1) To i
    Loop

    // Or lookup a replacement pattern by name:
    Move "FIRST_NAME" To sName
    Get ComGetReplaceString2 Of hoEmailTemplate sName To sTemp1
    Showln sName " = " sTemp1

    // Sample output:

    // Number of replace patterns: 3
    // FIRST_NAME: Elon
    // RECIPIENT_EMAIL: elon.musk@example.com
    // PRODUCT_NAME: Twitter Corporation
    // FIRST_NAME = Elon

    // ---------------------------------------------------------------------
    // Finally... demonstrate sending emails using the replacement patterns.
    // 

    // Set our mail server settings..
    Set ComSmtpHost Of hoMailman To "smtp.mail.us-west-2.awsapps.com"
    Set ComSmtpSsl Of hoMailman To True
    Set ComSmtpPort Of hoMailman To 465

    Set ComSmtpUsername Of hoMailman To "john@example.com"
    Set ComSmtpPassword Of hoMailman To "the_password"

    // Imagine we have data in JSON format, and we wish to send the templated email to each recipient...
    // 

    // {
    // 	"mail_merge" : [
    // 		{
    // 			"to": "mary@example.com",
    // 			"name": "Mary",
    // 			"product": "Widget 1" 
    // 		},
    // 		{
    // 			"to": "robert@example.com",
    // 			"name": "Robert",
    // 			"product": "Widget 2"
    // 		}	
    // 		...
    // 	]
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComLoadFile Of hoJson "qa_data/json/mail_merge.json" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move 0 To i
    Get ComSizeOfArray Of hoJson "mail_merge" To iCount
    While (i < iCount)
        Set ComI Of hoJson To i
        Get ComStringOf Of hoJson "mail_merge[i].to" To sEmailAddr
        Get ComStringOf Of hoJson "mail_merge[i].name" To sFirstName
        Get ComStringOf Of hoJson "mail_merge[i].product" To sProduct

        Get ComSetReplacePattern Of hoEmailTemplate "FIRST_NAME" sFirstName To iSuccess
        Get ComSetReplacePattern Of hoEmailTemplate "RECIPIENT_EMAIL" sEmailAddr To iSuccess
        Get ComSetReplacePattern Of hoEmailTemplate "PRODUCT_NAME" sProduct To iSuccess

        Get pvComObject of hoEmailTemplate to vEmailTemplate
        Get ComSendEmail Of hoMailman vEmailTemplate To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoMailman To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Showln "Send email to " sEmailAddr

        Move (i + 1) To i
    Loop

    Showln "Success."


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.