-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding mailgun sample #132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lib |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
runtime: python27 | ||
threadsafe: yes | ||
api_version: 1 | ||
|
||
handlers: | ||
- url: .* | ||
script: main.app |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2015 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from google.appengine.ext import vendor | ||
|
||
# Add any libraries installed in the "lib" folder. | ||
vendor.add('lib') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2015 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Sample Google App Engine application that sends mail using Mailgun. | ||
""" | ||
from urllib import urlencode | ||
|
||
import httplib2 | ||
import webapp2 | ||
|
||
|
||
# Your Mailgun Domain Name | ||
MAILGUN_DOMAIN_NAME = 'your-mailgun-domain-name' | ||
# Your Mailgun API key | ||
MAILGUN_API_KEY = 'your-mailgun-api-key' | ||
|
||
|
||
# [START simple_message] | ||
def send_simple_message(recipient): | ||
http = httplib2.Http() | ||
http.add_credentials('api', MAILGUN_API_KEY) | ||
|
||
url = 'https://api.mailgun.net/v3/{}/messages'.format(MAILGUN_DOMAIN_NAME) | ||
data = { | ||
'from': 'Example Sender <mailgun@{}>'.format(MAILGUN_DOMAIN_NAME), | ||
'to': recipient, | ||
'subject': 'This is an example email from Mailgun', | ||
'text': 'Test message from Mailgun' | ||
} | ||
|
||
resp, content = http.request(url, 'POST', urlencode(data)) | ||
|
||
if resp.status != 200: | ||
raise RuntimeError( | ||
'Mailgun API error: {} {}'.format(resp.status, content)) | ||
# [END simple_message] | ||
|
||
|
||
# [START complex_message] | ||
def send_complex_message(recipient): | ||
http = httplib2.Http() | ||
http.add_credentials('api', MAILGUN_API_KEY) | ||
|
||
url = 'https://api.mailgun.net/v3/{}/messages'.format(MAILGUN_DOMAIN_NAME) | ||
data = { | ||
'from': 'Example Sender <mailgun@{}>'.format(MAILGUN_DOMAIN_NAME), | ||
'to': recipient, | ||
'subject': 'This is an example email from Mailgun', | ||
'text': 'Test message from Mailgun', | ||
'html': '<html>HTML <strong>version</strong> of the body</html>' | ||
} | ||
|
||
resp, content = http.request(url, 'POST', urlencode(data)) | ||
|
||
if resp.status != 200: | ||
raise RuntimeError( | ||
'Mailgun API error: {} {}'.format(resp.status, content)) | ||
# [END complex_message] | ||
|
||
|
||
class MainPage(webapp2.RequestHandler): | ||
def get(self): | ||
self.response.content_type = 'text/html' | ||
self.response.write(""" | ||
<!doctype html> | ||
<html><body> | ||
<form method="POST"> | ||
<input type="text" name="recipient" placeholder="Enter recipient email"> | ||
<input type="submit" name="submit" value="Send simple email"> | ||
<input type="submit" name="submit" value="Send complex email"> | ||
</form> | ||
</body></html> | ||
""") | ||
|
||
def post(self): | ||
recipient = self.request.get('recipient') | ||
action = self.request.get('submit') | ||
|
||
if action == 'Send simple email': | ||
send_simple_message(recipient) | ||
else: | ||
send_complex_message(recipient) | ||
|
||
self.response.write('Mail sent') | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
('/', MainPage) | ||
], debug=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from tests import AppEngineTestbedCase, Http2Mock | ||
import webtest | ||
|
||
from . import main | ||
|
||
|
||
class TestMailgunHandlers(AppEngineTestbedCase): | ||
def setUp(self): | ||
super(TestMailgunHandlers, self).setUp() | ||
|
||
self.app = webtest.TestApp(main.app) | ||
|
||
def test_get(self): | ||
response = self.app.get('/') | ||
self.assertEqual(response.status_int, 200) | ||
|
||
def test_post(self): | ||
http = Http2Mock(responses=[{}]) | ||
|
||
with http: | ||
response = self.app.post('/', { | ||
'recipient': '[email protected]', | ||
'submit': 'Send simple email'}) | ||
|
||
self.assertEqual(response.status_int, 200) | ||
|
||
http = Http2Mock(responses=[{}]) | ||
|
||
with http: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could assert http was called, check the data it was called with matches what's expected. That's probably more than necessary, just food for thought. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coverage handles that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha |
||
response = self.app.post('/', { | ||
'recipient': '[email protected]', | ||
'submit': 'Send complex email'}) | ||
|
||
self.assertEqual(response.status_int, 200) | ||
|
||
http = Http2Mock(responses=[{'status': 500, 'body': 'Test error'}]) | ||
|
||
with http, self.assertRaises(Exception): | ||
self.app.post('/', { | ||
'recipient': '[email protected]', | ||
'submit': 'Send simple email'}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
httplib2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
license? technically code so I think it needs it.