PureBasic
PureBasic
Amazon SES v2: Publish delivery events to SNS and correlate a tagged send
See more Amazon SES Examples
Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.
Configure the SNS destination · SES event payload
Chilkat PureBasic Downloads
IncludeFile "CkSocket.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkRest.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
configureDestination.i = 1
setName.s = "application-mail"
; Use the same AWS Region for the identity, endpoint, and Signature Version 4.
region.s = "us-west-2"
host.i = CkStringBuilder::ckCreate()
If host.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(host,"email.")
CkStringBuilder::ckAppend(host,region)
CkStringBuilder::ckAppend(host,".amazonaws.com")
signer.i = CkAuthAws::ckCreate()
If signer.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Supply the credentials from your application's credential provider.
CkAuthAws::setCkAccessKey(signer, "AWS_ACCESS_KEY_ID")
CkAuthAws::setCkSecretKey(signer, "AWS_SECRET_ACCESS_KEY")
CkAuthAws::setCkRegion(signer, region)
CkAuthAws::setCkServiceName(signer, "ses")
connection.i = CkSocket::ckCreate()
If connection.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSocket::setCkRequireSslCertVerify(connection, 1)
success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(host),443,1,10000)
If success <> 1
Debug CkSocket::ckLastErrorText(connection)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
ProcedureReturn
EndIf
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkRest::ckUseConnection(rest,connection,0)
If success <> 1
Debug CkRest::ckLastErrorText(rest)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
success = CkRest::ckSetAuthAws(rest,signer)
If success <> 1
Debug CkRest::ckLastErrorText(rest)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
; For temporary AWS credentials, supply the session token from the same provider.
sessionToken.s = ""
If sessionToken <> ""
CkRest::ckAddQueryParam(rest,"X-Amz-Security-Token",sessionToken)
EndIf
If configureDestination = 1
path.i = CkStringBuilder::ckCreate()
If path.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(path,"/v2/email/configuration-sets/")
CkStringBuilder::ckAppend(path,setName)
CkStringBuilder::ckAppend(path,"/event-destinations")
destination.i = CkJsonObject::ckCreate()
If destination.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(destination,"EventDestinationName","delivery-to-sns")
CkJsonObject::ckUpdateString(destination,"EventDestination.MatchingEventTypes[0]","DELIVERY")
CkJsonObject::ckUpdateString(destination,"EventDestination.MatchingEventTypes[1]","BOUNCE")
CkJsonObject::ckUpdateString(destination,"EventDestination.MatchingEventTypes[2]","COMPLAINT")
CkJsonObject::ckUpdateString(destination,"EventDestination.SnsDestination.TopicArn","arn:aws:sns:us-west-2:123456789012:ses-delivery")
CkJsonObject::ckUpdateBool(destination,"EventDestination.Enabled",1)
CkRest::ckAddHeader(rest,"Content-Type","application/json")
setupResponse.s = CkRest::ckFullRequestString(rest,"POST",CkStringBuilder::ckGetAsString(path),CkJsonObject::ckEmit(destination))
If CkRest::ckLastMethodSuccess(rest) <> 1
Debug CkRest::ckLastErrorText(rest)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(path)
CkJsonObject::ckDispose(destination)
ProcedureReturn
EndIf
If CkRest::ckResponseStatusCode(rest) <> 200
Debug "SES returned HTTP " + Str(CkRest::ckResponseStatusCode(rest)) + " " + setupResponse
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(path)
CkJsonObject::ckDispose(destination)
ProcedureReturn
EndIf
Debug "Destination created. Use the send branch to test it."
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(path)
CkJsonObject::ckDispose(destination)
ProcedureReturn
EndIf
request.i = CkJsonObject::ckCreate()
If request.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(request,"FromEmailAddress","sender@example.com")
CkJsonObject::ckUpdateString(request,"Destination.ToAddresses[0]","success@simulator.amazonses.com")
CkJsonObject::ckUpdateString(request,"Content.Simple.Subject.Data","Delivery event test")
CkJsonObject::ckUpdateString(request,"Content.Simple.Body.Text.Data","Exercise the application-mail configuration set.")
CkJsonObject::ckUpdateString(request,"EmailTags[0].Name","notification_ref")
CkJsonObject::ckUpdateString(request,"EmailTags[0].Value","notify-1042")
CkJsonObject::ckUpdateString(request,"ConfigurationSetName",setName)
CkRest::ckAddHeader(rest,"Content-Type","application/json")
responseText.s = CkRest::ckFullRequestString(rest,"POST","/v2/email/outbound-emails",CkJsonObject::ckEmit(request))
If CkRest::ckLastMethodSuccess(rest) <> 1
Debug CkRest::ckLastErrorText(rest)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(path)
CkJsonObject::ckDispose(destination)
CkJsonObject::ckDispose(request)
ProcedureReturn
EndIf
If CkRest::ckResponseStatusCode(rest) <> 200
Debug "SES returned HTTP " + Str(CkRest::ckResponseStatusCode(rest)) + " " + responseText
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(path)
CkJsonObject::ckDispose(destination)
CkJsonObject::ckDispose(request)
ProcedureReturn
EndIf
response.i = CkJsonObject::ckCreate()
If response.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(response,responseText)
If success <> 1
Debug CkJsonObject::ckLastErrorText(response)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(path)
CkJsonObject::ckDispose(destination)
CkJsonObject::ckDispose(request)
CkJsonObject::ckDispose(response)
ProcedureReturn
EndIf
Debug "SES accepted the message: " + CkJsonObject::ckStringOf(response,"MessageId")
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(path)
CkJsonObject::ckDispose(destination)
CkJsonObject::ckDispose(request)
CkJsonObject::ckDispose(response)
ProcedureReturn
EndProcedure