Sample code for 30+ languages & platforms
Visual FoxPro

Amazon SES v2: Attach a PDF to a simple email

See more Amazon SES Examples

Place invoice.pdf in the working directory and use a verified sender. RawContent contains the Base64 representation of the file bytes for JSON transport. ContentTransferEncoding tells SES how to encode the PDF when it assembles the outgoing MIME message. For custom MIME structure, see Send a raw MIME email.

SES attachments · SendEmail

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loRequest
LOCAL lcRegion
LOCAL loHost
LOCAL loSigner
LOCAL loConnection
LOCAL loRest
LOCAL lcSessionToken
LOCAL lcResponseText
LOCAL loResponse

lnSuccess = 0

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

loPdf = CreateObject('Chilkat.BinData')
lnSuccess = loPdf.LoadFile("invoice.pdf")
IF (lnSuccess <> 1) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

IF (loPdf.NumBytes = 0) THEN
    ? "invoice.pdf is empty."
    RELEASE loPdf
    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","Invoice 1042")
loRequest.UpdateString("Content.Simple.Body.Text.Data","Your invoice is attached.")
loRequest.UpdateString("Content.Simple.Attachments[0].FileName","invoice-1042.pdf")
loRequest.UpdateString("Content.Simple.Attachments[0].ContentType","application/pdf")
loRequest.UpdateString("Content.Simple.Attachments[0].ContentDisposition","ATTACHMENT")
loRequest.UpdateString("Content.Simple.Attachments[0].ContentTransferEncoding","BASE64")
loRequest.UpdateString("Content.Simple.Attachments[0].RawContent",loPdf.GetEncoded("base64"))
*  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 loPdf
    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 loPdf
    RELEASE loRequest
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    CANCEL
ENDIF

lnSuccess = loRest.SetAuthAws(loSigner)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loPdf
    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 loPdf
    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 loPdf
    RELEASE loRequest
    RELEASE loHost
    RELEASE loSigner
    RELEASE loConnection
    RELEASE loRest
    CANCEL
ENDIF

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

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

RELEASE loPdf
RELEASE loRequest
RELEASE loHost
RELEASE loSigner
RELEASE loConnection
RELEASE loRest
RELEASE loResponse