The sendgrid
package provides some useful emailing capabilities. 📬 ✉️
This package is especially useful due to its integration with Heroku.
Reference:
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
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)