-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
44 lines (35 loc) · 1.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import flash, Flask, Markup, redirect, render_template, request
from sparkpost import SparkPost
from sparkpost.exceptions import SparkPostAPIException
# SparkPost relies upon requests so we must patch it up to work
# in Google App Engine.
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()
app = Flask(__name__)
app.secret_key = 'Sshhh. This is a secret!'
sp = SparkPost()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/send', methods=['POST'])
def send():
recipient = request.form.get('email')
try:
sp.transmissions.send(
use_sandbox=True,
from_email='[email protected]',
recipients=[recipient],
subject='Hello From Google App Engine!',
html='<h1>Google App Engine + Python + SparkPost = Epic!</h1>'
)
except SparkPostAPIException as e:
errors = e.response.json()['errors']
for err in errors:
msg = err.get('message')
desc = err.get('description')
flash(Markup('<strong>{}</strong>: {}'.format(msg, desc)))
return render_template('index.html', err=True)
flash('Email sent!')
return redirect('/')
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)