|
| 1 | +// Copyright 2016, 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 Mailjet = require('node-mailjet').connect( |
| 22 | + process.env.MJ_APIKEY_PUBLIC, |
| 23 | + process.env.MJ_APIKEY_PRIVATE |
| 24 | +); |
| 25 | +// [END setup] |
| 26 | + |
| 27 | +var app = express(); |
| 28 | + |
| 29 | +// Setup view engine |
| 30 | +app.set('views', path.join(__dirname, 'views')); |
| 31 | +app.set('view engine', 'jade'); |
| 32 | + |
| 33 | +// Parse form data |
| 34 | +app.use(bodyParser.json()); |
| 35 | +app.use(bodyParser.urlencoded({ extended: false })); |
| 36 | + |
| 37 | +// [START index] |
| 38 | +app.get('/', function(req, res) { |
| 39 | + res.render('index'); |
| 40 | +}); |
| 41 | +// [END index] |
| 42 | + |
| 43 | +// [START hello] |
| 44 | +app.post('/hello', function(req, res, next) { |
| 45 | + var options = { |
| 46 | + // From |
| 47 | + FromEmail: 'no-reply@appengine-mailjet-demo.com', |
| 48 | + FromName: 'Mailjet Demo', |
| 49 | + // To |
| 50 | + Recipients: [{ Email: req.body.email }], |
| 51 | + // Subject |
| 52 | + Subject: 'Hello World!', |
| 53 | + // Body |
| 54 | + 'Text-part': 'Mailjet on Google App Engine with Node.js', |
| 55 | + 'Html-part': '<h3>Mailjet on Google App Engine with Node.js</h3>' |
| 56 | + }; |
| 57 | + |
| 58 | + var request = Mailjet.post('send').request(options); |
| 59 | + |
| 60 | + request |
| 61 | + .on('success', function (response, body) { |
| 62 | + console.log(response.statusCode, body); |
| 63 | + // Render the index route on success |
| 64 | + return res.render('index', { |
| 65 | + sent: true |
| 66 | + }); |
| 67 | + }) |
| 68 | + .on('error', function (err) { |
| 69 | + return next(err); |
| 70 | + }); |
| 71 | +}); |
| 72 | +// [END hello] |
| 73 | + |
| 74 | +// [START server] |
| 75 | +var server = app.listen( |
| 76 | + process.env.PORT || 8080, |
| 77 | + '0.0.0.0', |
| 78 | + function () { |
| 79 | + var address = server.address().address; |
| 80 | + var port = server.address().port; |
| 81 | + console.log('App listening at http://%s:%s', address, port); |
| 82 | + console.log('Press Ctrl+C to quit.'); |
| 83 | + } |
| 84 | +); |
| 85 | +// [END server] |
| 86 | + |
0 commit comments