Sending Personalized Bulk Emails using SES Template

1. Introduction

In this article, we’re going to build a java program to send personalized bulk emails using SES template. We will send a personal email to every destination by applying template data.

This article is part of the ‘AWS Simple Email Service‘ series here at CloudTechPro.

2. Configuring Contacts

We need to configure multiple contacts to send bulk email. Let’s create contact pairs.

List<Pair<String, String>> contacts = new ArrayList<Pair<String, String>>();

// Contacts with email and Name
contacts.add(new Pair<String, String>("satish@cloudtechpro.com", "Satish-CloudTechPro"));
contacts.add(new Pair<String, String>("satishpandey.soft@gmail.com", "Satish-Gmail"));

Now we have a list of contacts with display names. Later, we’ll use these pairs in the email request.

3. Configuring Default Template Data

SES provides sending default values in case it’s missing in some destinations. Let’s set the default display name.

bulkTemplatedEmailRequest.withDefaultTemplateData(String.format(dataFormat, "UserNameNotAvailable"));

We can only set a single default template data for every request. Default data will be placed in case the destination does not provide that.

4. Prepare Destinations

We can send the same version of the email to multiple addresses. But in our case, we want to send personalized email to every address. Let’s design our code for that.

List<BulkEmailDestination> bulkEmailDestinations = new ArrayList<BulkEmailDestination>();
for (Pair<String, String> contact : contacts) {
    Destination destination = new Destination();
    List<String> toAddresses = new ArrayList<String>();
    toAddresses.add(contact.getKey());
    destination.setToAddresses(toAddresses);
    BulkEmailDestination bulkEmailDestination = new BulkEmailDestination();
    bulkEmailDestination.setDestination(destination);
    bulkEmailDestination.setReplacementTemplateData(String.format(dataFormat, contact.getValue()));
    bulkEmailDestinations.add(bulkEmailDestination);
}

We prepared the list of destinations. Each destination contains a single contact. We’ve configured replacement value for every contact to send a personal email.

Currently, AWS SES only supports max 50 destinations. I personally think this limit should be increased. You can create a loop and fire another request if you have more then 50 destinations.

5. Sending Bulk Email

Let’s prepare the bulk email request.

SendBulkTemplatedEmailRequest bulkTemplatedEmailRequest = new SendBulkTemplatedEmailRequest();
bulkTemplatedEmailRequest.withDestinations(bulkEmailDestinations);

We have created a request with destinations.

Now it’s time send the email.

private static void sendEmail() {
    // Prepare destinations
    SendBulkTemplatedEmailRequest bulkTemplatedEmailRequest = new SendBulkTemplatedEmailRequest();
    bulkTemplatedEmailRequest.withDestinations(bulkEmailDestinations);
    bulkTemplatedEmailRequest.withTemplate(templateName);
    bulkTemplatedEmailRequest.withDefaultTemplateData(String.format(dataFormat, "UserNameNotAvailable"));
    bulkTemplatedEmailRequest.withSource(senderEmail);
    SendBulkTemplatedEmailResult bulkTemplatedEmailResult = ses.sendBulkTemplatedEmail(bulkTemplatedEmailRequest);
    System.out.println(bulkTemplatedEmailResult.getStatus());
}

Congratulations! We have successfully sent personalized emails to each destination.

6. Conclusion

In this article, we’ve seen how to send personalized bulk emails using SES template. We’ve created a bulk email request with template data. Also, default template data configured to handle missing inputs from destinations.

As always, sample program can be found over on GitHub.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *