1616
1717# [START sendgrid-imp]
1818import sendgrid
19+ from sendgrid .helpers import mail
1920# [END sendgrid-imp]
2021import webapp2
2122
2223# make a secure connection to SendGrid
2324# [START sendgrid-config]
2425SENDGRID_API_KEY = 'your-sendgrid-api-key'
25- SENDGRID_DOMAIN = 'your-sendgrid-domain '
26+ SENDGRID_SENDER = 'your-sendgrid-sender '
2627# [END sendgrid-config]
2728
28- sg = sendgrid .SendGridClient (SENDGRID_API_KEY )
29-
3029
3130def send_simple_message (recipient ):
3231 # [START sendgrid-send]
33- message = sendgrid .Mail ()
34- message .set_subject ('message subject' )
35- message .set_html ('<strong>HTML message body</strong>' )
36- message .set_text ('plaintext message body' )
37- message .set_from ('Example App Engine Sender <sendgrid@{}>' .format (
38- SENDGRID_DOMAIN ))
39- message .add_to (recipient )
40- status , msg = sg .send (message )
41- return (status , msg )
32+
33+ sg = sendgrid .SendGridAPIClient (apikey = SENDGRID_API_KEY )
34+
35+ to_email = mail .Email (recipient )
36+ from_email = mail .Email (SENDGRID_SENDER )
37+ subject = 'This is a test email'
38+ content = mail .Content ('text/plain' , 'Example message.' )
39+ message = mail .Mail (from_email , subject , to_email , content )
40+
41+ response = sg .client .mail .send .post (request_body = message .get ())
42+
43+ return response
4244 # [END sendgrid-send]
4345
4446
@@ -59,10 +61,9 @@ def get(self):
5961class SendEmailHandler (webapp2 .RequestHandler ):
6062 def post (self ):
6163 recipient = self .request .get ('recipient' )
62- (status , msg ) = send_simple_message (recipient )
63- self .response .set_status (status )
64- if status == 200 :
65- self .response .write (msg )
64+ sg_response = send_simple_message (recipient )
65+ self .response .set_status (sg_response .status_code )
66+ self .response .write (sg_response .body )
6667
6768
6869app = webapp2 .WSGIApplication ([
0 commit comments