Sample code for 30+ languages & platforms
Xbase++

Amazon SES v2: Send a raw MIME email with a PDF attachment

See more Amazon SES Examples

Place invoice.pdf in the working directory. Chilkat builds the MIME message and gives the attachment the name invoice-1042.pdf. The v2 SendEmail request carries those bytes in Content.Raw.Data. For a blind copy, supply the address in Destination.BccAddresses and keep it out of the MIME headers.

Use a verified sender and grant ses:SendEmail for this v2 operation. The v2 message size limit is 40 MB including attachments after MIME encoding. For a straightforward attachment that needs no custom MIME layout, v2 also supports Content.Simple.Attachments.

Sending raw email · SES attachments · Sending quotas

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL cSender
LOCAL cRecipient
LOCAL oEmail
LOCAL oMime
LOCAL oRequest
LOCAL cRegion
LOCAL oHost
LOCAL oSigner
LOCAL oConnection
LOCAL oRest
LOCAL cSessionToken
LOCAL cResponseText
LOCAL oResult

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

nSuccess := 0

//  Read the PDF before making a network request.
oPdf := CreateObject("Chilkat.BinData")
nSuccess := oPdf:LoadFile("invoice.pdf")
IF (nSuccess != 1)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

IF (oPdf:NumBytes == 0)
    ? "invoice.pdf is empty."
    oPdf:destroy()
    RETURN
ENDIF

cSender := "sender+reports@example.com"
cRecipient := "success@simulator.amazonses.com"
oEmail := CreateObject("Chilkat.Email")
oEmail:From := cSender
oEmail:Subject := "Invoice 1042"
oEmail:Charset := "utf-8"
nSuccess := oEmail:AddTo("", cRecipient)
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oPdf:destroy()
    oEmail:destroy()
    RETURN
ENDIF

oEmail:AddPlainTextAlternativeBody("Invoice 1042 is attached.")
oEmail:AddHtmlAlternativeBody("<p>Invoice <strong>1042</strong> is attached.</p>")
nSuccess := oEmail:AddAttachmentBd("invoice-1042.pdf", oPdf, "application/pdf")
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oPdf:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  Get the exact MIME bytes, including the attachment's transfer encoding.
oMime := CreateObject("Chilkat.BinData")
nSuccess := oEmail:GetMimeBd(oMime)
IF (nSuccess != 1)
    ? oEmail:LastErrorText
    oPdf:destroy()
    oEmail:destroy()
    oMime:destroy()
    RETURN
ENDIF

//  Base64 here transports the entire MIME message as a blob in the JSON request.
oRequest := CreateObject("Chilkat.JsonObject")
oRequest:UpdateString("FromEmailAddress", cSender)
oRequest:UpdateString("Destination.ToAddresses[0]", cRecipient)
oRequest:UpdateString("Content.Raw.Data", oMime:GetEncoded("base64"))

//  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
    oPdf:destroy()
    oEmail:destroy()
    oMime: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
    oPdf:destroy()
    oEmail:destroy()
    oMime:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    RETURN
ENDIF

nSuccess := oRest:SetAuthAws(oSigner)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oPdf:destroy()
    oEmail:destroy()
    oMime: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
    oPdf:destroy()
    oEmail:destroy()
    oMime:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    RETURN
ENDIF

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

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

IF (oResult:HasMember("MessageId") != 1)
    ? "SES returned no message ID."
    oPdf:destroy()
    oEmail:destroy()
    oMime:destroy()
    oRequest:destroy()
    oHost:destroy()
    oSigner:destroy()
    oConnection:destroy()
    oRest:destroy()
    oResult:destroy()
    RETURN
ENDIF

? "SES accepted the message. Message ID: " + oResult:StringOf("MessageId")

oPdf:destroy()
oEmail:destroy()
oMime:destroy()
oRequest:destroy()
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oResult:destroy()