sendgrid-python

The Official Twilio SendGrid Led, Community Driven Python API Library

Github星跟踪图

.. image:: https://github.com/sendgrid/sendgrid-python/raw/master/twilio_sendgrid_logo.png
:target: https://www.sendgrid.com, Travis Badge, codecov, Python Versions, PyPI Version, Docker Badge, Email Notifications Badge, MIT licensed, Twitter Follow, GitHub contributors, Open Source Helpers, This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via Python.

NEW:

  • Subscribe to email notifications_ for releases and breaking changes.
  • Version 6.X release is a BREAKING CHANGE from version 5.X, please see the release notes_ for details.
  • Quickly get started with Docker_.
  • Send SMS messages with Twilio_.

This library provides full support for all Twilio SendGrid Web API v3_ endpoints, including v3 /mail/send_.

We want this library to be community driven and Twilio 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 <#installation>__
  • Quick Start <#quick-start>__
  • Common Use Cases <#use-cases>__
  • General Usage <#usage>__
  • Processing Inbound Email <#processing-inbound-email>__
  • Announcements <#announcements>__
  • Roadmap <#roadmap>__
  • How to Contribute <#how-to-contribute>__
  • Troubleshooting <#troubleshooting>__
  • About <#about>__
  • License <#license>__

Installation

Prerequisites

  • Python version 2.7 and 3.5+
  • For email, you will need a Twilio SendGrid account, starting at the free level_
  • For SMS messages, you will need a free Twilio account_

Setup Environment Variables

Mac


Update the development environment with your `SENDGRID_API_KEY`_ (more info `here <https://sendgrid.com/docs/User_Guide/Settings/api_keys.html>`__), for example:

.. code:: bash

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

Twilio 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):

.. code:: bash

set SENDGRID_API_KEY=YOUR_API_KEY

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

.. code:: bash

setx SENDGRID_API_KEY "YOUR_API_KEY"

Install Package

.. code:: bash

pip install sendgrid

Dependencies

  • Python-HTTP-Client_

Quick Start

Hello Email

The following is the minimum needed code to send an email with the /mail/send Helper_
(here <https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/kitchen_sink.md>__ is a full example):

With Mail Helper Class


.. code:: python

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

    message = Mail(
        from_email='from_email@example.com',
        to_emails='to@example.com',
        subject='Sending with Twilio SendGrid is Fun',
        html_content='<strong>and easy to do anywhere, even with Python</strong>')
    try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(str(e))

The ``Mail`` constructor creates a `personalization object`_ for you.
`Here <https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/kitchen_sink.md>`__ 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 <https://github.com/sendgrid/sendgrid-python/blob/master/examples/mail/mail.py#L27>__ is a full example):

.. code:: python

import os
from sendgrid import SendGridAPIClient

message = {
    'personalizations': [
        {
            'to': [
                {
                    'email': 'test@example.com'
                }
            ],
            'subject': 'Sending with Twilio SendGrid is Fun'
        }
    ],
    'from': {
        'email': 'test@example.com'
    },
    'content': [
        {
            'type': 'text/plain',
            'value': 'and easy to do anywhere, even with Python'
        }
    ]
}
try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(str(e))

General v3 Web API Usage (With Fluent Interface_)

.. code:: python

import os
from sendgrid import SendGridAPIClient

sg = SendGridAPIClient(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_)

.. code:: python

import os
from sendgrid import SendGridAPIClient

