|
| 1 | +// Copyright 2015, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +var express = require('express'); |
| 17 | +var path = require('path'); |
| 18 | +var bodyParser = require('body-parser'); |
| 19 | + |
| 20 | +// [START setup] |
| 21 | +var Mailgun = require('mailgun').Mailgun; |
| 22 | +var mg = new Mailgun(process.env.MAILGUN_API_KEY); |
| 23 | +// [END setup] |
| 24 | + |
| 25 | +var app = express(); |
| 26 | + |
| 27 | +// Setup view engine |
| 28 | +app.set('views', path.join(__dirname, 'views')); |
| 29 | +app.set('view engine', 'jade'); |
| 30 | + |
| 31 | +// Parse form data |
| 32 | +app.use(bodyParser.json()); |
| 33 | +app.use(bodyParser.urlencoded({ extended: false })); |
| 34 | + |
| 35 | +// [START index] |
| 36 | +app.get('/', function(req, res) { |
| 37 | + res.render('index'); |
| 38 | +}); |
| 39 | +// [END index] |
| 40 | + |
| 41 | +// [START hello] |
| 42 | +app.post('/hello', function(req, res, next) { |
| 43 | + var servername = ''; |
| 44 | + var options = {}; |
| 45 | + |
| 46 | + mg.sendText( |
| 47 | + // From |
| 48 | + |
| 49 | + // To |
| 50 | + req.body.email, |
| 51 | + // Subject |
| 52 | + 'Hello World!', |
| 53 | + // Body |
| 54 | + 'Mailgun on Google App Engine with Node.js', |
| 55 | + servername, |
| 56 | + options, |
| 57 | + function (err) { |
| 58 | + if (err) { |
| 59 | + return next(err); |
| 60 | + } else { |
| 61 | + // Render the index route on success |
| 62 | + return res.render('index', { |
| 63 | + sent: true |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + ); |
| 68 | +}); |
| 69 | +// [END hello] |
| 70 | + |
| 71 | +// [START server] |
| 72 | +var server = app.listen( |
| 73 | + process.env.PORT || 8080, |
| 74 | + '0.0.0.0', |
| 75 | + function () { |
| 76 | + var address = server.address().address; |
| 77 | + var port = server.address().port; |
| 78 | + console.log('App listening at http://%s:%s', address, port); |
| 79 | + console.log('Press Ctrl+C to quit.'); |
| 80 | + } |
| 81 | +); |
| 82 | +// [END server] |
| 83 | + |
0 commit comments