PowerShell
PowerShell
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 PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = $false
$createTemplate = $true
# Use the same AWS Region for the identity, endpoint, and Signature Version 4.
$region = "us-west-2"
$host = New-Object Chilkat.StringBuilder
$host.Append("email.")
$host.Append($region)
$host.Append(".amazonaws.com")
$signer = New-Object Chilkat.AuthAws
# Supply the credentials from your application's credential provider.
$signer.AccessKey = "AWS_ACCESS_KEY_ID"
$signer.SecretKey = "AWS_SECRET_ACCESS_KEY"
$signer.Region = $region
$signer.ServiceName = "ses"
$connection = New-Object Chilkat.Socket
$connection.RequireSslCertVerify = $true
$success = $connection.Connect($host.GetAsString(),443,$true,10000)
if ($success -ne $true) {
$($connection.LastErrorText)
exit
}
$rest = New-Object Chilkat.Rest
$success = $rest.UseConnection($connection,$false)
if ($success -ne $true) {
$($rest.LastErrorText)
exit
}
$success = $rest.SetAuthAws($signer)
if ($success -ne $true) {
$($rest.LastErrorText)
exit
}
# For temporary AWS credentials, supply the session token from the same provider.
$sessionToken = ""
if ($sessionToken -ne "") {
$rest.AddQueryParam("X-Amz-Security-Token",$sessionToken)
}
if ($createTemplate -eq $true) {
$request = New-Object Chilkat.JsonObject
$request.UpdateString("TemplateName","OrderReceipt")
$request.UpdateString("TemplateContent.Subject","Receipt for {{orderNumber}}")
$request.UpdateString("TemplateContent.Text","Thank you, {{firstName}}. Order {{orderNumber}}: {{#each items}}{{quantity}} x {{name}}. {{/each}}")
$request.UpdateString("TemplateContent.Html","<p>Thank you, {{firstName}}.</p><ul>{{#each items}}<li>{{quantity}} x {{name}}</li>{{/each}}</ul>")
$rest.AddHeader("Content-Type","application/json")
$setupResponse = $rest.FullRequestString("POST","/v2/email/templates",$request.Emit())
if ($rest.LastMethodSuccess -ne $true) {
$($rest.LastErrorText)
exit
}
if ($rest.ResponseStatusCode -ne 200) {
$("SES returned HTTP " + $rest.ResponseStatusCode + " " + $setupResponse)
exit
}
$("Template created. Set createTemplate to false to preview it.")
exit
}
$order = New-Object Chilkat.JsonObject
$order.UpdateString("firstName","Avery")
$order.UpdateString("orderNumber","1042")
$order.UpdateString("items[0].name","Notebook")
$order.UpdateInt("items[0].quantity",2)
$order.UpdateString("items[1].name","Pen")
$order.UpdateInt("items[1].quantity",1)
# Remove this key to try the missing-field case.
$tryMissingField = $false
if ($tryMissingField -eq $true) {
$order.Delete("orderNumber")
}
$preview = New-Object Chilkat.JsonObject
$preview.UpdateString("TemplateData",$order.Emit())
$rest.AddHeader("Content-Type","application/json")
$responseText = $rest.FullRequestString("POST","/v2/email/templates/OrderReceipt/render",$preview.Emit())
if ($rest.LastMethodSuccess -ne $true) {
$($rest.LastErrorText)
exit
}
if ($rest.ResponseStatusCode -ne 200) {
$("SES returned HTTP " + $rest.ResponseStatusCode + " " + $responseText)
exit
}
$response = New-Object Chilkat.JsonObject
$success = $response.Load($responseText)
if ($success -ne $true) {
$($response.LastErrorText)
exit
}
$($response.StringOf("RenderedTemplate"))