The Official SendGrid Led, Community Driven Python API Library
Python Shell HTML
Clone or download
Permalink
Failed to load latest commit information.
.github New PR Template Oct 28, 2017
docker-test Fix Tests May 25, 2018
docker Version Bump v5.4.1: Bug Fixes Jun 26, 2018
examples Updated def build_personalization Jun 15, 2018
proposals Merge pull request #353 from yothinix/fix-typography-on-mail-helper-p… Dec 16, 2017
sendgrid Version Bump v5.4.1: Bug Fixes Jun 26, 2018
test add test_directly_setting_substitutions Jun 8, 2018
use_cases Fix merge conflicts Jun 7, 2018
.codeclimate.yml Update .codeclimate.yml Oct 30, 2017
.coveragerc coverage set up for tox Oct 1, 2017
.env_sample Add a .env_sample file and Update README Oct 27, 2017
.gitignore Fix Tests May 25, 2018
.travis.yml Merge branch 'master' into patch-1 Jun 6, 2018
CHANGELOG.md Version Bump v5.4.1: Bug Fixes Jun 26, 2018
CODE_OF_CONDUCT.md update code of conduct Oct 10, 2017
CONTRIBUTING.md Merge pull request #513 from thepriefy/patch-3 Jun 6, 2018
LICENSE.txt Bump up year Feb 20, 2018
MANIFEST.in Version Bump v3.2.1: pep8, PyPi fixes Aug 17, 2016
Procfile Heroku adjustments Aug 16, 2016
README.md Typos Jun 6, 2018
TROUBLESHOOTING.md fixing link to use cases readme Jun 3, 2018
USAGE.md Updated USAGE.md and CONTRIBUTING.md Oct 31, 2017
app.json Updating links for merge Aug 17, 2016
cleanup.sh Version Bump v3.0.0: full v3 Web API support Jun 13, 2016
register.py Version Bump v5.4.1: Bug Fixes Jun 26, 2018
requirements.txt add six to requirements.txt Oct 29, 2016
sendgrid.env fixed documentation link in usage.md Oct 6, 2017
setup.py Change io.open to open, since regular open in Python 3 already suppor… Nov 1, 2017
tox.ini Refactor and add test for Send Oct 28, 2017

README.md

SendGrid Logo

Travis Badge codecov Docker Badge Email Notifications Badge MIT licensed Twitter Follow GitHub contributors

NEW:

  • Subscribe to email notifications for releases and breaking changes.
  • Quickly get started with Docker.

This library allows you to quickly and easily use the SendGrid Web API v3 via Python.

Version 3.X.X+ of this library provides full support for all SendGrid Web API v3 endpoints, including the new v3 /mail/send.

This library represents the beginning of a new path for SendGrid. We want this library to be community driven and SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or simply upvote or comment on existing issues or pull requests.

Please browse the rest of this README for further detail.

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • Python version 2.6, 2.7, 3.4, 3.5 or 3.6
  • The SendGrid service, starting at the free level

Setup Environment Variables

Update the development environment with your SENDGRID_API_KEY, for example:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

Sendgrid also supports local environment file .env. Copy or rename .env_sample into .env and update SENDGRID_API_KEY with your key.

Install Package

pip install sendgrid

Dependencies

Quick Start

Hello Email

The following is the minimum needed code to send an email with the /mail/send Helper (here is a full example):

With Mail Helper Class

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = Email("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

The Mail constructor creates a personalization object for you. Here is an example of how to add it.

Without Mail Helper Class

The following is the minimum needed code to send an email without the /mail/send Helper (here is a full example):

import sendgrid
import os

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
data = {
  "personalizations": [
    {
      "to": [
        {
          "email": "test@example.com"
        }
      ],
      "subject": "Sending with SendGrid is Fun"
    }
  ],
  "from": {
    "email": "test@example.com"
  },
  "content": [
    {
      "type": "text/plain",
      "value": "and easy to do anywhere, even with Python"
    }
  ]
}
response = sg.client.mail.send.post(request_body=data)
print(response.status_code)
print(response.body)
print(response.headers)

General v3 Web API Usage (With Fluent Interface)

import sendgrid
import os

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
response = sg.client.suppression.bounces.get()
print(response.status_code)
print(response.body)
print(response.headers)

General v3 Web API Usage (Without Fluent Interface)

import sendgrid
import os

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
response = sg.client._("suppression/bounces").get()
print(response.status_code)
print(response.body)
print(response.headers)

Processing Inbound Email

Please see our helper for utilizing our Inbound Parse webhook.

Usage

Use Cases

Examples of common API use cases, such as how to send an email with a transactional template.

Announcements

Join an experienced and passionate team that focuses on making an impact. Opportunities abound to grow the product - and grow your career! Check out our Data Platform Engineer role

Please see our announcement regarding breaking changes. Your support is appreciated!

All updates to this library are documented in our CHANGELOG and releases. You may also subscribe to email release notifications for releases and breaking changes.

Roadmap

If you are interested in the future direction of this project, please take a look at our open issues and pull requests. We would love to hear your feedback.

How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Quick links:

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-python is guided and supported by the SendGrid Developer Experience Team.

sendgrid-python is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-python are trademarks of SendGrid, Inc.

License

The MIT License (MIT)