Bootstrap FreeKB - SendGrid - Sending email using Python
SendGrid - Sending email using Python

Updated:   |  SendGrid articles

You will need an API Key such as SG.abcdefg123456789abcdefg123456789abcdefg123456789abcdefg123456789abcde to create an email using SendGrid and Python. At https://sendgrid.com/ select Email API > Integration Guide > Web App > Python and create an API key. This is your one and only chance to make note of the API Key, so make note of it now!

 

Or, you can Create email using REST API. You can also List your API Keys using the REST API.

curl --header "Authorization: Bearer SA.14Hj6fh1RBSXoatMt4f5hg.zOhdOFKuitjqOtWatZXmGefxDMFDWIIElNhe7vb78z9" --request GET --url https://api.sendgrid.com/v3/api_keys

 

Something like this should be returned.

{
    "result": [
        {
            "name": "default API Key for smtp.sendgrid.net relay",
            "api_key_id": "xyz123456789xyz1234567"
        },
        {
            "name": "default",
            "api_key_id": "abcdefg123456789abcdef"
        }
    ]
}

 

When testing/debugging, you can probably set the API key to Full Access. At https://sendgrid.com/ select Settings > API Keys > edit API Key and ensure Full Access is selected.

 

Use pip to install sendgrid.

pip install sendgrid

 

Send your first email.

#!/usr/bin/python3
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='from@example.com',
    to_emails='to@example.com',
    subject='Hello',
    html_content='<strong>World</strong>')

try:
    sg = SendGridAPIClient('YOUR API KEY')
except Exception as exception:
    print(f"got the following exception when attempting to create the sg object: {exception}")
else:
    print(f"successfully created the sg object")

try:
    response = sg.send(message)
except Exception as exception:
    print(f"got the following exception when attempting sg.send(message): {exception}")
else:
    print(f"response.status_code = {response.status_code}")
    print(f"response.body        = {response.body}")
    print(f"response.headers     = {response.headers}")

 

And here is how you can send a Template ID.

#!/usr/bin/python3
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='from@example.com',
    to_emails='to@example.com'
)

message.template_id = "d-abcdefg123456789abcdefg123456789"

try:
    sg = SendGridAPIClient('YOUR API KEY')
except Exception as exception:
    print(f"got the following exception when attempting to create the sg object: {exception}")
else:
    print(f"successfully created the sg object")

try:
    response = sg.send(message)
except Exception as exception:
    print(f"got the following exception when attempting sg.send(message): {exception}")
else:
    print(f"response.status_code = {response.status_code}")
    print(f"response.body        = {response.body}")
    print(f"response.headers     = {response.headers}")

 

Personalization can be used to carbon copy and blind carbon copy recipients.

#!/usr/bin/python3
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, Cc, Bcc, To

message = Mail(
    from_email='from@example.com',
    to_emails='to@example.com',
    subject='Hello',
    html_content='<strong>World</strong>'
)

personalization = Personalization()
personalization.add_to(To('john.doe@example.com'))
personalization.add_cc(Cc('jack.doe@example.com'))
personalization.add_bcc(Bcc('jane.doe@example.com'))
message.add_personalization(personalization)

try:
    sg = SendGridAPIClient('YOUR API KEY')
    response = sg.send(message)
    print(f"response.status_code = {response.status_code}")
    print(f"response.body        = {response.body}")
    print(f"response.headers     = {response.headers}")
except Exception as exception:
    print(exception)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e98f7a in the box below so that we can be sure you are a human.