On April 4, 2025, Amazon Simple Email Service (SES) announced the addition of attachment support through the SES v2 API's "Simple" send type. This new feature enables customers to easily attach PDF documents to emails or embed images for inline display within the email content, eliminating the need for recipients to download them separately.
This article provides a detailed guide on how to send emails with attachments using the "Simple" send type.
Steps to Send Attachments Using the "Simple" Send Type
Suppose you have a PDF file named document.pdf
and want to create an SES email request file called request-send-email.json
.
Here are the steps:
Step 1: Prepare the Template JSON File
Create a template file named request-template.json
with the following content:
{
"FromEmailAddress": "sender-email-address",
"Destination": {
"ToAddresses": [
"recipient-email-address"
]
},
"Content": {
"Simple": {
"Subject": {
"Data": "Email with Attachment"
},
"Body": {
"Text": {
"Data": "Please review the attached document."
}
},
"Attachments": [
{
"RawContent": "",
"ContentDisposition": "ATTACHMENT",
"FileName": "document.pdf",
"ContentDescription": "PDF Document Attachment",
"ContentTransferEncoding": "BASE64"
}
]
}
}
}
Note: Replace "sender-email-address"
and "recipient-email-address"
with your actual email addresses.
Step 2: Generate the Final JSON File
Method 1: Manually Convert and Embed Base64 Encoding
-
Convert the PDF file to Base64 encoding:
base64 -w 0 document.pdf > pdf-encoded.txt
-
View the generated Base64-encoded content:
cat pdf-encoded.txt
-
Open request-send-email.json
in a text editor (e.g., vim
) and manually paste the Base64 content into "RawContent": "<base64-encoded-content>"
.
Method 2: Automate Base64 Embedding with a Script
Since Base64 encoding for PDFs or images can be lengthy and prone to errors during manual copying, we recommend using the following script:
-
Install jq
(if not already installed):
sudo yum install -y jq
-
Convert the PDF to Base64 and embed it into the JSON:
CONTENT=$(base64 -w 0 document.pdf)
jq --arg content "$CONTENT" '.Content.Simple.Attachments[0].RawContent = $content' request-template.json > request-send-email.json
After this step, request-send-email.json
will contain the complete Base64-encoded content.
Step 3: Send the Email Using AWS CLI
Run the following command to send the email with the attachment via AWS CLI:
aws sesv2 send-email --cli-input-json file://request-send-email.json
How to Handle JPG Files?
The process for JPG files is identical; you only need to update the filename and description. For example, modify the attachment section in the JSON as follows:
"FileName": "image.jpg",
"ContentDescription": "JPG Image Attachment"
The Base64 encoding command remains unchanged:
CONTENT=$(base64 -w 0 image.jpg)
jq --arg content "$CONTENT" '.Content.Simple.Attachments[0].RawContent = $content' request-template.json > request-send-email.json
Conclusion
With the SES v2 API's "Simple" send type, users can now effortlessly send emails with attachments. This enhancement significantly boosts SES's flexibility, making it ideal for scenarios requiring document or image delivery. Whether you choose manual operations or script automation, sending emails with attachments is now efficient and straightforward.
References
- Amazon SES Attachment Sending API Update Announcement
- SES Official Documentation: Sending Attachments