Xbase++
Xbase++
Amazon SES v2: Create and preview a receipt template
See more Amazon SES Examples
Create the regional OrderReceipt template once. Set createTemplate to false to preview it with sample order data. To exercise the missing-field case, omit orderNumber from the preview data and inspect the rendering error. A successful preview returns MIME content without sending a message.
CreateEmailTemplate · TestRenderEmailTemplate
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL nCreateTemplate
LOCAL cRegion
LOCAL oHost
LOCAL oSigner
LOCAL oConnection
LOCAL oRest
LOCAL cSessionToken
LOCAL oRequest
LOCAL cSetupResponse
LOCAL oOrder
LOCAL nTryMissingField
LOCAL oPreview
LOCAL cResponseText
LOCAL oResponse
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
nCreateTemplate := 1
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
cRegion := "us-west-2"
oHost := CreateObject("Chilkat.StringBuilder")
oHost:Append("email.")
oHost:Append(cRegion)
oHost:Append(".amazonaws.com")
oSigner := CreateObject("Chilkat.AuthAws")
// Supply the credentials from your application's credential provider.
oSigner:AccessKey := "AWS_ACCESS_KEY_ID"
oSigner:SecretKey := "AWS_SECRET_ACCESS_KEY"
oSigner:Region := cRegion
oSigner:ServiceName := "ses"
oConnection := CreateObject("Chilkat.Socket")
oConnection:RequireSslCertVerify := 1
nSuccess := oConnection:Connect(oHost:GetAsString(), 443, 1, 10000)
IF (nSuccess != 1)
? oConnection:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
RETURN
ENDIF
oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:UseConnection(oConnection, 0)
IF (nSuccess != 1)
? oRest:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
nSuccess := oRest:SetAuthAws(oSigner)
IF (nSuccess != 1)
? oRest:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
// For temporary AWS credentials, supply the session token from the same provider.
cSessionToken := ""
IF (cSessionToken != "")
oRest:AddQueryParam("X-Amz-Security-Token", cSessionToken)
ENDIF
IF (nCreateTemplate == 1)
oRequest := CreateObject("Chilkat.JsonObject")
oRequest:UpdateString("TemplateName", "OrderReceipt")
oRequest:UpdateString("TemplateContent.Subject", "Receipt for {{orderNumber}}")
oRequest:UpdateString("TemplateContent.Text", "Thank you, {{firstName}}. Order {{orderNumber}}: {{#each items}}{{quantity}} x {{name}}. {{/each}}")
oRequest:UpdateString("TemplateContent.Html", "<p>Thank you, {{firstName}}.</p><ul>{{#each items}}<li>{{quantity}} x {{name}}</li>{{/each}}</ul>")
oRest:AddHeader("Content-Type", "application/json")
cSetupResponse := oRest:FullRequestString("POST", "/v2/email/templates", oRequest:Emit())
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oRequest:destroy()
RETURN
ENDIF
IF (oRest:ResponseStatusCode != 200)
? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cSetupResponse
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oRequest:destroy()
RETURN
ENDIF
? "Template created. Set createTemplate to false to preview it."
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oRequest:destroy()
RETURN
ENDIF
oOrder := CreateObject("Chilkat.JsonObject")
oOrder:UpdateString("firstName", "Avery")
oOrder:UpdateString("orderNumber", "1042")
oOrder:UpdateString("items[0].name", "Notebook")
oOrder:UpdateInt("items[0].quantity", 2)
oOrder:UpdateString("items[1].name", "Pen")
oOrder:UpdateInt("items[1].quantity", 1)
// Remove this key to try the missing-field case.
nTryMissingField := 0
IF (nTryMissingField == 1)
oOrder:Delete("orderNumber")
ENDIF
oPreview := CreateObject("Chilkat.JsonObject")
oPreview:UpdateString("TemplateData", oOrder:Emit())
oRest:AddHeader("Content-Type", "application/json")
cResponseText := oRest:FullRequestString("POST", "/v2/email/templates/OrderReceipt/render", oPreview:Emit())
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oRequest:destroy()
oOrder:destroy()
oPreview:destroy()
RETURN
ENDIF
IF (oRest:ResponseStatusCode != 200)
? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cResponseText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oRequest:destroy()
oOrder:destroy()
oPreview:destroy()
RETURN
ENDIF
oResponse := CreateObject("Chilkat.JsonObject")
nSuccess := oResponse:Load(cResponseText)
IF (nSuccess != 1)
? oResponse:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oRequest:destroy()
oOrder:destroy()
oPreview:destroy()
oResponse:destroy()
RETURN
ENDIF
? oResponse:StringOf("RenderedTemplate")
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oRequest:destroy()
oOrder:destroy()
oPreview:destroy()
oResponse:destroy()