diff --git a/.travis.yml b/.travis.yml index 3561dafc31..24fc2fa9ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,6 +36,7 @@ cache: - appengine/logging/node_modules/ - appengine/loopback/node_modules/ - appengine/mailgun/node_modules/ + - appengine/mailjet/node_modules/ - appengine/memcached/node_modules/ - appengine/mongodb/node_modules/ - appengine/parse-server/node_modules/ @@ -49,7 +50,8 @@ cache: - appengine/twilio/node_modules/ - appengine/webpack/node_modules/ - appengine/websockets/node_modules/ - - computeengine/sendgrid/node_modules/ + - bigquery/node_modules/ + - computeengine/node_modules/ - datastore/node_modules/ - functions/uuid/node_modules/ - logging/node_modules/ diff --git a/appengine/mailjet/README.md b/appengine/mailjet/README.md new file mode 100644 index 0000000000..be4567a852 --- /dev/null +++ b/appengine/mailjet/README.md @@ -0,0 +1,16 @@ +## Express.js + Mailjet on Google App Engine + +> [Mailjet][1] is an all-in-one email service provider. +> +> – www.mailjet.com + +This sample application demonstrates how to use [Express.js][2] and +[node-mailjet][3] to send transactional email on [Google App Engine][4]. + +You can also read the [Mailjet documentation][5]. + +[1]: https://www.mailjet.com/ +[2]: http://expressjs.com +[3]: https://github.com/mailjet/mailjet-apiv3-nodejs +[4]: https://cloud.google.com/appengine +[5]: https://dev.mailjet.com/ diff --git a/appengine/mailjet/app.js b/appengine/mailjet/app.js new file mode 100644 index 0000000000..91b649451a --- /dev/null +++ b/appengine/mailjet/app.js @@ -0,0 +1,86 @@ +// Copyright 2016, 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. + +'use strict'; + +var express = require('express'); +var path = require('path'); +var bodyParser = require('body-parser'); + +// [START setup] +var Mailjet = require('node-mailjet').connect( + process.env.MJ_APIKEY_PUBLIC, + process.env.MJ_APIKEY_PRIVATE +); +// [END setup] + +var app = express(); + +// Setup view engine +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'jade'); + +// Parse form data +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); + +// [START index] +app.get('/', function(req, res) { + res.render('index'); +}); +// [END index] + +// [START hello] +app.post('/hello', function(req, res, next) { + var options = { + // From + FromEmail: 'no-reply@appengine-mailjet-demo.com', + FromName: 'Mailjet Demo', + // To + Recipients: [{ Email: req.body.email }], + // Subject + Subject: 'Hello World!', + // Body + 'Text-part': 'Mailjet on Google App Engine with Node.js', + 'Html-part': '

Mailjet on Google App Engine with Node.js

