Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

v3 API Python Code Example


(information)

Info

We recommend using SendGrid Python, our client library, available on GitHub(link takes you to an external page), with full documentation.

(information)

Info

Do you have an API Key(link takes you to an external page) yet? If not, go get one. You're going to need it to integrate!


Using SendGrid's Python Library

using-sendgrids-python-library page anchor

_19
# using SendGrid's Python Library
_19
# https://github.com/sendgrid/sendgrid-python
_19
import os
_19
from sendgrid import SendGridAPIClient
_19
from sendgrid.helpers.mail import Mail
_19
_19
message = Mail(
_19
from_email='from_email@example.com',
_19
to_emails='to@example.com',
_19
subject='Sending with Twilio SendGrid is Fun',
_19
html_content='<strong>and easy to do anywhere, even with Python</strong>')
_19
try:
_19
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
_19
response = sg.send(message)
_19
print(response.status_code)
_19
print(response.body)
_19
print(response.headers)
_19
except Exception as e:
_19
print(e.message)


Rate this page: