Package: sendgrid
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 therelease notesfor details.Send SMS messages withTwilio.This library provides full support for all Twilio SendGridWeb API v3endpoints, includingv3 /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 createissuesandpull requestsor 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 ContentsInstallationQuick StartCommon Use CasesGeneral UsageProcessing Inbound EmailAnnouncementsHow to ContributeTroubleshootingAboutLicenseInstallationPrerequisitesPython version 2.7 and 3.5+For email, you will need a Twilio SendGrid account, starting at thefree levelFor SMS messages, you will need a freeTwilio accountSetup Environment VariablesMacUpdate the development environment with yourSENDGRID_API_KEY(more infohere), for example:echo"export SENDGRID_API_KEY='YOUR_API_KEY'">sendgrid.envecho"sendgrid.env">>.gitignoresource./sendgrid.envTwilio SendGrid also supports local environment file.env.
Copy or rename.env_sampleinto.envand updateSENDGRID_API_KEYwith your key.WindowsTemporarily set the environment variable (accessible only during the current CLI session):setSENDGRID_API_KEY=YOUR_API_KEYPermanently set the environment variable (accessible in all subsequent CLI sessions):setxSENDGRID_API_KEY"YOUR_API_KEY"Install PackagepipinstallsendgridDependenciesPython-HTTP-ClientECDSA-PythonQuick StartHello EmailThe following is the minimum needed code to send an email with the/mail/send Helper(hereis a full example):With Mail Helper ClassimportosfromsendgridimportSendGridAPIClientfromsendgrid.helpers.mailimportMailmessage=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)exceptExceptionase:print(str(e))TheMailconstructor creates apersonalization objectfor you.Hereis an example of how to add it.Without Mail Helper ClassThe following is the minimum needed code to send an email without the /mail/send Helper
(hereis a full example):importosfromsendgridimportSendGridAPIClientmessage={'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)exceptExceptionase:print(str(e))General v3 Web API Usage (WithFluent Interface)importosfromsendgridimportSendGridAPIClientsg=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 (WithoutFluent Interface)importosfromsendgridimportSendGridAPIClientsg=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 EmailPlease seeour helperfor utilizing our Inbound Parse webhook.UsageTwilio SendGrid DocumentationLibrary Usage DocumentationExample CodeHow-to: Migration from v2 to v3v3 Web API Mail Send Helper- build a request object payload for a v3 /mail/send API call.Processing Inbound EmailUse CasesExamples 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.AnnouncementsAll updates to this library are documented in ourCHANGELOGandreleases.How to ContributeWe encourage contribution to our libraries (you might even score some nifty swag), please see ourCONTRIBUTINGguide for details.Quick links:Feature RequestBug ReportsImprovements to the CodebaseReview Pull RequestsTroubleshootingPlease see ourtroubleshooting guidefor common library issues.Aboutsendgrid-pythonis maintained and funded by Twilio SendGrid, Inc.
The names and logos forsendgrid-pythonare trademarks of Twilio SendGrid, Inc.LicenseThe MIT License (MIT)