PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkSocket.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkBinData.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
; Read the PDF before making a network request.
pdf.i = CkBinData::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckLoadFile(pdf,"invoice.pdf")
If success <> 1
Debug CkBinData::ckLastErrorText(pdf)
CkBinData::ckDispose(pdf)
ProcedureReturn
EndIf
If CkBinData::ckNumBytes(pdf) = 0
Debug "invoice.pdf is empty."
CkBinData::ckDispose(pdf)
ProcedureReturn
EndIf
sender.s = "sender+reports@example.com"
recipient.s = "success@simulator.amazonses.com"
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkEmail::setCkFrom(email, sender)
CkEmail::setCkSubject(email, "Invoice 1042")
CkEmail::setCkCharset(email, "utf-8")
success = CkEmail::ckAddTo(email,"",recipient)
If success <> 1
Debug CkEmail::ckLastErrorText(email)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
CkEmail::ckAddPlainTextAlternativeBody(email,"Invoice 1042 is attached.")
CkEmail::ckAddHtmlAlternativeBody(email,"<p>Invoice <strong>1042</strong> is attached.</p>")
success = CkEmail::ckAddAttachmentBd(email,"invoice-1042.pdf",pdf,"application/pdf")
If success <> 1
Debug CkEmail::ckLastErrorText(email)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
; Get the exact MIME bytes, including the attachment's transfer encoding.
mime.i = CkBinData::ckCreate()
If mime.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkEmail::ckGetMimeBd(email,mime)
If success <> 1
Debug CkEmail::ckLastErrorText(email)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
ProcedureReturn
EndIf
; Base64 here transports the entire MIME message as a blob in the JSON request.
request.i = CkJsonObject::ckCreate()
If request.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(request,"FromEmailAddress",sender)
CkJsonObject::ckUpdateString(request,"Destination.ToAddresses[0]",recipient)
CkJsonObject::ckUpdateString(request,"Content.Raw.Data",CkBinData::ckGetEncoded(mime,"base64"))
; 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)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
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)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
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)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
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
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)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
If CkRest::ckResponseStatusCode(rest) <> 200
Debug "SES returned HTTP " + Str(CkRest::ckResponseStatusCode(rest)) + " " + responseText
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
result.i = CkJsonObject::ckCreate()
If result.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(result,responseText)
If success <> 1
Debug CkJsonObject::ckLastErrorText(result)
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndIf
If CkJsonObject::ckHasMember(result,"MessageId") <> 1
Debug "SES returned no message ID."
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndIf
Debug "SES accepted the message. Message ID: " + CkJsonObject::ckStringOf(result,"MessageId")
CkBinData::ckDispose(pdf)
CkEmail::ckDispose(email)
CkBinData::ckDispose(mime)
CkJsonObject::ckDispose(request)
CkStringBuilder::ckDispose(host)
CkAuthAws::ckDispose(signer)
CkSocket::ckDispose(connection)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndProcedure