Skip to content

Commit c4eb175

Browse files
author
Bill Prin
committed
Add Sengrid example
1 parent 7aa1689 commit c4eb175

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

appengine/sendgrid/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Sendgrid & Google App Engine
2+
3+
This sample application demonstrates how to use [Sendgrid with Google App Engine](https://cloud.google.com/appengine/docs/python/mail/sendgrid)
4+
5+
Refer to the [App Engine Samples README](../../README.md) for information on how to run and deploy this sample.
6+
7+
# Setup
8+
9+
Before running this sample:
10+
11+
1. You will need a [Sendgrid account](http://sendgrid.com/partner/google).
12+
2. Update the `SENGRID_DOMAIN_NAME` and `SENGRID_API_KEY` constants in `main.py`. You can use
13+
the [Sendgrid sandbox domain](https://support.sendgrid.com/hc/en-us/articles/201995663-Safely-Test-Your-Sending-Speed).

appengine/sendgrid/app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: python27
2+
threadsafe: yes
3+
api_version: 1
4+
5+
handlers:
6+
- url: .*
7+
script: main.app
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.ext import vendor
16+
17+
# Add any libraries installed in the "lib" folder.
18+
vendor.add('lib')

appengine/sendgrid/main.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import sendgrid
18+
import webapp2
19+
20+
# make a secure connection to SendGrid
21+
SENDGRID_API_KEY = 'your-sendgrid-api-key'
22+
SENDGRID_DOMAIN = 'your-sendgrid-domain'
23+
24+
sg = sendgrid.SendGridClient(SENDGRID_API_KEY)
25+
26+
27+
def send_simple_message(recipient):
28+
message = sendgrid.Mail()
29+
message.set_subject('message subject')
30+
message.set_html('<strong>HTML message body</strong>')
31+
message.set_text('plaintext message body')
32+
message.set_from('from: Example Sender <mailgun@{}>'.format(
33+
SENDGRID_DOMAIN))
34+
message.set_from('App Engine App <sendgrid@{}>'.format(SENDGRID_DOMAIN))
35+
message.add_to(recipient)
36+
37+
# use the Web API to send your message
38+
39+
status, msg = sg.send(message)
40+
print "CALLIGN SEND %s done: %s %s" % (str(sg.send), status, msg)
41+
return (status, msg)
42+
43+
44+
class MainPage(webapp2.RequestHandler):
45+
def get(self):
46+
self.response.content_type = 'text/html'
47+
self.response.write("""
48+
<!doctype html>
49+
<html><body>
50+
<form action="/send" method="POST">
51+
<input type="text" name="recipient" placeholder="Enter recipient email">
52+
<input type="submit" name="submit" value="Send simple email">
53+
</form>
54+
</body></html>
55+
""")
56+
57+
58+
class SendEmailHandler(webapp2.RequestHandler):
59+
60+
def post(self):
61+
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)
66+
67+
68+
app = webapp2.WSGIApplication([
69+
('/', MainPage),
70+
('/send', SendEmailHandler)
71+
], debug=True)

appengine/sendgrid/main_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import main
16+
17+
import mock
18+
import pytest
19+
import webtest
20+
21+
22+
@pytest.fixture
23+
def app():
24+
return webtest.TestApp(main.app)
25+
26+
27+
def test_get(app):
28+
response = app.get('/')
29+
assert response.status_int == 200
30+
31+
32+
@mock.patch.object(main.sg, 'send', return_value=(200, "OK"))
33+
def test_post(send_mock, app):
34+
app.post('/send', {
35+
'recipient': '[email protected]'
36+
})
37+
send_mock.assert_called_once_with(mock.ANY)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sendgrid==2.2.1

0 commit comments

Comments
 (0)