Integrate SendGrid and create dynamic templates

Usman Ali
3 min readAug 9, 2022

We often need to send emails to get the job done. Some cases are welcoming new accounts with instructions, reset forgot passwords, inviting specific events, etc. In this article, I will explain how to integrate SendGrid into your application. Also, how to send dynamic templates data from request. In the end, I will send a POST request with the required credentials and detail in order to send Email using python(you can choose any alternative programming language).

Steps:

  1. Create SendGrid Account.
  2. Create API keys.
  3. Create Dynamic Templates.
  4. Create Sender.
  5. Send Request.

1. Create SendGrid Account

Click on this link to create SendGrid Account. Fill and complete all the required forms detail in order to send an account activation request. When your account will be activated continue towards your second step.

2. Create API keys

Click on this link and click on Create API key.

Enter your API Key Name and select the desired permission according to your requirements.

3. Create Dynamic Templates

Go to this link and click on Create Dynamic Template, and enter your template name. Next, you have to add a version to your Dynamic template.

Click on Add Version and select SendGrid Email Designs. You can select design whatever you want and after that, you have two choices, one is to open that design in the Design Editor or in the Code Editor. Design Editor is used for the drag and drop option while Code Editor is used for editing that design from code.

Open the design into the desired selector. And place all the dynamic template data into these brackets {{}} with space on right and left.

For example in my case I want to send subject and title as a Dynamic Template Data from my POST request. So it will be like that, subject can be seen on left side.

Save your design and make sure to copy the Template ID which will be used in the request.

4. Create Sender

Go to this link to create a sender. You can also create a sender from other email accounts.

For example, if you want to create a sender from a client's email. You have to enter all the desired details by getting them from the client. After that, you have to ask the client to verify that sender, that's all.

Check the verified column with a green tick to make sure that sender is verified.

5. Send Request

Create your own data by customizing to, from, and reply_to keys. As I want to send the subject and title from a request I will put them into the dynamic_template_data. At the end paste copied Template ID into the template_id and add the API key in the request header.

Here is a complete code.

import requests
data = {
"personalizations": [
{
"to": [
{
"email": "example@gmail.com",
"name": "ExampleEmail"
}
],
"dynamic_template_data": {
"subject": "{subject}",
"title": "{title}",
},
}
],
"from": {
"email": "account@gmail.com",
"name": "AccountEmail"
},
"reply_to": {
"email": "reply@gmail.com",
"name": "ReplyTo"
},
"template_id": "{template_id}"
}
requests.post(
url="https://api.sendgrid.com/v3/mail/send",
json=data,
headers={'Authorization': f'Bearer {SENDGRID_API_KEY}'},
)

Conclusion:

In this post, we looked at how to Integrate SendGrid and create dynamic templates. In upcoming posts, I will discuss SendGrid's other features in their detail.

--

--

Usman Ali
0 Followers

Software Engineer | Python | Django