Go
Go
Amazon SES v2: Send a personalized receipt from a stored template
See more Amazon SES Examples
First create and preview the OrderReceipt template in the sending Region. Supply a verified sender. Build the order as structured data, then serialize it once into the string required by TemplateData. A message ID records acceptance. Rendering failures can be reported through SES event publishing.
Chilkat Go Downloads
success := false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
order := chilkat.NewJsonObject()
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)
request := chilkat.NewJsonObject()
request.UpdateString("FromEmailAddress","sender@example.com")
request.UpdateString("Destination.ToAddresses[0]","success@simulator.amazonses.com")
request.UpdateString("Content.Template.TemplateName","OrderReceipt")
request.UpdateString("Content.Template.TemplateData",*order.Emit())
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
region := "us-west-2"
host := chilkat.NewStringBuilder()
host.Append("email.")
host.Append(region)
host.Append(".amazonaws.com")
signer := chilkat.NewAuthAws()
// Supply the credentials from your application's credential provider.
signer.SetAccessKey("AWS_ACCESS_KEY_ID")
signer.SetSecretKey("AWS_SECRET_ACCESS_KEY")
signer.SetRegion(region)
signer.SetServiceName("ses")
connection := chilkat.NewSocket()
connection.SetRequireSslCertVerify(true)
success = connection.Connect(*host.GetAsString(),443,true,10000)
if success != true {
fmt.Println(connection.LastErrorText())
order.DisposeJsonObject()
request.DisposeJsonObject()
host.DisposeStringBuilder()
signer.DisposeAuthAws()
connection.DisposeSocket()
return
}
rest := chilkat.NewRest()
success = rest.UseConnection(connection,false)
if success != true {
fmt.Println(rest.LastErrorText())
order.DisposeJsonObject()
request.DisposeJsonObject()
host.DisposeStringBuilder()
signer.DisposeAuthAws()
connection.DisposeSocket()
rest.DisposeRest()
return
}
success = rest.SetAuthAws(signer)
if success != true {
fmt.Println(rest.LastErrorText())
order.DisposeJsonObject()
request.DisposeJsonObject()
host.DisposeStringBuilder()
signer.DisposeAuthAws()
connection.DisposeSocket()
rest.DisposeRest()
return
}
// For temporary AWS credentials, supply the session token from the same provider.
sessionToken := ""
if sessionToken != "" {
rest.AddQueryParam("X-Amz-Security-Token",sessionToken)
}
rest.AddHeader("Content-Type","application/json")
responseText := rest.FullRequestString("POST","/v2/email/outbound-emails",*request.Emit())
if rest.LastMethodSuccess() != true {
fmt.Println(rest.LastErrorText())
order.DisposeJsonObject()
request.DisposeJsonObject()
host.DisposeStringBuilder()
signer.DisposeAuthAws()
connection.DisposeSocket()
rest.DisposeRest()
return
}
if rest.ResponseStatusCode() != 200 {
fmt.Println("SES returned HTTP ", rest.ResponseStatusCode(), " ", *responseText)
order.DisposeJsonObject()
request.DisposeJsonObject()
host.DisposeStringBuilder()
signer.DisposeAuthAws()
connection.DisposeSocket()
rest.DisposeRest()
return
}
response := chilkat.NewJsonObject()
success = response.Load(*responseText)
if success != true {
fmt.Println(response.LastErrorText())
order.DisposeJsonObject()
request.DisposeJsonObject()
host.DisposeStringBuilder()
signer.DisposeAuthAws()
connection.DisposeSocket()
rest.DisposeRest()
response.DisposeJsonObject()
return
}
fmt.Println("SES accepted the message: ", *response.StringOf("MessageId"))
order.DisposeJsonObject()
request.DisposeJsonObject()
host.DisposeStringBuilder()
signer.DisposeAuthAws()
connection.DisposeSocket()
rest.DisposeRest()
response.DisposeJsonObject()