Socket
Socket
Sign inDemoInstall

sendgrid

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sendgrid

Twilio SendGrid library for Python


Maintainers
1

.. image:: https://github.com/sendgrid/sendgrid-python/raw/HEAD/twilio_sendgrid_logo.png :target: https://www.sendgrid.com

|Tests Badge| |Python Versions| |PyPI Version| |Docker 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:

  • Version 6.X release is a BREAKING CHANGE from version 5.X, please see the release notes_ for details.
  • 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>__
  • 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_
  • ECDSA-Python_

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/HEAD/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/HEAD/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/HEAD/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

All updates to this library are documented in our CHANGELOG_ and releases_.

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_

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.

License

The MIT License (MIT)_

.. _Twilio: https://github.com/sendgrid/sendgrid-python/blob/HEAD/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/HEAD/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 .. _ECDSA-Python: https://github.com/starkbank/ecdsa-python .. _/mail/send Helper: https://github.com/sendgrid/sendgrid-python/tree/HEAD/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/HEAD/sendgrid/helpers/inbound .. _Twilio SendGrid Documentation: https://sendgrid.com/docs/API_Reference/index.html .. _Library Usage Documentation: https://github.com/sendgrid/sendgrid-python/tree/HEAD/USAGE.md .. _Example Code: https://github.com/sendgrid/sendgrid-python/tree/HEAD/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/HEAD/sendgrid/helpers/mail .. _Processing Inbound Email: https://github.com/sendgrid/sendgrid-python/tree/HEAD/sendgrid/helpers/inbound .. _Examples of common API use cases: https://github.com/sendgrid/sendgrid-python/blob/HEAD/use_cases/README.md .. _breaking changes: https://github.com/sendgrid/sendgrid-python/issues/217 .. _CHANGELOG: https://github.com/sendgrid/sendgrid-python/blob/HEAD/CHANGELOG.md .. _releases: https://github.com/sendgrid/sendgrid-python/releases .. _CONTRIBUTING: https://github.com/sendgrid/sendgrid-python/blob/HEAD/CONTRIBUTING.md .. _Feature Request: https://github.com/sendgrid/sendgrid-python/blob/HEAD/CONTRIBUTING.md#feature-request .. _Bug Reports: https://github.com/sendgrid/sendgrid-python/blob/HEAD/CONTRIBUTING.md#submit-a-bug-report .. _Improvements to the Codebase: https://github.com/sendgrid/sendgrid-python/blob/HEAD/CONTRIBUTING.md#improvements-to-the-codebase .. _Review Pull Requests: https://github.com/sendgrid/sendgrid-python/blob/HEAD/CONTRIBUTING.md#code-reviews .. _troubleshooting guide: https://github.com/sendgrid/sendgrid-python/blob/HEAD/TROUBLESHOOTING.md .. _The MIT License (MIT): https://github.com/sendgrid/sendgrid-python/blob/HEAD/LICENSE

.. |Tests Badge| image:: https://github.com/sendgrid/sendgrid-python/actions/workflows/test.yml/badge.svg :target: https://github.com/sendgrid/sendgrid-python/actions/workflows/test.yml .. |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/ .. |Docker Badge| image:: https://img.shields.io/docker/automated/sendgrid/sendgrid-python.svg :target: https://hub.docker.com/r/sendgrid/sendgrid-python/ .. |MIT licensed| image:: https://img.shields.io/badge/license-MIT-blue.svg :target: ./LICENSE .. |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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc