Elevate your emails with Amazon SES Templates -Tips & Tricks
Tags: aws, python, tutorial, serverless
Published: 2023-12-29 12:15:18



Have you heard of Amazon Simple Email Service (_SES_)? In case the answer is "**_No_**", in the next lines I will introduce the AWS SES and all the charm it brings to process of sending emails.

---

![A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6l4oago2ezajf4o6x84q.png)

_Credits for the design - [Anna Magura](https://dribbble.com/shots/6100401-Girl-with-letter)_

## What is AWS SES?

> Amazon Simple Email Service (SES) is an email platform that provides an easy, cost-effective way for you to send and receive email using your own email addresses and domains.

It allows you to send marketing emails such as special offers, transactional emails such as order confirmations, and other types of correspondence such as newsletters. When you use AWS SES to receive mail, you can develop software solutions such as email autoresponders, email unsubscribe systems, and applications that generate customer support tickets from incoming emails.

For more information refer to the [documentation](https://docs.aws.amazon.com/ses/latest/dg/Welcome.html).

### But …
The feature of AWS SES that will elevate your emails is actually the - **TEMPLATES**.

> Templates allows you to create designs that will be re-usable and save a lot of time! 

Here are tips and tricks that will not only enhance your email experience but also add a touch of fun to it! 

---

> Note: In this article, we will delve into AWS Lambda and the Python language, which I predominantly use. However, feel free to adapt and re-implement the concepts using your preferred programming language if necessary.

---

## Tip & Trick #1: Enhance the visual appeal of your emails by incorporating HTML, CSS, and static JavaScript for a more polished and engaging presentation

If your emails looks like this:

![A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya4xvxxrl82pbjsu1r7g.png)


it is time to change it! Let's elevate your communication to a whole new level.

![A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkx6e5hw16rs3q5c9ime.jpg)
_Credits for the design- Anna Magura_


To do so, play with `HTML`, `CSS` and `static Java Script` to create the look you prefer. 
After you let your imagination works, here is the coding part:
1. Create AWS Lambda function

2. Use Python language to write the code that will send the email on your behalf. 

The trick is to create separate Python file where you will put your `html` code.

Let's call it `email_template.py` and inside the file create variable called `content` using the following form:

```
content = """
<!DOCTYPE HTML PUBLIC ....
< here goes your html, css, js code>
"""
```

in the `main.py` which is your main file that contains `lambda handler function` import the file like shown bellow:

```
from email_template import content
```
To send the email I prefer using `MIMEApplication`, `MIMEMultipart` and `MIMEText` and `send_raw_email` options, because it allows me to manipulate the data and even send the attachment.

### Create function that will send the email using MIME and SES client
To create SES Client, we will need commonly used Python `boto3` library
 

```
ses_client = boto3.client('ses')
```
One of the methods from [MIME](https://docs.python.org/3/library/email.mime.html) is `message.attach()` that I use to add email body, attachments, etc. where `message` is variable created using `MIMEMultipart('mixed')`.
To send the email use `ses_client.send_raw_email()`, like in the example from the documentation:

```
response = client.send_raw_email(
    Destinations=[
    ],
    FromArn='',
    RawMessage={
        'Data': 'From: sender@example.com\nTo: recipient@example.com\nSubject: Test email (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary="NextPart"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: text/plain;\nContent-Disposition: attachment; filename="attachment.txt"\n\nThis is the text in the attachment.\n\n--NextPart--',
    },
    ReturnPathArn='',
    Source='',
    SourceArn='',
)

print(response)
```
As a final touch, as  `RawMessage` we can use previously created `message` variable and pass it as a `string`.

---

## Tip & Trick #2: SEMPLATES
The trick is to simply use [Semplates](https://semplates.io/)!

![A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxgssjq3gqtxokuns8i0.png)
> About Semplates - unlock the full potential of Amazon SES with Semplates' email template service. Design and publish personalized, responsive and branded emails with a few clicks.

In other words, Semplates is a game changer when it comes to AWS SES Templates!

Even if you are not that familiar with Front-End, by using user friendly Interface, you will be able to drag and drop HTML elements to create Template of your choice or even choose one from the provided examples. 

After you are done, simply connect your AWS Account with the Semplates account following the well explained steps and Publish the Template.
It will show up in your `AWS Account -> SES Templates` and you can use it in your Lambda function using the following line of code:

```
template = ses_client.get_template(TemplateName=<your template name>)
```
and if needed, extract the HTML code from the template.

For more information, follow the [documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses/client/get_template.html).

---

## Now, let's highlight a few key considerations

- Avoid incorporating Dynamic JavaScript content as it lacks support from major email providers like GMAIL.

- When utilizing Semplates, ensure to select the correct Region corresponding to your Lambda function.

- It's crucial to note when using Semplates as team, that only one user is permitted per Region in AWS Account. This restriction is attributed to security measures, preventing unauthorized individuals from republishing or deleting your created Templates, as clarified by Semplate Support.

---

In a nutshell, AWS SES adds a touch of fun to email communication! Elevate your messages with engaging designs using HTML, CSS, and static JavaScript. Dive into the world of AWS Lambda and Python for seamless email customization. And for an extra dose of joy, discover the game-changing Semplates - creating personalized, responsive templates has never been this enjoyable! Just remember to pick the right AWS region and embrace the fun side of AWS SES.

## Refference:
If you prefer Medium, here is [link](https://medium.com/@aleksandraljuboje/elevate-your-emails-with-aws-ses-templates-tips-and-tricks-708f0b818432) to the blog post.  

[Managing AWS Simple Email Service Templates a More Convenient Way - Using Semplates](https://medium.com/swlh/managing-aws-simple-email-service-templates-a-new-way-using-semplates-266b73a49033)

[Using templates to send personalized email with the Amazon SES API](https://docs.aws.amazon.com/ses/latest/dg/send-personalized-email-api.html?source=post_page-----708f0b818432--------------------------------)