Emails that arrive every time with Sendgrid

What is Sendgrid?

Sendgrid is a suite of tools and services offered by Twilio that allows you to send emails, create scenarios, newsletters and automate everything related to sending messages.

So rather than setting up your own server to send emails from your web apps, you can rely on Sendgrid to do the job. The advantages are numerous: reliability, ease of implementation, better deliverability of your messages and above all you no longer risk having your server blacklisted for sending spam.

Sendgrid is a tool that can be used like that, without technical skills thanks to a myriad of no-code tools, but it is also very easy to use the APIs or libraries offered by Sendgrid to precisely interact with the service 100 % in code, regardless of language.

Sendgrid also allows you to link your own domain name so that your sender addresses are in your colors, but this is not a mandatory step.

Create no-code campaigns

Thanks to Sendgrid, you can of course simply send newsletters without anything to do with coding or designing, simply by choosing a theme and modifying it before sending it to your contact list.

But the strongest added value of the service lies in the “Automations” section which makes it easy to create, by filling out a few forms, a whole scenario for each new person who gives you their email address.

For example, you enter a new subscriber. Immediately you send him a welcome email. Then a few days later, you send a message back, and a few days or weeks later, you can send him other messages…etc. The idea is to prepare a whole series of emails that will be sent to your new subscriber over time. This is very practical for building loyalty and communicating gradually with your subscribers without having to send the same thing to everyone.

Thanks to Sendgrid, you can set up all this quickly directly via their site, without having any technical skills.

Create a campaign with code

Creating campaigns with code is also very simple since the Sendgrid site will guide you in the use of these APIs. Just choose to use the web API (or the SMTP relay) to then be guided according to the chosen language: Python, Ruby, JavaScript, Go, PHP…etc.

Then, Sendgrid will guide you step by step, from creating an API key, to implementing the code and allowing you to validate that your first test email has been sent correctly and that everything is OK.

Once this first step has been completed, you will then be able to do everything with code: Create contact lists, feed them, delete contacts, manage scenarios for sending emails, create and schedule newsletters, etc. It’s happiness, because it’s simple to integrate and the documentation is very rich.

First time connecting to Sendgrid with Python

First of all, you have to install the sendgrid lib like this:

pip3 install sendgrid

Then you must create an API key by going to next page. You can adjust the permissions of this key so that it gives you access, for example, to marketing functionalities (creation of lists, etc.), to sending emails, to using templates, etc. It’s up to you to make your settings according to what you want.

From there, all you have to do is code. To connect to the API, all you have to do is use the following code:

from sendgrid import SendGridAPIClient

sg = SendGridAPIClient('MA_CLÉ_API')

For example, if I want to create a new contact list, I can add the following code below:

#create a list

data = {
    "name": "MA LISTE"
}
response = sg.client.marketing.lists.post(
    request_body=data
)
print(response.status_code)
print(response.body)
print(response.headers)

And it will then be visible at Sendgrid and I could add contacts to it.

Here is a code to add a contact in my new list. I got his ID before:

from sendgrid import SendGridAPIClient

sg = SendGridAPIClient('MA_CLÉ_API')

data = {
    "list_ids": ["a44be4a3-5cae-43f2-8ce5-cc32a042e54f"],
    "contacts": [
        {
            "email": "[email protected]",
            "first_name": "Korben",
            "last_name": "Manu",
            "country": "France"
        }
    ]
}

response = sg.client.marketing.contacts.put(request_body=data)

print(response.status_code)
print(response.body)
print(response.headers)

What’s cool is that if you’ve created custom fields for your contacts, you can also populate them directly through code with the “custom_fields” field.

Contact import can also be done by coding the import of a CSV.

If you encounter errors and want to get a bit more info from Sendgrid, I recommend adding the following import:

from python_http_client.exceptions import HTTPError

And encapsulate the launch of the request like this

try:
    response = sg.client.marketing.contacts.put(request_body=data)
except HTTPError as e:
    print(e.to_dict)

So obviously, I didn’t specify it, but you can add, modify, delete contacts, send them messages, create, modify or delete lists and so on.

It is also possible to send emails directly to your lists and I will show you how.

Sending email with Sendgrid in Python

To create an e-mail send, all you have to do is forge it by respecting the specifications of the API.

We thus specify in the data object, all the information of the mail, namely the sender (sender_id), the subject, the HTML and text content, the link to unsubscribe or the list to which to send the message… etc Everything is of course explained in the API doc.

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

sg = SendGridAPIClient('MA_CLÉ_API')

data = {
    "name": "Ma newsletter 2",
    "status": "active",
        "email_config": {
            "sender_id": 4441081,
            "subject": "Quoi de neuf ?",
            "html_content": "<html><body><p>Cette semaine voici les nouveaux sujets...</p></body></html>",
            "plain_content": "Cette semaine voici les nouveaux sujets...",
            "custom_unsubscribe_url": "http://www.example.com/unsubscribe",
        },
    
    "send_to": {
        "list_ids": [
            "a44be4a3-5cae-43f2-8ce5-cc32a042e54f"
        ],    
    },
}

try:
    response = sg.client.marketing.singlesends.post(request_body=data)
except HTTPError as e:
    print(e.to_dict)
else:
    print(response.status_code)
    print(response.body)
    print(response.headers)

The cost of Sendgrid

In terms of Sendgrid’s costs, it depends on your email consumption, but the good news is that below 2000 contacts and 6000 emails sent per month it’s totally free! So it’s great to try and start building a service around that, then switch to a paid option as soon as the audience is there.

In general, by comparing with the prices of the competition, the price at the mall is quite equivalent to what can be found elsewhere. On the other hand, the real difference lies in the tools and services that are offered on Sendgrid. With Sendgrid, whether you are a confirmed developer or a simple amateur, you can set up a whole email automation or create your newsletter in the space of a day.

It’s within everyone’s reach!

5/5 - (1 vote)
SAKHRI Mohamed
SAKHRI Mohamed

The blog of a computer enthusiast who shares news, tutorials, tips, online tools and software for Windows, macOS, Linux, Web designer and Video games.

Articles: 3764

Leave a Reply

Your email address will not be published.