Visual FoxPro
Visual FoxPro
Amazon SES v2: Send HTML with a plain text alternative
See more Amazon SES Examples
Send a single report notification with an HTML body and a plain text alternative. Each part conveys the same information so the recipient can read it with either kind of mail client. Use a verified sender in the sending Region and grant ses:SendEmail.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loRequest
LOCAL lcRegion
LOCAL loHost
LOCAL loSigner
LOCAL loConnection
LOCAL loRest
LOCAL lcSessionToken
LOCAL lcResponseText
LOCAL loResult
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Replace the From address with your verified sender. The destination is the SES mailbox simulator.
loRequest = CreateObject('Chilkat.JsonObject')
loRequest.UpdateString("FromEmailAddress","sender+reports@example.com")
loRequest.UpdateString("Destination.ToAddresses[0]","success@simulator.amazonses.com")
loRequest.UpdateString("Content.Simple.Subject.Data","Weekly report: 12 orders shipped")
loRequest.UpdateString("Content.Simple.Subject.Charset","UTF-8")
* SES builds a multipart/alternative message from the two body representations.
loRequest.UpdateString("Content.Simple.Body.Text.Charset","UTF-8")
loRequest.UpdateString("Content.Simple.Body.Text.Data","Weekly report: 12 orders shipped.")
loRequest.UpdateString("Content.Simple.Body.Html.Charset","UTF-8")
loRequest.UpdateString("Content.Simple.Body.Html.Data","<h1>Weekly report</h1><p><strong>12 orders</strong> shipped.</p>")
* 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 loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
CANCEL
ENDIF
loRest = CreateObject('Chilkat.Rest')
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
? loRest.LastErrorText
RELEASE loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
CANCEL
ENDIF
lnSuccess = loRest.SetAuthAws(loSigner)
IF (lnSuccess <> 1) THEN
? loRest.LastErrorText
RELEASE loRequest
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
loRest.AddHeader("Content-Type","application/json")
lcResponseText = loRest.FullRequestString("POST","/v2/email/outbound-emails",loRequest.Emit())
IF (loRest.LastMethodSuccess <> 1) THEN
? loRest.LastErrorText
RELEASE loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 200) THEN
? "SES returned HTTP " + STR(loRest.ResponseStatusCode) + " " + lcResponseText
RELEASE loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
CANCEL
ENDIF
loResult = CreateObject('Chilkat.JsonObject')
lnSuccess = loResult.Load(lcResponseText)
IF (lnSuccess <> 1) THEN
? loResult.LastErrorText
RELEASE loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
RELEASE loResult
CANCEL
ENDIF
IF (loResult.HasMember("MessageId") <> 1) THEN
? "SES returned no message ID."
RELEASE loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
RELEASE loResult
CANCEL
ENDIF
? "SES accepted the message. Message ID: " + loResult.StringOf("MessageId")
RELEASE loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
RELEASE loResult