Skip to content

Commit 80c63c5

Browse files
author
Jon Wayne Parrott
committed
Updating standard sendgrid sample.
Change-Id: Ib02f29768a64fc61503af6021e9a63c30549a96c
1 parent 67c0c1d commit 80c63c5

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

appengine/standard/sendgrid/main.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,31 @@
1616

1717
# [START sendgrid-imp]
1818
import sendgrid
19+
from sendgrid.helpers import mail
1920
# [END sendgrid-imp]
2021
import webapp2
2122

2223
# make a secure connection to SendGrid
2324
# [START sendgrid-config]
2425
SENDGRID_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

3130
def 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):
5961
class 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

6869
app = webapp2.WSGIApplication([

appengine/standard/sendgrid/main_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ def test_get(app):
3232
@mock.patch.object(main.sg, 'send', return_value=(200, "OK"))
3333
def test_post(send_mock, app):
3434
app.post('/send', {
35-
'recipient': 'waprin@google.com'
35+
'recipient': 'user@example.com'
3636
})
3737
send_mock.assert_called_once_with(mock.ANY)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sendgrid==2.2.1
1+
sendgrid==3.0.0

0 commit comments

Comments
 (0)