How quirion sends attachments using email templates with Amazon Simple Email Service (SES)
This is part one of the two-part guest series on extending Simple Email Services with advanced functionality. Find part two here.
quirion is an award-winning German robo-advisor, founded in 2013, and with more than 1 billion euros under management. At quirion, we send out five thousand emails a day to more than 60,000 customers.
We chose Amazon Simple Email Service (SES) because it is an easy-to-use and cost-effective email platform. In particular, we benefit from email templates in SES, which ensure a consistent look and feel of our communication. These templates come with a styled and personalized HTML email body, perfect for transactional emails. Sometimes it is necessary to add attachments to an email, which is currently not supported by the SES template feature. To overcome this problem, we created a solution to use the SES template functionality and add file attachments.
Solution: orchestrate different email sending options using AWS Step Functions
The solution we built is fully serverless, which means we do not have to manage the underlying infrastructure. We use AWS Cloud Development Kit (AWS CDK) to deploy the architecture and analyze the resources.
The solution extends SES to send attachments using email templates. SES offers three possibilities for sending emails:
- Simple — A standard email message. When you create this type of message, you specify the sender, the recipient, and the message body, and Amazon SES assembles the message for you.
- Raw — A raw, MIME-formatted email message. When you send this type of email, you have to specify all of the message headers, as well as the message body. You can use this message type to send messages that contain attachments. The message that you specify has to be a valid MIME message.
- Templated — A message that contains personalization tags. When you send this type of email, Amazon SES API v2 automatically replaces the tags with values that you specify.
In this post, we will combine the Raw and the Templated options.
The figure below describes the architecture diagram for the proposed solution.
- The entry point to the application is an EventBridge event bus that routes incoming events to a Step Function workflow.
- An event consists of the personalization parameters, the sender and recipient addresses, the template name and optionally the document-related properties such as a reference to the S3 bucket in which the document is stored. Depending on whether the event contains document-related properties, the Step Function workflow decides how the email is prepared and sent.
- In case the event does not contain document-related properties, it uses the SendEmail action to send a templated email. The action requires the template name and the data to replace the personalization tags.
- If the event contains document-related properties, the raw sending option of the SendEmail action must be used. If we also want to use an email template, we need to use that as a raw MIME message. So, we use the TestRenderEmailTemplate action to get the raw MIME message from the template and use a Lambda function to get and add the document. The Lambda function then triggers SES to send the email.
The solution is simplified for this blog post and is used to show the possibilities of SES. We will not discuss the code of the lambda function as there are several ways to implement it depending on your preferred programming language.
- An AWS Account that provides access to AWS services.
- An Amazon Simple Email Service (Amazon SES) verified identity. You can create and verify a sending identity by following the steps described in the documentation.
- AWS CDK installed.
- AWS CLI installed.
- Depending on your system, Docker may need to be running.
- git installed.
- GO installed.
Step 1: Use the AWS CDK to deploy the application
To download and deploy the application run the following commands:
$ git clone firstname.lastname@example.org: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
In your terminal, navigate to
aws-ses-examples/projects/email-sender 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, complete the following steps:
- On the AWS Console, choose the S3.
- Select the bucket with the name that starts with ses-documents.
- Copy and save the bucket name for later.
- Create a new folder called test.
- Upload the hello.txt from
aws-ses-examples/projects/email-sender/filesinto the folder.
Step 4: Trigger sending an email using Amazon EventBridge
To trigger sending an email, complete the following steps:
- On the AWS Console, choose the Amazon EventBridge.
- Select Event busses from the sidebar.
- Select Send events.
- Create an event as the following image shows. You can copy the Event detail from
aws-ses-examples/projects/email-sender/files/event.json. Don’t forget to replace the sender, recipient and bucket with your values.
- As a result of sending the event, you should receive an email with the document attached.
- To send an email without attachment, edit the event as follows:
Step 5: Analyze the result
- On the AWS Console, choose Step Functions.
- Select the state machine with the name that includes EmailSender.
- You should see two Succeeded executions. If you select them the dataflows should look like this:
- You can select each step of the dataflows and analyze the inputs and outputs.
Step 6: Cleaning up
- Navigate your terminal to
- Delete all resources with
- Delete the created SES email template with:
aws ses delete-template --template-name HelloDocument
There are several ways to extend this solution’s functionality, see some of them below:
- If you send an email that contains invalid personalization content, Amazon SES might accept the message, but won’t be able to deliver it. For this reason, if you plan to send personalized email, you should configure Amazon SES to send Rendering Failure event notifications.
- You can create nested templates to share common elements, such as the company’s logo, name or imprint. See part two of this blog series for instructions.
- When you create a new Amazon SES account, by default your emails are sent from IP addresses that are shared with other SES users. You can also use dedicated IP addresses that are reserved for your exclusive use. This gives you complete control over your sender reputation and enables you to isolate your reputation for different segments within email programs.
In this blog post, we explored how to use Amazon SES to send attachments using email templates. We used an Amazon EventBridge to trigger a Step Function that chooses between sending a raw or templated SES email. This solution uses a full serverless architecture without having to manage the underlying infrastructure. We used the AWS CDK to deploy a predefined architecture and analyzed the deployed resources.
About the authors
|Mark Kirchner is a backend engineer at quirion AG. He uses AWS CDK and several AWS services to provide a cloud backend for a web application used for financial services. He follows a full serverless approach and enjoys resolving problems with AWS.|
|Dominik Richter is a Solutions Architect at Amazon Web Services. He primarily works with financial services customers in Germany and particularly enjoys Serverless technology, which he also uses for his own mobile apps.|
The content and opinions in this post are those of the third-party author and AWS is not responsible for the content or accuracy of this post.