Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 48a3be6

Browse filesBrowse files
author
Jon Wayne Parrott
committed
Adding mailjet samples for standard and flex (GoogleCloudPlatform#318)
Change-Id: I64b2fd1d4e1176e9fcbecf2f75592e78007e6a8a
1 parent ffc65cf commit 48a3be6
Copy full SHA for 48a3be6

File tree

Expand file treeCollapse file tree

17 files changed

+446
-30
lines changed
Filter options
Expand file treeCollapse file tree

17 files changed

+446
-30
lines changed

‎appengine/mailjet/.gitignore

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

‎appengine/mailjet/README.md

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python Mailjet email sample for Google App Engine Standard
2+
3+
This sample demonstrates how to use [Mailjet](https://www.mailgun.com) on [Google App Engine Standard](https://cloud.google.com/appengine/docs/).
4+
5+
## Setup
6+
7+
Before you can run or deploy the sample, you will need to do the following:
8+
9+
1. [Create a Mailjet Account](http://www.mailjet.com/google).
10+
11+
2. Configure your Mailjet settings in the environment variables section in ``app.yaml``.

‎appengine/mailjet/app.yaml

Copy file name to clipboard
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
runtime: python27
2+
threadsafe: yes
3+
api_version: 1
4+
5+
handlers:
6+
- url: .*
7+
script: main.app
8+
9+
# [START env_variables]
10+
env_variables:
11+
MAILJET_API_KEY: your-mailjet-api-key
12+
MAILJET_API_SECRET: your-mailjet-api-secret
13+
MAILJET_SENDER: your-mailjet-sender-address
14+
# [END env_variables]

‎appengine/mailjet/appengine_config.py

Copy file name to clipboard
+18Lines 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/mailjet/main.py

Copy file name to clipboard
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
# [START app]
16+
import logging
17+
import os
18+
19+
from flask import Flask, render_template, request
20+
# [start config]
21+
import mailjet_rest
22+
import requests_toolbelt.adapters.appengine
23+
24+
# Use the App Engine requests adapter to allow the requests library to be
25+
# used on App Engine.
26+
requests_toolbelt.adapters.appengine.monkeypatch()
27+
28+
MAILJET_API_KEY = os.environ['MAILJET_API_KEY']
29+
MAILJET_API_SECRET = os.environ['MAILJET_API_SECRET']
30+
MAILJET_SENDER = os.environ['MAILJET_SENDER']
31+
# [END config]
32+
33+
app = Flask(__name__)
34+
35+
36+
# [START send_message]
37+
def send_message(to):
38+
client = mailjet_rest.Client(
39+
auth=(MAILJET_API_KEY, MAILJET_API_SECRET))
40+
41+
data = {
42+
'FromEmail': MAILJET_SENDER,
43+
'FromName': 'App Engine Flex Mailjet Sample',
44+
'Subject': 'Example email.',
45+
'Text-part': 'This is an example email.',
46+
'Html-part': 'This is an <i>example</i> email.',
47+
'Recipients': [{'Email': to}]
48+
}
49+
50+
result = client.send.create(data=data)
51+
52+
return result.json()
53+
# [END send_message]
54+
55+
56+
@app.route('/')
57+
def index():
58+
return render_template('index.html')
59+
60+
61+
@app.route('/send/email', methods=['POST'])
62+
def send_email():
63+
to = request.form.get('to')
64+
65+
result = send_message(to)
66+
67+
return 'Email sent, response: <pre>{}</pre>'.format(result)
68+
69+
70+
@app.errorhandler(500)
71+
def server_error(e):
72+
logging.exception('An error ocurred during a request.')
73+
return """
74+
An internal error occurred: <pre>{}</pre>
75+
See logs for full stacktrace.
76+
""".format(e), 500
77+
# [END app]

‎appengine/mailjet/main_test.py

Copy file name to clipboard
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 re
16+
17+
import pytest
18+
import responses
19+
20+
21+
@pytest.fixture
22+
def app(monkeypatch):
23+
monkeypatch.setenv('MAILJET_API_KEY', 'apikey')
24+
monkeypatch.setenv('MAILJET_API_SECRET', 'apisecret')
25+
monkeypatch.setenv('MAILJET_SENDER', 'sender')
26+
27+
import main
28+
29+
main.app.testing = True
30+
return main.app.test_client()
31+
32+
33+
def test_index(app):
34+
r = app.get('/')
35+
assert r.status_code == 200
36+
37+
38+
@responses.activate
39+
def test_send_email(app):
40+
responses.add(
41+
responses.POST,
42+
re.compile(r'.*'),
43+
body='{"test": "message"}',
44+
content_type='application/json')
45+
46+
r = app.post('/send/email', data={'to': 'user@example.com'})
47+
48+
assert r.status_code == 200
49+
assert 'test' in r.data.decode('utf-8')
50+
51+
assert len(responses.calls) == 1
52+
request_body = responses.calls[0].request.body
53+
assert 'user@example.com' in request_body

‎appengine/mailjet/requirements.txt

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask==0.10.1
2+
requests==2.10.0
3+
requests-toolbelt==0.6.0
4+
mailjet_rest==1.1.1
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{#
2+
# Copyright 2016 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#}
16+
<!doctype html>
17+
<html>
18+
<head>
19+
<title>Mailjet on Google App Engine</title>
20+
</head>
21+
<body>
22+
<!-- [START form] -->
23+
<form method="post" action="/send/email">
24+
<input type="text" name="to" placeholder="Enter recipient email">
25+
<input type="submit" name="submit" value="Send email">
26+
</form>
27+
<!-- [END form] -->
28+
</body>
29+
</html>

‎managed_vms/mailjet/README.md

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Python Mailjet email sample for Google App Engine Flexible
2+
3+
This sample demonstrates how to use [Mailjet](https://www.mailgun.com) on [Google App Engine Flexible](https://cloud.google.com/appengine/docs/flexible/).
4+
5+
## Setup
6+
7+
Before you can run or deploy the sample, you will need to do the following:
8+
9+
1. [Create a Mailjet Account](http://www.mailjet.com/google).
10+
11+
2. Configure your Mailjet settings in the environment variables section in ``app.yaml``.
12+
13+
## Running locally
14+
15+
Refer to the [top-level README](../README.md) for instructions on running and deploying.
16+
17+
You can run the application locally and send emails from your local machine. You
18+
will need to set environment variables before starting your application:
19+
20+
$ export MAILGUN_API_KEY=[your-mailgun-api-key]
21+
$ export MAILGUN_API_SECRET=[your-mailgun-secret]
22+
$ export MAILGUN_SENDER=[your-sender-address]
23+
$ python main.py

‎managed_vms/mailjet/app.yaml

Copy file name to clipboard
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: python
2+
vm: true
3+
entrypoint: gunicorn -b :$PORT main:app
4+
5+
runtime_config:
6+
python_version: 3
7+
8+
# [START env_variables]
9+
env_variables:
10+
MAILJET_API_KEY: your-mailjet-api-key
11+
MAILJET_API_SECRET: your-mailjet-api-secret
12+
MAILJET_SENDER: your-mailjet-sender-address
13+
# [END env_variables]

‎managed_vms/mailjet/main.py

Copy file name to clipboard
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
# [START app]
16+
import logging
17+
import os
18+
19+
from flask import Flask, render_template, request
20+
# [start config]
21+
import mailjet_rest
22+
23+
MAILJET_API_KEY = os.environ['MAILJET_API_KEY']
24+
MAILJET_API_SECRET = os.environ['MAILJET_API_SECRET']
25+
MAILJET_SENDER = os.environ['MAILJET_SENDER']
26+
# [END config]
27+
28+
app = Flask(__name__)
29+
30+
31+
# [START send_message]
32+
def send_message(to):
33+
client = mailjet_rest.Client(
34+
auth=(MAILJET_API_KEY, MAILJET_API_SECRET))
35+
36+
data = {
37+
'FromEmail': MAILJET_SENDER,
38+
'FromName': 'App Engine Flex Mailjet Sample',
39+
'Subject': 'Example email.',
40+
'Text-part': 'This is an example email.',
41+
'Html-part': 'This is an <i>example</i> email.',
42+
'Recipients': [{'Email': to}]
43+
}
44+
45+
result = client.send.create(data=data)
46+
47+
return result.json()
48+
# [END send_message]
49+
50+
51+
@app.route('/')
52+
def index():
53+
return render_template('index.html')
54+
55+
56+
@app.route('/send/email', methods=['POST'])
57+
def send_email():
58+
to = request.form.get('to')
59+
60+
result = send_message(to)
61+
62+
return 'Email sent, response: <pre>{}</pre>'.format(result)
63+
64+
65+
@app.errorhandler(500)
66+
def server_error(e):
67+
logging.exception('An error ocurred during a request.')
68+
return """
69+
An internal error occurred: <pre>{}</pre>
70+
See logs for full stacktrace.
71+
""".format(e), 500
72+
73+
74+
if __name__ == '__main__':
75+
# This is used when running locally. Gunicorn is used to run the
76+
# application on Google App Engine. See entrypoint in app.yaml.
77+
app.run(host='127.0.0.1', port=8080, debug=True)
78+
# [END app]

‎managed_vms/mailjet/main_test.py

Copy file name to clipboard
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 re
16+
17+
import pytest
18+
import responses
19+
20+
21+
@pytest.fixture
22+
def app(monkeypatch):
23+
monkeypatch.setenv('MAILJET_API_KEY', 'apikey')
24+
monkeypatch.setenv('MAILJET_API_SECRET', 'apisecret')
25+
monkeypatch.setenv('MAILJET_SENDER', 'sender')
26+
27+
import main
28+
29+
main.app.testing = True
30+
return main.app.test_client()
31+
32+
33+
def test_index(app):
34+
r = app.get('/')
35+
assert r.status_code == 200
36+
37+
38+
@responses.activate
39+
def test_send_email(app):
40+
responses.add(
41+
responses.POST,
42+
re.compile(r'.*'),
43+
body='{"test": "message"}',
44+
content_type='application/json')
45+
46+
r = app.post('/send/email', data={'to': 'user@example.com'})
47+
48+
assert r.status_code == 200
49+
assert 'test' in r.data.decode('utf-8')
50+
51+
assert len(responses.calls) == 1
52+
request_body = responses.calls[0].request.body
53+
assert 'user@example.com' in request_body

‎managed_vms/mailjet/requirements.txt

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask==0.10.1
2+
gunicorn==19.4.5
3+
requests[security]==2.9.1
4+
mailjet_rest==1.1.1

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.