Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lnConfigureDestination
LOCAL lcSetName
LOCAL lcRegion
LOCAL loHost
LOCAL loSigner
LOCAL loConnection
LOCAL loRest
LOCAL lcSessionToken
LOCAL loPath
LOCAL loDestination
LOCAL lcSetupResponse
LOCAL loRequest
LOCAL lcResponseText
LOCAL loResponse

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0

lnConfigureDestination = 1
lcSetName = "application-mail"
*  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
lcRegion = "us-west-2"
loHost = CreateObject('Chilkat.StringBuilder')
loHost.Append("email.")
loHost.Append(lcRegion)
loHost.Append(".amazonaws.com")

loSigner = CreateObject('Chilkat.AuthAws')
*  Supply the credentials from your application's credential provider.
loSigner.AccessKey = "AWS_ACCESS_KEY_ID"
loSigner.SecretKey = "AWS_SECRET_ACCESS_KEY"
loSigner.Region = lcRegion
loSigner.ServiceName = "ses"

loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
lnSuccess = loConnection.Connect(loHost.GetAsString(),443,1,10000)
IF (lnSuccess <> 1) THEN
    ? loConnection.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    CANCEL
ENDIF

loRest = CreateObject('Chilkat.Rest')
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    CANCEL
ENDIF

lnSuccess = loRest.SetAuthAws(loSigner)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    CANCEL
ENDIF

*  For temporary AWS credentials, supply the session token from the same provider.
lcSessionToken = ""
IF (lcSessionToken <> "") THEN
    loRest.AddQueryParam("X-Amz-Security-Token",lcSessionToken)
ENDIF

IF (lnConfigureDestination = 1) THEN
    loPath = CreateObject('Chilkat.StringBuilder')
    loPath.Append("/v2/email/configuration-sets/")
    loPath.Append(lcSetName)
    loPath.Append("/event-destinations")
    loDestination = CreateObject('Chilkat.JsonObject')
    loDestination.UpdateString("EventDestinationName","delivery-to-sns")
    loDestination.UpdateString("EventDestination.MatchingEventTypes[0]","DELIVERY")
    loDestination.UpdateString("EventDestination.MatchingEventTypes[1]","BOUNCE")
    loDestination.UpdateString("EventDestination.MatchingEventTypes[2]","COMPLAINT")
    loDestination.UpdateString("EventDestination.SnsDestination.TopicArn","arn:aws:sns:us-west-2:123456789012:ses-delivery")
    loDestination.UpdateBool("EventDestination.Enabled",1)

    loRest.AddHeader("Content-Type","application/json")
    lcSetupResponse = loRest.FullRequestString("POST",loPath.GetAsString(),loDestination.Emit())
    IF (loRest.LastMethodSuccess <> 1) THEN
        ? loRest.LastErrorText
        RELEASE loHost
        RELEASE loSigner
        RELEASE loConnection
        RELEASE loRest
        RELEASE loPath
        RELEASE loDestination
        CANCEL
    ENDIF

    IF (loRest.ResponseStatusCode <> 200) THEN
        ? "SES returned HTTP " + STR(loRest.ResponseStatusCode) + " " + lcSetupResponse
        RELEASE loHost
        RELEASE loSigner
        RELEASE loConnection
        RELEASE loRest
        RELEASE loPath
        RELEASE loDestination
        CANCEL
    ENDIF

    ? "Destination created. Use the send branch to test it."
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loPath
    RELEASE loDestination
    CANCEL
ENDIF

loRequest = CreateObject('Chilkat.JsonObject')
loRequest.UpdateString("FromEmailAddress","sender@example.com")
loRequest.UpdateString("Destination.ToAddresses[0]","success@simulator.amazonses.com")
loRequest.UpdateString("Content.Simple.Subject.Data","Delivery event test")
loRequest.UpdateString("Content.Simple.Body.Text.Data","Exercise the application-mail configuration set.")
loRequest.UpdateString("EmailTags[0].Name","notification_ref")
loRequest.UpdateString("EmailTags[0].Value","notify-1042")
loRequest.UpdateString("ConfigurationSetName",lcSetName)

loRest.AddHeader("Content-Type","application/json")
lcResponseText = loRest.FullRequestString("POST","/v2/email/outbound-emails",loRequest.Emit())
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loPath
    RELEASE loDestination
    RELEASE loRequest
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "SES returned HTTP " + STR(loRest.ResponseStatusCode) + " " + lcResponseText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loPath
    RELEASE loDestination
    RELEASE loRequest
    CANCEL
ENDIF

loResponse = CreateObject('Chilkat.JsonObject')
lnSuccess = loResponse.Load(lcResponseText)
IF (lnSuccess <> 1) THEN
    ? loResponse.LastErrorText
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    RELEASE loPath
    RELEASE loDestination
    RELEASE loRequest
    RELEASE loResponse
    CANCEL
ENDIF

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

RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
RELEASE loPath
RELEASE loDestination
RELEASE loRequest
RELEASE loResponse