Featured image of post How to Send Email With Sendgrid Api

How to Send Email With Sendgrid Api

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sendgrid
#import os
from sendgrid.helpers.mail import Mail, Email, To, Content

#export SENDGRID_API_KEY="API_KEY"
#echo $SENDGRID_API_KEY
SENDGRID_API_KEY="API_KEY"
print(SENDGRID_API_KEY)
#sg = sendgrid.SendGridAPIClient(api_key=os.environ.get(SENDGRID_API_KEY))
sg = sendgrid.SendGridAPIClient(api_key=SENDGRID_API_KEY)
from_email = Email("sender@sender.com")  # Change to your verified sender
to_email = To("receiver@receiver.com")  # Change to your recipient
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)

# Get a JSON-ready representation of the Mail object
mail_json = mail.get()

# Send an HTTP POST request to /mail/send
response = sg.client.mail.send.post(request_body=mail_json)
print(response.status_code)
print(response.headers)
Built with Hugo
Theme Stack designed by Jimmy