Sample code for 30+ languages & platforms
Xbase++

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.

SendEmail

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oOrder
LOCAL oRequest
LOCAL cRegion
LOCAL oHost
LOCAL oSigner
LOCAL oConnection
LOCAL oRest
LOCAL cSessionToken
LOCAL cResponseText
LOCAL oResponse

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0

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)
oRequest := CreateObject("Chilkat.JsonObject")
oRequest:UpdateString("FromEmailAddress", "sender@example.com")
oRequest:UpdateString("Destination.ToAddresses[0]", "success@simulator.amazonses.com")
oRequest:UpdateString("Content.Template.TemplateName", "OrderReceipt")
oRequest:UpdateString("Content.Template.TemplateData", oOrder:Emit())
//  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
    oOrder:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    RETURN
ENDIF

oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:UseConnection(oConnection, 0)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oOrder:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    RETURN
ENDIF

nSuccess := oRest:SetAuthAws(oSigner)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oOrder:destroy()
    oRequest:destroy()
    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

oRest:AddHeader("Content-Type", "application/json")
cResponseText := oRest:FullRequestString("POST", "/v2/email/outbound-emails", oRequest:Emit())
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oOrder:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode != 200)
    ? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cResponseText
    oOrder:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    RETURN
ENDIF

oResponse := CreateObject("Chilkat.JsonObject")
nSuccess := oResponse:Load(cResponseText)
IF (nSuccess != 1)
    ? oResponse:LastErrorText
    oOrder:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    oResponse:destroy()
    RETURN
ENDIF

? "SES accepted the message: " + oResponse:StringOf("MessageId")

oOrder:destroy()
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oResponse:destroy()