From 78ff1d9d9064cb3cf6040f57836a06151e3ad20e Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Wed, 21 Oct 2015 15:34:38 -0700 Subject: [PATCH] Added mailgun example. --- appengine/mailgun/README.md | 64 +++++++++++++++++++++ appengine/mailgun/app.js | 89 +++++++++++++++++++++++++++++ appengine/mailgun/app.yaml | 21 +++++++ appengine/mailgun/package.json | 21 +++++++ appengine/mailgun/views/error.jade | 19 ++++++ appengine/mailgun/views/index.jade | 25 ++++++++ appengine/mailgun/views/layout.jade | 20 +++++++ 7 files changed, 259 insertions(+) create mode 100644 appengine/mailgun/README.md create mode 100644 appengine/mailgun/app.js create mode 100644 appengine/mailgun/app.yaml create mode 100644 appengine/mailgun/package.json create mode 100644 appengine/mailgun/views/error.jade create mode 100644 appengine/mailgun/views/index.jade create mode 100644 appengine/mailgun/views/layout.jade diff --git a/appengine/mailgun/README.md b/appengine/mailgun/README.md new file mode 100644 index 00000000000..9b7e4e3bed4 --- /dev/null +++ b/appengine/mailgun/README.md @@ -0,0 +1,64 @@ +## Mailgun on Google App Engine + +> [Mailgun](https://www.mailgun.com/): The Email Service For Developers + +### Sign up for Mailgun + +1. Sign up for a [Mailgun account](https://mailgun.com/signup). +1. Add a [new domain](https://mailgun.com/app/domains). +1. Find your API key in your new domain's settings. + +### Create a new Node.js app + +This example uses [Express.js](http://expressjs.com) and +[node-mailgun](http://github.com/shz/node-mailgun). + +### Configure + +Create an `app.yaml` in the root of your application with the following +contents: + +```yaml +runtime: nodejs +vm: true +api_version: 1 +env_variables: + PORT: 8080 + MAILGUN_API_KEY: +``` + +### Start the app locally + +``` +$ MAILGUN_API_KEY= npm start +``` + +Now visit http://localhost:8080 and try sending yourself an email. + +When the app is deployed to Google Cloud Platform the `MAILGUN_API_KEY` +environment variable will automatically be supplied to the app. + +### Deploy + +Ensure your gcloud sdk is setup by running: + +``` +$ gcloud init +``` + +For convenience, you can use an npm script to run the `gcloud` command. Add +these lines to your `package.json` file: + +```json +"scripts": { + "start": "node app.js", + "deploy": "gcloud preview app deploy app.yaml --promote" +} +``` + +At the terminal you can now run the following command to deploy your +application: + +``` +$ npm deploy +``` \ No newline at end of file diff --git a/appengine/mailgun/app.js b/appengine/mailgun/app.js new file mode 100644 index 00000000000..bd7f964aa6e --- /dev/null +++ b/appengine/mailgun/app.js @@ -0,0 +1,89 @@ +// Copyright 2015, 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 http = require('http'); +var express = require('express'); +var path = require('path'); +var bodyParser = require('body-parser'); + +// [START setup] +var Mailgun = require('mailgun').Mailgun; +var mg = new Mailgun(process.env.MAILGUN_API_KEY); + +var app = express(); +// [END setup] + +var link = 'https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/mailgun'; // jshint ignore:line + +// 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', { + title: 'Hello World! Express.js + Mailgun on Google App Engine.' + }); +}); +// [END index] + +// [START hello] +app.post('/hello', function(req, res, next) { + var servername = ''; + var options = {}; + + mg.sendText( + // From + 'no-reply@appengine-mailgun-demo.com', + // To + req.body.email, + // Subject + 'Hello World!', + // Body + 'Go to ' + link + ' to read about using Mailgun on Google App Engine.', + servername, + options, + function (err) { + if (err) { + return next(new Error('Failed to send email!')); + } else { + // Render the index route on success + return res.render('index', { + title: 'Hello World! Express.js + Mailgun on Google App Engine.', + sent: true + }); + } + } + ); +}); +// [END hello] + +// [START server] +var server = http.createServer(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://' + address + ':' + port); + console.log('Press Ctrl+C to quit.'); + } +); +// [END server] + diff --git a/appengine/mailgun/app.yaml b/appengine/mailgun/app.yaml new file mode 100644 index 00000000000..4d8ff918028 --- /dev/null +++ b/appengine/mailgun/app.yaml @@ -0,0 +1,21 @@ +# Copyright 2015, 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 +api_version: 1 +env_variables: + PORT: 8080 + MAILGUN_API_KEY: +# [END app_yaml] \ No newline at end of file diff --git a/appengine/mailgun/package.json b/appengine/mailgun/package.json new file mode 100644 index 00000000000..adc63d0dd0c --- /dev/null +++ b/appengine/mailgun/package.json @@ -0,0 +1,21 @@ +{ + "name": "appengine-mailgun", + "description": "An example of using Mailgun in Node.js on Google App Engine.", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "engines": { + "node": "~0.12.7" + }, + "scripts": { + "start": "node app.js", + "deploy": "gcloud preview app deploy app.yaml --promote" + }, + "dependencies": { + "body-parser": "^1.14.1", + "express": "^4.13.3", + "jade": "^1.11.0", + "mailgun": "^0.5.0", + "serve-favicon": "^2.3.0" + } +} diff --git a/appengine/mailgun/views/error.jade b/appengine/mailgun/views/error.jade new file mode 100644 index 00000000000..b9dd8f8eb82 --- /dev/null +++ b/appengine/mailgun/views/error.jade @@ -0,0 +1,19 @@ +// Copyright 2015, 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. + +extends layout + +block content + h1= message + h2= error.status + pre #{error.stack} diff --git a/appengine/mailgun/views/index.jade b/appengine/mailgun/views/index.jade new file mode 100644 index 00000000000..df4e7cc2c33 --- /dev/null +++ b/appengine/mailgun/views/index.jade @@ -0,0 +1,25 @@ +// Copyright 2015, 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. + +extends layout + +block content + h1= title + p Welcome to #{title} + 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/appengine/mailgun/views/layout.jade b/appengine/mailgun/views/layout.jade new file mode 100644 index 00000000000..08c319bfe38 --- /dev/null +++ b/appengine/mailgun/views/layout.jade @@ -0,0 +1,20 @@ +// Copyright 2015, 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 + link(rel='stylesheet', href='/stylesheets/style.css') + body + block content \ No newline at end of file