' + }; + + var request = Mailjet.post('send').request(options); + + request + .on('success', function (response, body) { + console.log(response.statusCode, body); + // Render the index route on success + return res.render('index', { + sent: true + }); + }) + .on('error', function (err) { + return next(err); + }); +}); +// [END hello] + +// [START server] +var server = app.listen( + process.env.PORT || 8080, + '0.0.0.0', + function () { + var address = server.address().address; + var port = server.address().port; + console.log('App listening at http://%s:%s', address, port); + console.log('Press Ctrl+C to quit.'); + } +); +// [END server] + diff --git a/appengine/mailjet/app.yaml b/appengine/mailjet/app.yaml new file mode 100644 index 0000000000..2f0fbe307c --- /dev/null +++ b/appengine/mailjet/app.yaml @@ -0,0 +1,23 @@ +# Copyright 2016, 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. + +# [START app_yaml] +runtime: nodejs +vm: true +env_variables: + MJ_APIKEY_PUBLIC: + MJ_APIKEY_PRIVATE: + +skip_files: + - ^(.*/)?.*/node_modules/.*$ +# [END app_yaml] diff --git a/appengine/mailjet/package.json b/appengine/mailjet/package.json new file mode 100644 index 0000000000..3e4997a70e --- /dev/null +++ b/appengine/mailjet/package.json @@ -0,0 +1,21 @@ +{ + "name": "appengine-mailjet", + "description": "An example of using Mailjet in Node.js on Google App Engine.", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "author": "Google Inc.", + "engines": { + "node": "~4.2" + }, + "scripts": { + "start": "node app.js", + "deploy": "gcloud preview app deploy" + }, + "dependencies": { + "body-parser": "^1.14.2", + "express": "^4.13.4", + "jade": "^1.11.0", + "node-mailjet": "^1.1.0" + } +} diff --git a/appengine/mailjet/views/index.jade b/appengine/mailjet/views/index.jade new file mode 100644 index 0000000000..e179a807e3 --- /dev/null +++ b/appengine/mailjet/views/index.jade @@ -0,0 +1,27 @@ +// Copyright 2016, 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. + +doctype html +html + head + title= title + body + h1 Hello World! + p Express.js + Mailjet on Google App Engine. + hr + if sent + p Email sent! + else + form(name="hello", action="/hello", method="post") + input(type="email", placeholder="Enter your email to send yourself a Hello World message", name="email", style="width: 50%; margin-right: 15px;") + input(type="submit", value="Send") diff --git a/computeengine/README.md b/computeengine/README.md index bed0d54b3f..0fbd7b5983 100644 --- a/computeengine/README.md +++ b/computeengine/README.md @@ -14,10 +14,17 @@ Install dependencies: ### sendgrid.js -Also required a `SENDGRID_API_KEY` environment variable to be set. +Requires a `SENDGRID_API_KEY` environment variable to be set. npm run sendgrid +### mailjet.js + +Requires `MAILJET_API_KEY` and `MAILJET_API_SECRET` environment variables to be +set. + + npm run mailjet + ### vms.js npm run vms diff --git a/computeengine/mailjet.js b/computeengine/mailjet.js new file mode 100644 index 0000000000..3231a0141c --- /dev/null +++ b/computeengine/mailjet.js @@ -0,0 +1,40 @@ +// Copyright 2016, 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. + +'use strict'; + +// [START send] +var mailer = require('nodemailer'); +var smtp = require('nodemailer-smtp-transport'); + +var transport = mailer.createTransport(smtp({ + service: 'Mailjet', + auth: { + user: process.env.MAILJET_API_KEY || '' + } +})); + +transport.sendMail({ + from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', // From address + to: 'EMAIL@EXAMPLE.COM', // To address + subject: 'test email from Node.js on Google Cloud Platform', // Subject + text: 'Hello!\n\nThis a test email from Node.js.' // Content +}, function (err, json) { + if (err) { + console.log(err); + } else { + console.log(json); + } +}); +// [END send] diff --git a/computeengine/package.json b/computeengine/package.json index ae703c56c8..ac9dfa6383 100644 --- a/computeengine/package.json +++ b/computeengine/package.json @@ -10,12 +10,15 @@ }, "scripts": { "sendgrid": "node sendgrid.js", + "mailjet": "node mailjet.js", "vms": "node vms.js", "vms_api": "node vms_api.js" }, "dependencies": { "gcloud": "^0.30.3", "googleapis": "^4.0.0", + "nodemailer": "^2.3.2", + "nodemailer-smtp-transport": "^2.4.2", "sendgrid": "^2.0.0" } } diff --git a/test/appengine/all.test.js b/test/appengine/all.test.js index 74241cd8e0..449e248295 100644 --- a/test/appengine/all.test.js +++ b/test/appengine/all.test.js @@ -135,6 +135,12 @@ var sampleTests = [ args: ['app.js'], msg: 'Express.js + Mailgun on Google App Engine.' }, + { + dir: 'appengine/mailjet', + cmd: 'node', + args: ['app.js'], + msg: 'Express.js + Mailjet on Google App Engine.' + }, { dir: 'appengine/memcached', cmd: 'node', diff --git a/test/computeengine/mailjet.test.js b/test/computeengine/mailjet.test.js new file mode 100644 index 0000000000..bb5eb069f0 --- /dev/null +++ b/test/computeengine/mailjet.test.js @@ -0,0 +1,51 @@ +// Copyright 2016, 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. + +'use strict'; + +var test = require('ava'); +var proxyquire = require('proxyquire').noPreserveCache(); +process.env.MAILJET_API_KEY = 'foo'; +process.env.MAILJET_API_SECRET = 'bar'; + +test.cb('should send an email', function (t) { + proxyquire('../../computeengine/mailjet.js', { + nodemailer: { + createTransport: function (arg) { + t.is(arg, 'test'); + return { + sendMail: function (payload, cb) { + t.same(payload, { + from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', + to: 'EMAIL@EXAMPLE.COM', + subject: 'test email from Node.js on Google Cloud Platform', + text: 'Hello!\n\nThis a test email from Node.js.' + }); + cb('done'); + t.end(); + } + }; + } + }, + 'nodemailer-smtp-transport': function (options) { + t.same(options, { + service: 'Mailjet', + auth: { + user: 'foo', + pass: 'bar' + } + }); + return 'test'; + } + }); +});