Sample code for 30+ languages & platforms
PowerBuilder

Using Replace Patterns in Email

See more Email Object Examples

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

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_EmailTemplate
oleobject loo_Mailman
string ls_Mime
long li_Count
long i
string ls_Name
oleobject loo_Json
string ls_EmailAddr
string ls_FirstName
string ls_Product

li_Success = 0

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

//  ---------------------------------------------------------------------
//  Create an email template for sending.
loo_EmailTemplate = create oleobject
li_rc = loo_EmailTemplate.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_EmailTemplate
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  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"...
loo_EmailTemplate.Subject = "Hello FIRST_NAME,"

loo_EmailTemplate.From = "john@example.com"
loo_EmailTemplate.AddTo("FIRST_NAME","RECIPIENT_EMAIL")

loo_EmailTemplate.SetHtmlBody("<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:
loo_EmailTemplate.SaveEml("qa_output/emailTemplate.eml")

//  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
//  To: FIRST_NAME <RECIPIENT_EMAIL>
//  
//  <html><body><h2>Hello FIRST_NAME,</h2><p>Your order for PRODUCT_NAME has been shipped.</p></body></html>

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

loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")

loo_EmailTemplate.SetReplacePattern("FIRST_NAME","Elon")
loo_EmailTemplate.SetReplacePattern("RECIPIENT_EMAIL","elon.musk@example.com")
loo_EmailTemplate.SetReplacePattern("PRODUCT_NAME","Twitter Corporation")

//  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.
ls_Mime = loo_Mailman.RenderToMime(loo_EmailTemplate)
Write-Debug ls_Mime

//  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.
li_Count = loo_EmailTemplate.NumReplacePatterns
Write-Debug "Number of replace patterns: " + string(li_Count)

i = 0
do while i < li_Count
    //  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
    Write-Debug loo_EmailTemplate.GetReplacePattern(i) + ": " + loo_EmailTemplate.GetReplaceString(i)
    i = i + 1
loop

//  Or lookup a replacement pattern by name:
ls_Name = "FIRST_NAME"
Write-Debug ls_Name + " = " + loo_EmailTemplate.GetReplaceString2(ls_Name)

//  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..
loo_Mailman.SmtpHost = "smtp.mail.us-west-2.awsapps.com"
loo_Mailman.SmtpSsl = 1
loo_Mailman.SmtpPort = 465

loo_Mailman.SmtpUsername = "john@example.com"
loo_Mailman.SmtpPassword = "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"
//  		}	
//  		...
//  	]
//  }

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Json.LoadFile("qa_data/json/mail_merge.json")
if li_Success = 0 then
    Write-Debug loo_Json.LastErrorText
    destroy loo_EmailTemplate
    destroy loo_Mailman
    destroy loo_Json
    return
end if

i = 0
li_Count = loo_Json.SizeOfArray("mail_merge")
do while i < li_Count
    loo_Json.I = i
    ls_EmailAddr = loo_Json.StringOf("mail_merge[i].to")
    ls_FirstName = loo_Json.StringOf("mail_merge[i].name")
    ls_Product = loo_Json.StringOf("mail_merge[i].product")

    loo_EmailTemplate.SetReplacePattern("FIRST_NAME",ls_FirstName)
    loo_EmailTemplate.SetReplacePattern("RECIPIENT_EMAIL",ls_EmailAddr)
    loo_EmailTemplate.SetReplacePattern("PRODUCT_NAME",ls_Product)

    li_Success = loo_Mailman.SendEmail(loo_EmailTemplate)
    if li_Success = 0 then
        Write-Debug loo_Mailman.LastErrorText
        destroy loo_EmailTemplate
        destroy loo_Mailman
        destroy loo_Json
        return
    end if

    Write-Debug "Send email to " + ls_EmailAddr

    i = i + 1
loop

Write-Debug "Success."


destroy loo_EmailTemplate
destroy loo_Mailman
destroy loo_Json