sg = SendGridAPIClient(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

  • Twilio SendGrid Documentation_
  • Library Usage Documentation_
  • Example Code_
  • How-to: Migration from v2 to v3_
  • v3 Web API Mail Send Helper_ - build a request object payload for a v3 /mail/send API call.
  • Processing Inbound Email_

Use Cases

Examples of common API use cases_, such as how to send an email with a transactional template or add an attachment or send an SMS message.

Announcements

Join an experienced and passionate team that focuses on making an impact.
Opportunities abound_ to grow the product - and grow your career!

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 <https://github.com/sendgrid/sendgrid-python/pulls>__.
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:

  • Feature Request_
  • Bug Reports_
  • Improvements to the Codebase_
  • Review Pull Requests_
  • Sign the CLA to Create a Pull Request_

Troubleshooting

Please see our troubleshooting guide_ for common library issues.

About

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

Email the Developer Experience Team here <mailto:dx@sendgrid.com>__ in case of any queries.

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

License

The MIT License (MIT)_

.. _notifications: https://dx.sendgrid.com/newsletter/python
.. _Docker: https://github.com/sendgrid/sendgrid-python/tree/master/docker
.. _Twilio: https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/sms.md
.. _release notes: https://github.com/sendgrid/sendgrid-python/releases/tag/v6.0.0
.. _Web API v3: https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html
.. _v3 /mail/send: https://sendgrid.com/blog/introducing-v3mailsend-sendgrids-new-mail-endpoint
.. _issues: https://github.com/sendgrid/sendgrid-python/issues
.. _pull requests: https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md
.. _free level: https://sendgrid.com/free?source=sendgrid-python
.. _Twilio account: https://www.twilio.com/try-twilio?source=sendgrid-python
.. _SENDGRID_API_KEY: https://app.sendgrid.com/settings/api_keys
.. _Python-HTTP-Client: https://github.com/sendgrid/python-http-client
.. _/mail/send Helper: https://github.com/sendgrid/sendgrid-python/tree/master/sendgrid/helpers/mail
.. _personalization object: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html
.. _Fluent Interface: https://sendgrid.com/blog/using-python-to-implement-a-fluent-interface-to-any-rest-api/
.. _our helper: https://github.com/sendgrid/sendgrid-python/tree/master/sendgrid/helpers/inbound
.. _Twilio SendGrid Documentation: https://sendgrid.com/docs/API_Reference/index.html
.. _Library Usage Documentation: https://github.com/sendgrid/sendgrid-python/tree/master/USAGE.md
.. _Example Code: https://github.com/sendgrid/sendgrid-python/tree/master/examples
.. _How-to: Migration from v2 to v3: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/how_to_migrate_from_v2_to_v3_mail_send.html
.. _v3 Web API Mail Send Helper: https://github.com/sendgrid/sendgrid-python/tree/master/sendgrid/helpers/mail
.. _Processing Inbound Email: https://github.com/sendgrid/sendgrid-python/tree/master/sendgrid/helpers/inbound
.. _Examples of common API use cases: https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/README.md
.. _Opportunities abound: https://sendgrid.com/careers
.. _breaking changes: https://github.com/sendgrid/sendgrid-python/issues/217
.. _CHANGELOG: https://github.com/sendgrid/sendgrid-python/blob/master/CHANGELOG.md
.. _releases: https://github.com/sendgrid/sendgrid-python/releases
.. _release notifications: https://dx.sendgrid.com/newsletter/python
.. _CONTRIBUTING: https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md
.. _Feature Request: https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#feature-request
.. _Bug Reports: https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#submit-a-bug-report
.. _Improvements to the Codebase: https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#improvements-to-the-codebase
.. _Review Pull Requests: https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#code-reviews
.. _Sign the CLA to Create a Pull Request: https://cla.sendgrid.com/sendgrid/sendgrid-python
.. _troubleshooting guide: https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md
.. _Developer Experience Team: mailto:dx@sendgrid.com
.. _The MIT License (MIT): https://github.com/sendgrid/sendgrid-python/blob/master/LICENSE.md

.., Travis Badge, image:: https://travis-ci.org/sendgrid/sendgrid-python.svg?branch=master
:target: https://travis-ci.org/sendgrid/sendgrid-python
.., Python Versions, image:: https://img.shields.io/pypi/pyversions/sendgrid.svg
:target: https://pypi.org/project/sendgrid/
.., PyPI Version, image:: https://img.shields.io/pypi/v/sendgrid.svg
:target: https://pypi.org/project/sendgrid/
.., codecov, image:: https://img.shields.io/codecov/c/github/sendgrid/sendgrid-python/master.svg?style=flat-square&label=Codecov+Coverage
:target: https://codecov.io/gh/sendgrid/sendgrid-python
.., Docker Badge, image:: https://img.shields.io/docker/automated/sendgrid/sendgrid-python.svg
:target: https://hub.docker.com/r/sendgrid/sendgrid-python/
.., Email Notifications Badge, image:: https://dx.sendgrid.com/badge/python
:target: https://dx.sendgrid.com/newsletter/python
.., MIT licensed, image:: https://img.shields.io/badge/license-MIT-blue.svg
:target: ./LICENSE.md
.., Twitter Follow, image:: https://img.shields.io/twitter/follow/sendgrid.svg?style=social&label=Follow
:target: https://twitter.com/sendgrid
.., GitHub contributors, image:: https://img.shields.io/github/contributors/sendgrid/sendgrid-python.svg
:target: https://github.com/sendgrid/sendgrid-python/graphs/contributors
.., Open Source Helpers, image:: https://www.codetriage.com/sendgrid/sendgrid-python/badges/users.svg
:target: https://www.codetriage.com/sendgrid/sendgrid-python

主要指标

概览
名称与所有者sendgrid/sendgrid-python
主编程语言Python
编程语言Python (语言数: 6)
平台
许可证MIT License
所有者活动
创建于2012-02-25 19:10:40
推送于2025-06-12 10:27:23
最后一次提交
发布数132
最新版本名称6.12.4 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数1.6k
关注者数216
派生数722
提交数1.5k
已启用问题?
问题数586
打开的问题数42
拉请求数331
打开的拉请求数11
关闭的拉请求数183
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?