Sample code for 30+ languages & platforms
PowerBuilder

Amazon SES v2: Send private appointment reminders and inspect each result

See more Amazon SES Examples

Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.

SendBulkEmail · Per-entry outcomes

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Request
oleobject loo_FirstReminder
oleobject loo_SecondReminder
string ls_Region
oleobject loo_Host
oleobject loo_Signer
oleobject loo_Connection
oleobject loo_Rest
string ls_SessionToken
string ls_ResponseText
oleobject loo_Response
long li_Count
long i
string ls_Recipient
string ls_Status

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0

loo_Request = create oleobject
li_rc = loo_Request.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Request
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Request.UpdateString("FromEmailAddress","sender@example.com")
loo_Request.UpdateString("DefaultContent.Template.TemplateContent.Subject","Appointment reminder")
loo_Request.UpdateString("DefaultContent.Template.TemplateContent.Text","Hello {{name}}, your appointment is {{slot}}.")
loo_Request.UpdateString("DefaultContent.Template.TemplateData","{~"name~":~"there~",~"slot~":~"in your booking confirmation~"}")

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

loo_FirstReminder.UpdateString("name","Avery")
loo_FirstReminder.UpdateString("slot","Tuesday at 9 AM Central")
loo_Request.UpdateString("BulkEmailEntries[0].Destination.ToAddresses[0]","avery@example.com")
loo_Request.UpdateString("BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData",loo_FirstReminder.Emit())

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

loo_SecondReminder.UpdateString("name","Robin")
loo_SecondReminder.UpdateString("slot","Wednesday at 2 PM Central")
loo_Request.UpdateString("BulkEmailEntries[1].Destination.ToAddresses[0]","robin@example.com")
loo_Request.UpdateString("BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData",loo_SecondReminder.Emit())
//  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
ls_Region = "us-west-2"
loo_Host = create oleobject
li_rc = loo_Host.ConnectToNewObject("Chilkat.StringBuilder")

loo_Host.Append("email.")
loo_Host.Append(ls_Region)
loo_Host.Append(".amazonaws.com")

loo_Signer = create oleobject
li_rc = loo_Signer.ConnectToNewObject("Chilkat.AuthAws")

//  Supply the credentials from your application's credential provider.
loo_Signer.AccessKey = "AWS_ACCESS_KEY_ID"
loo_Signer.SecretKey = "AWS_SECRET_ACCESS_KEY"
loo_Signer.Region = ls_Region
loo_Signer.ServiceName = "ses"

loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
li_Success = loo_Connection.Connect(loo_Host.GetAsString(),443,1,10000)
if li_Success <> 1 then
    Write-Debug loo_Connection.LastErrorText
    destroy loo_Request
    destroy loo_FirstReminder
    destroy loo_SecondReminder
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    return
end if

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Request
    destroy loo_FirstReminder
    destroy loo_SecondReminder
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

li_Success = loo_Rest.SetAuthAws(loo_Signer)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Request
    destroy loo_FirstReminder
    destroy loo_SecondReminder
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

//  For temporary AWS credentials, supply the session token from the same provider.
ls_SessionToken = ""
if ls_SessionToken <> "" then
    loo_Rest.AddQueryParam("X-Amz-Security-Token",ls_SessionToken)
end if

loo_Rest.AddHeader("Content-Type","application/json")
ls_ResponseText = loo_Rest.FullRequestString("POST","/v2/email/outbound-bulk-emails",loo_Request.Emit())
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Request
    destroy loo_FirstReminder
    destroy loo_SecondReminder
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "SES returned HTTP " + string(loo_Rest.ResponseStatusCode) + " " + ls_ResponseText
    destroy loo_Request
    destroy loo_FirstReminder
    destroy loo_SecondReminder
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    return
end if

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

li_Success = loo_Response.Load(ls_ResponseText)
if li_Success <> 1 then
    Write-Debug loo_Response.LastErrorText
    destroy loo_Request
    destroy loo_FirstReminder
    destroy loo_SecondReminder
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Response
    return
end if

li_Count = loo_Response.SizeOfArray("BulkEmailEntryResults")
if li_Count <> loo_Request.SizeOfArray("BulkEmailEntries") then
    Write-Debug "Incomplete results. Reconcile the original batch before retrying."
    destroy loo_Request
    destroy loo_FirstReminder
    destroy loo_SecondReminder
    destroy loo_Host
    destroy loo_Signer
    destroy loo_Connection
    destroy loo_Rest
    destroy loo_Response
    return
end if

i = 0
do while (i < li_Count)
    loo_Response.I = i
    loo_Request.I = i
    ls_Recipient = loo_Request.StringOf("BulkEmailEntries[i].Destination.ToAddresses[0]")
    ls_Status = loo_Response.StringOf("BulkEmailEntryResults[i].Status")
    if ls_Status = "SUCCESS" then
        Write-Debug ls_Recipient + " accepted: " + loo_Response.StringOf("BulkEmailEntryResults[i].MessageId")
    else
        if (ls_Status = "ACCOUNT_THROTTLED") OR (ls_Status = "TRANSIENT_FAILURE") then
            Write-Debug ls_Recipient + " retry later: " + ls_Status
        else
            Write-Debug ls_Recipient + " review before retry: " + ls_Status + " " + loo_Response.StringOf("BulkEmailEntryResults[i].Error")
        end if

    end if

    i = i + 1
loop


destroy loo_Request
destroy loo_FirstReminder
destroy loo_SecondReminder
destroy loo_Host
destroy loo_Signer
destroy loo_Connection
destroy loo_Rest
destroy loo_Response