Skip to content
The Official Twilio SendGrid Led, Community Driven Python API Library
Python Other
  1. Python 97.9%
  2. Other 2.1%
Branch: master
Clone or download

Latest commit

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docker-test Twilio update Apr 6, 2019
docker docs: cleanup support/help/contact information Apr 8, 2020
examples docs: cleanup support/help/contact information Apr 8, 2020
proposals Twilio update Apr 6, 2019
sendgrid Release 6.3.2 May 27, 2020
test fix: migrate to common prism setup (#888) May 5, 2020
use_cases feat: add support for Twilio Email (#882) Apr 21, 2020
.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 feat: allow interactive docker testing with 'command' env var May 29, 2020
.travis.yml Update .travis.yml Jun 3, 2020
CHANGELOG.md [Librarian] Version Bump May 27, 2020
CODE_OF_CONDUCT.md docs: baseline all the templated markdown docs (#865) Jan 17, 2020
CONTRIBUTING.md docs: shorten and correct the issue template link May 29, 2020
Dockerfile fix: migrate to common prism setup (#888) May 5, 2020
FIRST_TIMERS.md docs: cleanup support/help/contact information Apr 8, 2020
ISSUE_TEMPLATE.md docs: baseline all the templated markdown docs (#865) Jan 17, 2020
LICENSE.md docs: baseline all the templated markdown docs (#865) Jan 17, 2020
MANIFEST.in fix: add config.yml file to pypi distribution files (#872) Feb 26, 2020
Makefile feat: allow interactive docker testing with 'command' env var May 29, 2020
PULL_REQUEST_TEMPLATE.md docs: baseline all the templated markdown docs (#865) Jan 17, 2020
Procfile Heroku adjustments Aug 16, 2016
README.md docs: Update readme supported versions (#893) May 12, 2020
README.rst docs: cleanup support/help/contact information Apr 8, 2020
TROUBLESHOOTING.md docs: support verbiage for login issues (#880) Mar 31, 2020
USAGE.md docs: Remove more references to 'whitelabel' (#734) Mar 6, 2020
app.json Twilio update Apr 6, 2019
cleanup.sh Version Bump v3.0.0: full v3 Web API support Jun 13, 2016
live_test.py docs: cleanup support/help/contact information Apr 8, 2020
requirements.txt chore: prep the repo for automated releasing (#862) Jan 16, 2020
setup.py docs: cleanup support/help/contact information Apr 8, 2020
test.csv Add small logo Apr 18, 2019
tox.ini Add support for Python 3.7 Oct 5, 2018
twilio_sendgrid_logo.png Add logo file Apr 6, 2019
twilio_sendgrid_logo_small.png Add small logo Apr 18, 2019

README.md

SendGrid Logo

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

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.7, 3.5, 3.6, 3.7, or 3.8
  • The SendGrid service, starting at the free level

Setup Environment Variables

Mac

Update the development environment with your SENDGRID_API_KEY (more info here), 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.

Windows

Temporarily set the environment variable(accessible only during the current cli session):

set SENDGRID_API_KEY=YOUR_API_KEY

Permanently set the environment variable(accessible in all subsequent cli sessions):

setx SENDGRID_API_KEY "YOUR_API_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(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = To("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, to_email, subject, 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(api_key=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(api_key=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(api_key=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

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 maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-python are trademarks of Twilio SendGrid, Inc.

If you need help installing or using the library, please check the Twilio SendGrid Support Help Center.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

License

The MIT License (MIT)

You can’t perform that action at this time.