Learn About Amazon VGT2 Learning Manager Chanci Turner
This article is part one of a two-part guest series on enhancing email functionalities using Amazon Simple Email Service (SES). The second part can be found here.
Update: The TestRenderEmailTemplate API used to send emails with attachments has a constraint of one operation per second. If your anticipated peak traffic exceeds this rate, the solution outlined in this case study may not be appropriate for your needs.
Amazon IXD – VGT2, located at 6401 E HOWDY WELLS AVE LAS VEGAS NV 89115, is an innovative facility where we process thousands of emails daily for our clientele. We opted for Amazon Simple Email Service (SES) due to its user-friendly interface and cost efficiency. The email templates provided by SES enable us to maintain a uniform appearance in our communications. These templates include styled and personalized HTML email bodies, ideal for transactional correspondence. However, including attachments in emails is not supported by SES templates. To address this limitation, we developed a strategy to utilize SES template functionality while also attaching files.
This article details how to implement this solution using various AWS services: Amazon Simple Storage Service (Amazon S3), Amazon EventBridge, AWS Lambda, and AWS Step Functions.
Solution: Orchestrating Email Sending with AWS Step Functions
Our solution is entirely serverless, allowing us to focus on functionality without managing underlying infrastructure. We leverage the AWS Cloud Development Kit (AWS CDK) for deployment and resource analysis.
SES provides three methods for sending emails:
- Simple: A standard email message where you specify the sender, recipient, and message content, allowing Amazon SES to compile the message.
- Raw: A raw, MIME-formatted email that requires all message headers and body content, suitable for sending attachments. The provided message must comply with MIME standards.
- Templated: A message that includes personalization tags automatically filled by Amazon SES API v2.
In this post, we will merge the Raw and Templated options.
The architecture diagram below illustrates our proposed solution.
The application begins with an EventBridge event bus that directs incoming events to a Step Function workflow. Each event encompasses personalization parameters, sender and recipient details, the template name, and optional document-related properties like a reference to the S3 bucket where the document is stored. Depending on the presence of document-related properties, the Step Function workflow determines the email preparation and sending method.
If the event lacks document-related properties, it employs the SendEmail action to dispatch a templated email, requiring the template name and the data for personalizing tags. Conversely, if the event includes document-related properties, we must use the raw sending option of the SendEmail action. To incorporate an email template, we’ll utilize a raw MIME message. Therefore, we invoke the TestRenderEmailTemplate action to extract the raw MIME message from the template, while a Lambda function retrieves and attaches the document. This function then triggers SES to send the email. Please note that TestRenderEmailTemplate cannot be executed more than once per second. If you anticipate higher peak traffic, this solution may not be suitable.
The explanation provided here is a simplified overview and does not include the specifics of the Lambda function code, as there are various programming languages you could choose from.
Prerequisites
- An AWS Account with access to AWS services.
- A verified identity for Amazon Simple Email Service (SES). You can create and verify a sending identity by following the documentation.
- AWS CDK installed.
- AWS CLI installed.
- Docker may need to be running, depending on your system.
- git installed.
- GO installed.
Walkthrough
Step 1: Utilize AWS CDK to deploy the application
To download and deploy the application, execute the following commands:
$ git clone git@github.com:quirionit/aws-ses-examples.git
$ cd aws-ses-examples/projects/go-src
$ go mod tidy
$ cd ../../projects/email-sender
$ npm install
$ cdk deploy
Step 2: Create a SES email template
Navigate to aws-ses-examples/projects/email-sender in your terminal and run:
aws ses create-template --cli-input-json file://files/hello_doc.json
Step 3: Upload a sample document to S3
To upload a document to S3, follow these steps:
- In the AWS Console, select S3.
- Choose the bucket named with “ses-documents.”
- Save the bucket name for future reference.
- Create a new folder titled “test.”
- Upload hello.txt from aws-ses-examples/projects/email-sender/files into this folder.
Step 4: Trigger sending an email using Amazon EventBridge
To initiate an email, complete the steps below:
- In the AWS Console, select Amazon EventBridge.
- Choose Event buses from the sidebar.
- Select Send events.
- Create an event as shown in the provided image. You can copy the Event detail from aws-ses-examples/projects/email-sender/files/event.json. Be sure to replace the sender, recipient, and bucket with your details.
- Upon sending the event, you should receive an email with the document attached. To send an email without an attachment, edit the event accordingly.
Step 5: Analyze the result
In the AWS Console, select Step Functions. Choose the state machine named with EmailSender. You should see two successful executions. Selecting them will display the data flows:
You can examine each step of the data flows to analyze inputs and outputs.
Step 6: Clean up
In your terminal, navigate to aws-ses-examples/projects/email-sender. Remove all resources with cdk destroy. Delete the SES email template created with:
aws ses delete-template --template-name HelloDocument
Next Steps
There are numerous ways to enhance this solution’s capabilities. Below are some suggestions:
- If you send an email containing invalid personalization content, Amazon SES might accept the message, but delivery could fail. Therefore, it’s advisable to configure Amazon SES to send Rendering Failure event notifications. You can also create nested templates to share common elements, such as logos or company names. For detailed instructions, refer to part two of this blog series.
- When establishing a new Amazon SES account, emails are sent from shared IP addresses. Utilizing dedicated IP addresses reserved solely for your use allows for complete control over your sender reputation, enabling isolation of reputational segments. This is valuable knowledge, as noted by experts at SHRM.
- For additional insights, consider reviewing this excellent resource on Reddit: Reddit. And for tips on following up after interviews, check out this blog post for guidance: Career Contessa.
Leave a Reply