Skip to content

Latest commit

 

History

History
66 lines (43 loc) · 1.78 KB

File metadata and controls

66 lines (43 loc) · 1.78 KB

Python Language Overview

The sendgrid Package

The sendgrid package provides some useful emailing capabilities. 📬 ✉️

This package is especially useful due to its integration with Heroku.

Reference:

Installation

Install sendgrid, if necessary:

# For Pipenv users (Mac or Windows), run from a project's root directory:
pipenv install sendgrid

# For Homebrew-installed Python 3.x on Mac OS:
pip3 install sendgrid

# All others:
pip install sendgrid

Usage

To setup this example, first ensure you have set an environment variable called SENDGRID_API_KEY to facilitate authentication with the SendGrid email service. For instructions, follow the Heroku SendGrid Guide.

Send yourself an email:

import os
import sendgrid
from sendgrid.helpers.mail import * # source of Email, Content, Mail, etc.

# AUTHENTICATE

SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')

sg = sendgrid.SendGridAPIClient(apikey = SENDGRID_API_KEY)

# COMPILE REQUEST PARAMETERS

subject = "Hello World from the SendGrid Python Library!"
my_email = Email("my_address@gmail.com")
from_email = my_email
to_email = my_email
content = Content("text/plain", "Hello, Email!")
mail = Mail(from_email, subject, to_email, content)

# ISSUE REQUEST

response = sg.client.mail.send.post(request_body=mail.get())

# PARSE RESPONSE

print(response.status_code)
print(response.body)
print(response.headers)