Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False

Local $bCreateTemplate = True
;  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
Local $sRegion = "us-west-2"
$oHost = ObjCreate("Chilkat.StringBuilder")
$oHost.Append("email.")
$oHost.Append($sRegion)
$oHost.Append(".amazonaws.com")

$oSigner = ObjCreate("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 = $sRegion
$oSigner.ServiceName = "ses"

$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$bSuccess = $oConnection.Connect($oHost.GetAsString(),443,True,10000)
If ($bSuccess <> True) Then
    ConsoleWrite($oConnection.LastErrorText & @CRLF)
    Exit
EndIf

$oRest = ObjCreate("Chilkat.Rest")
$bSuccess = $oRest.UseConnection($oConnection,False)
If ($bSuccess <> True) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.SetAuthAws($oSigner)
If ($bSuccess <> True) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

;  For temporary AWS credentials, supply the session token from the same provider.
Local $sessionToken = ""
If ($sessionToken <> "") Then
    $oRest.AddQueryParam("X-Amz-Security-Token",$sessionToken)
EndIf

If ($bCreateTemplate = True) Then
    $oRequest = ObjCreate("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")
Local $setupResponse = $oRest.FullRequestString("POST","/v2/email/templates",$oRequest.Emit())
    If ($oRest.LastMethodSuccess <> True) Then
        ConsoleWrite($oRest.LastErrorText & @CRLF)
        Exit
    EndIf

    If ($oRest.ResponseStatusCode <> 200) Then
        ConsoleWrite("SES returned HTTP " & $oRest.ResponseStatusCode & " " & $setupResponse & @CRLF)
        Exit
    EndIf

    ConsoleWrite("Template created. Set createTemplate to false to preview it." & @CRLF)
    Exit
EndIf

$oOrder = ObjCreate("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.
Local $bTryMissingField = False
If ($bTryMissingField = True) Then
    $oOrder.Delete("orderNumber")
EndIf

$oPreview = ObjCreate("Chilkat.JsonObject")
$oPreview.UpdateString("TemplateData",$oOrder.Emit())

$oRest.AddHeader("Content-Type","application/json")
Local $sResponseText = $oRest.FullRequestString("POST","/v2/email/templates/OrderReceipt/render",$oPreview.Emit())
If ($oRest.LastMethodSuccess <> True) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

If ($oRest.ResponseStatusCode <> 200) Then
    ConsoleWrite("SES returned HTTP " & $oRest.ResponseStatusCode & " " & $sResponseText & @CRLF)
    Exit
EndIf

$oResponse = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oResponse.Load($sResponseText)
If ($bSuccess <> True) Then
    ConsoleWrite($oResponse.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($oResponse.StringOf("RenderedTemplate") & @CRLF)