Sending an email with Sendgrid

Alexis Chilinski
3 min readAug 17, 2020

--

I wrote a blogpost a while ago about sending an email through Twilio’s Verify, but if you’re just looking to send an email from your backend (say… a Python/Django backend? You know me), then this is the blogpost for you!

First, of course, you will need to create a Sendgrid account. You can make a free account, unless you want to upgrade for more emails sent per day.

This quick tutorial will run through the Sendgrid API. So once you create your account, you will be prompted to verify a Sender Identity. You are given two choices — you can either use your own domain, or Domain Authentication (which will require editing your domain’s DNS) or Single-Sender Verification (it will use the name you choose, but it will be sent via sendgrid.net):

Options for Sendgrid’s Sender Identity

Once you set this up (and you confirm the email address you used to create your account), you’ll be all set to start sending emails from the desired sender. Click the ‘Email API’ tab on the left navbar, then choose the Web API. Then you will be prompted to choose your language. I am apparently obsessed with Python, so that’s what I’ll be working with. You will then have to create an API key, and pick a name for it (something relevant to your project). Then create the key. Remember to store this somewhere SAFE (like in a .env file in your project directory and then put the .env in your gitignore so nobody else will have access to it). You will need to use this key when sending emails through the API.

Then install the proper package (if your language requires it). For python, it’s the sendgrid-python library, so it’ll be a pip3 install sendgrid command. I also use the decouple library for importing from a .env file. Also, make sure you’re installing libraries in your virtual environment, and then run pip3 freeze to make sure it was successfully installed. Then, I recommend creating a file for the next email functions so it doesn’t clutter up your other files (since you can just import the functions later).

Now you can simply follow the next directions that Sendgrid has written out:

If you’ll notice, there’s some hard-coded data in this as well as some unnecessary printing. These are fine for now while you’re testing, but make sure to remove the prints and to put this all in one function and use arguments to make it more dynamic. For example this is my code:

import sendgrid
from decouple import config
sendgrid_key = config("SENDGRID_API_KEY")def send_email(to_email, subject, value):
send_grid = sendgrid.SendGridAPIClient(api_key=sendgrid_key)
data = get_data(to_email, subject, value)
send_grid.client.mail.send.post(request_body=data)
def get_data(to_email, subject, value):
return {
"personalizations": [{"to": [{"email": to_email}], "subject":
subject}],
"from": {"email": "email@email", "name": "My Company"},
"content": [{"type": "text/html", "value": value}],
}

So, to break this down, I have put the sendgrid functions in its own function, and I’ve created a helper method that holds the data for the email, and this is all completely dynamic. For example, if I were to run:

send_email("customer@customer.com", "Hello World!", "<p>Welcome,
valued customer!</p>")

This would use my SENDGRID_API_KEY sent through the Sendgrid Web API to send an email to customer@customer.com that reads “Welcome, valued customer!” with a “Hello World!” subject line.

The sendgrid-python library docs are very helpful, and I’m only using the bare minimum (all of the fields I’m using are required, per the docs). But, if you want to include more fields, see the full example here. You can integrate templates you create on Sendgrid, include attachments, etc. You can also use plain text instead of text/html in the email body (as I’ve used text/html), for example.

This is just a simple example of sending emails through Sendgrid, but it has other functionality that caters to others’ needs (promotions, etc).

Hope this helps somebody out there!

--

--