Emails that always arrive with Sendgrid

— Article in association with Twilio —

What is Sendgrid?

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

So instead of setting up your own server to send email from your web apps, you can trust Sendgrid to get the job done. The benefits 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 this, without technical knowledge thanks to a myriad of no-code tools, but it is also very easy to use the APIs or libraries that Sendgrid offers 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 their colors, but this is not a required step.

Create campaigns without code

Thanks to shipping networkOf course, you can just send newsletters without anything to do with coding or design, just choosing a theme and modifying it before sending it to your contact list.

But the greatest 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, enter a new subscriber. You immediately send him a welcome email. Then a few days later you text him 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 to build loyalty and communicate little by little with your subscribers without having to send them all the same thing.

Thanks to Sendgrid, you can quickly configure all of this directly through your site, without any technical knowledge.

Create a campaign with code

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

Sendgrid will then guide you step by step, from creating an API key to deploying the code, and allow you to validate that your first test email was sent successfully and that everything is OK.

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

First time connecting to Sendgrid with Python

First of all you need to install sendgrid lib like this:

pip3 install sendgrid

Then you need to create an API key by going to next page. You can adjust the permissions of this key so that it gives you access to, for example, marketing features (building lists, etc.), sending emails, using templates, etc. It’s up to you to configure 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 then it will be visible on Sendgrid and you could add contacts to it.

Here is a code to add a contact in my new list. I got your 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)

The good thing is that if you have created custom fields for your contacts, you can also populate them directly through code with the “custom_fields” field.

Importing contacts can also be done by coding the import from a CSV.

If you run into errors and want to get a little more information 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, message them, create, modify or delete lists, etc.

It’s also possible to send emails directly to your lists and I’ll show you how.

Sending email with Sendgrid in Python

To create an email send, all you have to do is forge it according to the API specifications.

Thus we specify in the data object, all the information of the mail, that is, 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. Of course, everything is explained in the API document.

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

As for the costs of shipping networkIt depends on your email consumption, but the good news is that below 2,000 contacts and 6,000 emails sent per month, it’s totally free! So it’s great to try to start building a service around that and then switch to a paid option as soon as the audience is there.

In general, when comparing with the prices of the competition, the price in the mall is quite equivalent to what can be found in other places. On the other hand, the real difference lies in the tools and services offered on Sendgrid. With Sendgrid, whether you’re a confirmed developer or just a hobbyist, you can set up a full email automation or create your newsletter in the space of a day.

It is available to everyone!

Leave a Comment

Your email address will not be published.