Skip to content

Instantly share code, notes, and snippets.

@steveoh
Last active May 4, 2023 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveoh/efa7c2c6f660735145c40797f486ef22 to your computer and use it in GitHub Desktop.
Save steveoh/efa7c2c6f660735145c40797f486ef22 to your computer and use it in GitHub Desktop.
Sending email with SendGrid

Email Test

  1. install dependencies
pip install sendgrid
  1. Create an api key in sendgrid
  2. Plug it in on line 13
  3. Run the script
python main.py
#!/usr/bin/env python
# * coding: utf8 *
"""
main.py - A script that send an email with sendgrid
"""
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from_address = 'no-reply@utah.gov'
to = ['dford@utah.gov', 'pfunk@utah.gov']
api_key = 'SG....'
message = Mail(from_email=from_address, to_emails=to, subject='email is working', html_content='<strong>hi</strong>')
try:
client = SendGridAPIClient(api_key)
response = client.send(message)
print(response.status_code, response.body, response.headers)
except Exception as e:
print(f'Error sending email with SendGrid: {e}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment