Skip to content

Commit

Permalink
Updating standard sendgrid sample.
Browse files Browse the repository at this point in the history
Change-Id: Ib02f29768a64fc61503af6021e9a63c30549a96c
  • Loading branch information
Jon Wayne Parrott committed Jul 6, 2016
1 parent 67c0c1d commit 80c63c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
33 changes: 17 additions & 16 deletions appengine/standard/sendgrid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,31 @@

# [START sendgrid-imp]
import sendgrid
from sendgrid.helpers import mail
# [END sendgrid-imp]
import webapp2

# make a secure connection to SendGrid
# [START sendgrid-config]
SENDGRID_API_KEY = 'your-sendgrid-api-key'
SENDGRID_DOMAIN = 'your-sendgrid-domain'
SENDGRID_SENDER = 'your-sendgrid-sender'
# [END sendgrid-config]

sg = sendgrid.SendGridClient(SENDGRID_API_KEY)


def send_simple_message(recipient):
# [START sendgrid-send]
message = sendgrid.Mail()
message.set_subject('message subject')
message.set_html('<strong>HTML message body</strong>')
message.set_text('plaintext message body')
message.set_from('Example App Engine Sender <sendgrid@{}>'.format(
SENDGRID_DOMAIN))
message.add_to(recipient)
status, msg = sg.send(message)
return (status, msg)

sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)

to_email = mail.Email(recipient)
from_email = mail.Email(SENDGRID_SENDER)
subject = 'This is a test email'
content = mail.Content('text/plain', 'Example message.')
message = mail.Mail(from_email, subject, to_email, content)

response = sg.client.mail.send.post(request_body=message.get())

return response
# [END sendgrid-send]


Expand All @@ -59,10 +61,9 @@ def get(self):
class SendEmailHandler(webapp2.RequestHandler):
def post(self):
recipient = self.request.get('recipient')
(status, msg) = send_simple_message(recipient)
self.response.set_status(status)
if status == 200:
self.response.write(msg)
sg_response = send_simple_message(recipient)
self.response.set_status(sg_response.status_code)
self.response.write(sg_response.body)


app = webapp2.WSGIApplication([
Expand Down
2 changes: 1 addition & 1 deletion appengine/standard/sendgrid/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def test_get(app):
@mock.patch.object(main.sg, 'send', return_value=(200, "OK"))
def test_post(send_mock, app):
app.post('/send', {
'recipient': 'waprin@google.com'
'recipient': 'user@example.com'
})
send_mock.assert_called_once_with(mock.ANY)
2 changes: 1 addition & 1 deletion appengine/standard/sendgrid/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sendgrid==2.2.1
sendgrid==3.0.0

0 comments on commit 80c63c5

Please sign in to comment.