Django + Esputnik – Send Dynamic Email

In this tutorial I will show you the setup and how to send emails to clients who signed up at your system or purchased something.

First, set up a Django project and sign up at Esputnik.com 🙂 and come back to me.

1. Make an API key

Go to Settings -> API and click Add key. Then create it and copy.

2. Make an API key

In your working environment, install requests:

pip install requests

All available requests to the Esputnik API can be found here. Navigate to the request information block and click on Request body to see what can you add or change about it.

3. Email Template

Choose or make it at the Messages dashboard. The important thing to notice is that in order to get dynamic content in, you need to user $!data.get(‘your_variable’). If you want to use this in a link, choose type “Other”.

There in the list of emails you can see a string of numbers – that is the message_id you need to copy to your code.

4. Send Email

The Python code with comments:

import requests

# your account's data
ESPUTNIK_SECRET = "API KEY"
auth = ("anyvalue_for_username", ESPUTNIK_SECRET)

# Email template
message_id = 11111111

# request URL with the message_id
method_url = f"/v1/message/{ message_id }/smartsend"
url = "https://esputnik.com/api" + method_url

# the client's data
user_email = "private@gmail.com"
first_name = "Katerina"


# Request Body

# this needs to be serialized to string, so it is better to work with it separately
json_parameters = {}

data = {
    "recipients": [{
        "locator": user_email,
        "jsonParam": str(json_parameters),
    }],
    "email": "true",
}

x = requests.post(url, json=data, auth=auth)

# here you can see what is going on
print(x.text)
print(x.content)

# this should be 200
print(x.status_code)

5. Dynamic Context

Say, in my email template I have a dynamic text:

$!data.get('first_name'), it is a please to see you among us. Please share your thought on the topic of the day - $!data.get('topic_of_the_day').

Then change the json_parameters dictionary:


json_parameters = {
    "first_name": first_name,
    "topic_of_the_day": "puppies"
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.