Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lnCreateTemplate
LOCAL lcRegion
LOCAL loHost
LOCAL loSigner
LOCAL loConnection
LOCAL loRest
LOCAL lcSessionToken
LOCAL loRequest
LOCAL lcSetupResponse
LOCAL loOrder
LOCAL lnTryMissingField
LOCAL loPreview
LOCAL lcResponseText
LOCAL loResponse

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0

lnCreateTemplate = 1
*  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
lcRegion = "us-west-2"
loHost = CreateObject('Chilkat.StringBuilder')
loHost.Append("email.")
loHost.Append(lcRegion)
loHost.Append(".amazonaws.com")

loSigner = CreateObject('Chilkat.AuthAws')
*  Supply the credentials from your application's credential provider.
loSigner.AccessKey = "AWS_ACCESS_KEY_ID"
loSigner.SecretKey = "AWS_SECRET_ACCESS_KEY"
loSigner.Region = lcRegion
loSigner.ServiceName = "ses"

loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
lnSuccess = loConnection.Connect(loHost.GetAsString(),443,1,10000)
IF (lnSuccess <> 1) THEN
    ? loConnection.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    CANCEL
ENDIF

loRest = CreateObject('Chilkat.Rest')
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    CANCEL
ENDIF

lnSuccess = loRest.SetAuthAws(loSigner)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    CANCEL
ENDIF

*  For temporary AWS credentials, supply the session token from the same provider.
lcSessionToken = ""
IF (lcSessionToken <> "") THEN
    loRest.AddQueryParam("X-Amz-Security-Token",lcSessionToken)
ENDIF

IF (lnCreateTemplate = 1) THEN
    loRequest = CreateObject('Chilkat.JsonObject')
    loRequest.UpdateString("TemplateName","OrderReceipt")
    loRequest.UpdateString("TemplateContent.Subject","Receipt for {{orderNumber}}")
    loRequest.UpdateString("TemplateContent.Text","Thank you, {{firstName}}. Order {{orderNumber}}: {{#each items}}{{quantity}} x {{name}}. {{/each}}")
    loRequest.UpdateString("TemplateContent.Html","<p>Thank you, {{firstName}}.</p><ul>{{#each items}}<li>{{quantity}} x {{name}}</li>{{/each}}</ul>")

    loRest.AddHeader("Content-Type","application/json")
    lcSetupResponse = loRest.FullRequestString("POST","/v2/email/templates",loRequest.Emit())
    IF (loRest.LastMethodSuccess <> 1) THEN
        ? loRest.LastErrorText
        RELEASE loHost
        RELEASE loSigner
        RELEASE loConnection
        RELEASE loRest
        RELEASE loRequest
        CANCEL
    ENDIF

    IF (loRest.ResponseStatusCode <> 200) THEN
        ? "SES returned HTTP " + STR(loRest.ResponseStatusCode) + " " + lcSetupResponse
        RELEASE loHost
        RELEASE loSigner
        RELEASE loConnection
        RELEASE loRest
        RELEASE loRequest
        CANCEL
    ENDIF

    ? "Template created. Set createTemplate to false to preview it."
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loRequest
    CANCEL
ENDIF

loOrder = CreateObject('Chilkat.JsonObject')
loOrder.UpdateString("firstName","Avery")
loOrder.UpdateString("orderNumber","1042")
loOrder.UpdateString("items[0].name","Notebook")
loOrder.UpdateInt("items[0].quantity",2)
loOrder.UpdateString("items[1].name","Pen")
loOrder.UpdateInt("items[1].quantity",1)
*  Remove this key to try the missing-field case.
lnTryMissingField = 0
IF (lnTryMissingField = 1) THEN
    loOrder.Delete("orderNumber")
ENDIF

loPreview = CreateObject('Chilkat.JsonObject')
loPreview.UpdateString("TemplateData",loOrder.Emit())

loRest.AddHeader("Content-Type","application/json")
lcResponseText = loRest.FullRequestString("POST","/v2/email/templates/OrderReceipt/render",loPreview.Emit())
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loRequest
    RELEASE loOrder
    RELEASE loPreview
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "SES returned HTTP " + STR(loRest.ResponseStatusCode) + " " + lcResponseText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loRequest
    RELEASE loOrder
    RELEASE loPreview
    CANCEL
ENDIF

loResponse = CreateObject('Chilkat.JsonObject')
lnSuccess = loResponse.Load(lcResponseText)
IF (lnSuccess <> 1) THEN
    ? loResponse.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loRequest
    RELEASE loOrder
    RELEASE loPreview
    RELEASE loResponse
    CANCEL
ENDIF

? loResponse.StringOf("RenderedTemplate")

RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
RELEASE loRequest
RELEASE loOrder
RELEASE loPreview
RELEASE loResponse