Whether you’re part of a business or organization, sending out emails is crucial for connecting with people. More often than not, you’ll find yourself needing to send similar emails to a group and sometimes, they need to be dynamic. Yes, you can send out one email and just add everyone to the BCC but having a dynamic and more personalized email helps create a better connection between you and the recipient. This is a tutorial on how you can create your very own Batch Email Sender Application with just AWS and Python.
Prerequisites
In order to follow this tutorial, you must have the following installed and configured.
- AWS CLI
- Python (preferably 3.12)
- Boto3 Python Module
- Text editor of your choice (preferably VS Code)
Amazon SES Identities
In Amazon SES, you can verify identities which will allow you to send emails using these identities. Currently, you can verify a Domain or an Email Address identity. For the purposes of this tutorial, we’ll only be using an Email Address identity.
Note: You can only send an email to verified email identities in sandbox mode.
To verify an Email Address identity, go to your Amazon SES Dashboard through the AWS Console.
Go to “Identities” from the sidebar navigation and then click the “Create Identity” button.
For the Identity type, choose “Email address” and input your email address. Click “Create identity” to save your settings.
You will receive an email to confirm that this email address does belong to you.
Application Development
Now that you have a verified email identity, we can start the development of our application using Python and Boto3. There are 2 things that we need to set up in order to send dynamic emails. The first one is an email template.
Amazon SES Email Templates
An email template allows us to send dynamic emails with a few minor details that vary. Setting one up is pretty simple because all we have to do is refer to Boto3’s SES Documentation.
- Create a Python file called `main.py` and paste in the following code.
- Run the code with `python main.py`
You can read Boto3’s SES Documentation for more details but here’s a gist of the code above. TemplateName is going to be the name of your template. There are no restrictions when it comes to naming your template but it does have to be unique. The SubjectPart is self-explanatory. The TextPart and HtmlPart are the body of the email. TextPart only shows up when the recipient’s email engine doesn’t allow for HTML rendering.
As you can see from the code above, there is {{name}}. This is a template variable and this is how we make dynamic emails. You can modify the value of this variable every time you send an email.
Sending an Email
Now that we have a registered email identity and a template to use, we can start sending out dynamic emails.
- Create a new Python file named “send_email.py” and paste the following code.
- Run the file using `python send_email.py`.
Again, you can read up on Boto3’s SES Documentation for more details. The Source parameter will be the email identity you’ll be using to send the templated email. The Destination parameter may have multiple options such as the ToAddresses, CcAddresses, and BccAddresses. All these are lists of email addresses, making it easy for you to send to multiple people. The TemplateData is a dictionary where we take the variables in our template and give them a value. You must make sure that all variables in the template has a value provided for in the TemplateData because failing to do so will lead to your email not sending.
Note: I suggest that you add your email as a BccAddresses to check if emails are being sent.