AutoIt
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
; Read the PDF before making a network request.
$oPdf = ObjCreate("Chilkat.BinData")
$bSuccess = $oPdf.LoadFile("invoice.pdf")
If ($bSuccess <> True) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
If ($oPdf.NumBytes = 0) Then
ConsoleWrite("invoice.pdf is empty." & @CRLF)
Exit
EndIf
Local $sender = "sender+reports@example.com"
Local $sRecipient = "success@simulator.amazonses.com"
$oEmail = ObjCreate("Chilkat.Email")
$oEmail.From = $sender
$oEmail.Subject = "Invoice 1042"
$oEmail.Charset = "utf-8"
$bSuccess = $oEmail.AddTo("",$sRecipient)
If ($bSuccess <> True) Then
ConsoleWrite($oEmail.LastErrorText & @CRLF)
Exit
EndIf
$oEmail.AddPlainTextAlternativeBody("Invoice 1042 is attached.")
$oEmail.AddHtmlAlternativeBody("<p>Invoice <strong>1042</strong> is attached.</p>")
$bSuccess = $oEmail.AddAttachmentBd("invoice-1042.pdf",$oPdf,"application/pdf")
If ($bSuccess <> True) Then
ConsoleWrite($oEmail.LastErrorText & @CRLF)
Exit
EndIf
; Get the exact MIME bytes, including the attachment's transfer encoding.
$oMime = ObjCreate("Chilkat.BinData")
$bSuccess = $oEmail.GetMimeBd($oMime)
If ($bSuccess <> True) Then
ConsoleWrite($oEmail.LastErrorText & @CRLF)
Exit
EndIf
; Base64 here transports the entire MIME message as a blob in the JSON request.
$oRequest = ObjCreate("Chilkat.JsonObject")
$oRequest.UpdateString("FromEmailAddress",$sender)
$oRequest.UpdateString("Destination.ToAddresses[0]",$sRecipient)
$oRequest.UpdateString("Content.Raw.Data",$oMime.GetEncoded("base64"))
; Use the same AWS Region for the identity, endpoint, and Signature Version 4.
Local $sRegion = "us-west-2"
$oHost = ObjCreate("Chilkat.StringBuilder")
$oHost.Append("email.")
$oHost.Append($sRegion)
$oHost.Append(".amazonaws.com")
$oSigner = ObjCreate("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 = $sRegion
$oSigner.ServiceName = "ses"
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$bSuccess = $oConnection.Connect($oHost.GetAsString(),443,True,10000)
If ($bSuccess <> True) Then
ConsoleWrite($oConnection.LastErrorText & @CRLF)
Exit
EndIf
$oRest = ObjCreate("Chilkat.Rest")
$bSuccess = $oRest.UseConnection($oConnection,False)
If ($bSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oRest.SetAuthAws($oSigner)
If ($bSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
; For temporary AWS credentials, supply the session token from the same provider.
Local $sessionToken = ""
If ($sessionToken <> "") Then
$oRest.AddQueryParam("X-Amz-Security-Token",$sessionToken)
EndIf
$oRest.AddHeader("Content-Type","application/json")
Local $sResponseText = $oRest.FullRequestString("POST","/v2/email/outbound-emails",$oRequest.Emit())
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
If ($oRest.ResponseStatusCode <> 200) Then
ConsoleWrite("SES returned HTTP " & $oRest.ResponseStatusCode & " " & $sResponseText & @CRLF)
Exit
EndIf
$oResult = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oResult.Load($sResponseText)
If ($bSuccess <> True) Then
ConsoleWrite($oResult.LastErrorText & @CRLF)
Exit
EndIf
If ($oResult.HasMember("MessageId") <> True) Then
ConsoleWrite("SES returned no message ID." & @CRLF)
Exit
EndIf
ConsoleWrite("SES accepted the message. Message ID: " & $oResult.StringOf("MessageId") & @CRLF)