16
16
17
17
# [START sendgrid-imp]
18
18
import sendgrid
19
+ from sendgrid .helpers import mail
19
20
# [END sendgrid-imp]
20
21
import webapp2
21
22
22
23
# make a secure connection to SendGrid
23
24
# [START sendgrid-config]
24
25
SENDGRID_API_KEY = 'your-sendgrid-api-key'
25
- SENDGRID_DOMAIN = 'your-sendgrid-domain '
26
+ SENDGRID_SENDER = 'your-sendgrid-sender '
26
27
# [END sendgrid-config]
27
28
28
- sg = sendgrid .SendGridClient (SENDGRID_API_KEY )
29
-
30
29
31
30
def send_simple_message (recipient ):
32
31
# [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
42
44
# [END sendgrid-send]
43
45
44
46
@@ -59,10 +61,9 @@ def get(self):
59
61
class SendEmailHandler (webapp2 .RequestHandler ):
60
62
def post (self ):
61
63
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 )
66
67
67
68
68
69
app = webapp2 .WSGIApplication ([
0 commit comments