Skip to content

Commit ed1e39d

Browse files
committed
Added mailgun example.
1 parent e941b43 commit ed1e39d

File tree

13 files changed

+434
-0
lines changed

13 files changed

+434
-0
lines changed

appengine/mailgun/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./config.js

appengine/mailgun/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## Mailgun on Google App Engine
2+
3+
> [Mailgun](https://www.mailgun.com/): The Email Service For Developers
4+
5+
### Sign up for Mailgun
6+
7+
1. Sign up for a [Mailgun account](https://mailgun.com/signup).
8+
1. Add a [new domain](https://mailgun.com/app/domains).
9+
1. Find your API key in your new domain's settings.
10+
11+
### Create a new Node.js app
12+
13+
This example is going to use [Express.js](http://expressjs.com).
14+
15+
### Configure
16+
17+
Create an `app.yaml` in the root of your application with the following
18+
contents:
19+
20+
```yaml
21+
runtime: nodejs
22+
vm: true
23+
api_version: 1
24+
env_variables:
25+
PORT: 8080
26+
```
27+
28+
Create a `config.js` in the root of your application with the following
29+
contents:
30+
31+
```yaml
32+
module.exports = {
33+
mailgunApiKey: 'your-mailgun-api-key'
34+
};
35+
```
36+
37+
### Start the app locally
38+
39+
```
40+
$ npm start
41+
```
42+
43+
Now visit http://localhost:8080 and try sending yourself an email.
44+
45+
### Deploy
46+
47+
For convenience, you can use an npm script to run the `gcloud` command. Add
48+
these lines to your `package.json` file:
49+
50+
```json
51+
"scripts": {
52+
"start": "node ./bin/www",
53+
"deploy": "gcloud preview app deploy app.yaml --promote --project <your-project-id>"
54+
}
55+
```
56+
57+
At the terminal you can now run the following command to deploy your
58+
application:
59+
60+
```
61+
$ npm deploy
62+
```

appengine/mailgun/app.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 logger = require('morgan');
19+
var cookieParser = require('cookie-parser');
20+
var bodyParser = require('body-parser');
21+
22+
var routes = require('./routes/index');
23+
var hello = require('./routes/hello');
24+
25+
var app = express();
26+
27+
// view engine setup
28+
app.set('views', path.join(__dirname, 'views'));
29+
app.set('view engine', 'jade');
30+
31+
app.use(logger('dev'));
32+
app.use(bodyParser.json());
33+
app.use(bodyParser.urlencoded({ extended: false }));
34+
app.use(cookieParser());
35+
app.use(express.static(path.join(__dirname, 'public')));
36+
37+
app.use('/', routes);
38+
app.use('/hello', hello);
39+
40+
// catch 404 and forward to error handler
41+
app.use(function(req, res, next) {
42+
var err = new Error('Not Found');
43+
err.status = 404;
44+
next(err);
45+
});
46+
47+
// error handlers
48+
49+
// development error handler
50+
// will print stacktrace
51+
if (app.get('env') === 'development') {
52+
app.use(function(err, req, res) {
53+
res.status(err.status || 500);
54+
res.render('error', {
55+
message: err.message,
56+
error: err
57+
});
58+
});
59+
}
60+
61+
// production error handler
62+
// no stacktraces leaked to user
63+
app.use(function(err, req, res) {
64+
res.status(err.status || 500);
65+
res.render('error', {
66+
message: err.message,
67+
error: {}
68+
});
69+
});
70+
71+
72+
module.exports = app;

appengine/mailgun/app.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
api_version: 1
18+
env_variables:
19+
PORT: 8080
20+
# [END app_yaml]

appengine/mailgun/bin/www

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
// [START server]
6+
7+
/**
8+
* Module dependencies.
9+
*/
10+
var app = require('../app');
11+
var debug = require('debug')('express:server');
12+
var http = require('http');
13+
14+
/**
15+
* Get port from environment and store in Express.
16+
*/
17+
var port = normalizePort(process.env.PORT || 8080);
18+
app.set('port', port);
19+
20+
/**
21+
* Create HTTP server.
22+
*/
23+
24+
var server = http.createServer(app);
25+
26+
/**
27+
* Listen on provided port, on all network interfaces.
28+
*/
29+
server.listen(port);
30+
server.on('error', onError);
31+
server.on('listening', onListening);
32+
33+
// [END server]
34+
35+
/**
36+
* Normalize a port into a number, string, or false.
37+
*/
38+
function normalizePort(val) {
39+
var port = parseInt(val, 10);
40+
41+
if (isNaN(port)) {
42+
// named pipe
43+
return val;
44+
}
45+
46+
if (port >= 0) {
47+
// port number
48+
return port;
49+
}
50+
51+
return false;
52+
}
53+
54+
/**
55+
* Event listener for HTTP server "error" event.
56+
*/
57+
function onError(error) {
58+
if (error.syscall !== 'listen') {
59+
throw error;
60+
}
61+
62+
var bind = typeof port === 'string'
63+
? 'Pipe ' + port
64+
: 'Port ' + port;
65+
66+
// handle specific listen errors with friendly messages
67+
switch (error.code) {
68+
case 'EACCES':
69+
console.error(bind + ' requires elevated privileges');
70+
process.exit(1);
71+
break;
72+
case 'EADDRINUSE':
73+
console.error(bind + ' is already in use');
74+
process.exit(1);
75+
break;
76+
default:
77+
throw error;
78+
}
79+
}
80+
81+
/**
82+
* Event listener for HTTP server "listening" event.
83+
*/
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

appengine/mailgun/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
mailgunApiKey: 'key-4z6h5ysqalzctf97y-17dqzbnlpxz8b8'
3+
};

appengine/mailgun/package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "appengine-mailgun",
3+
"description": "An example of using Mailgun in Node.js on Google App Engine.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"engines": {
8+
"node": "~0.12.7"
9+
},
10+
"scripts": {
11+
"start": "node ./bin/www",
12+
"deploy": "gcloud preview app deploy app.yaml --promote --project mailgun-demo"
13+
},
14+
"dependencies": {
15+
"body-parser": "^1.14.1",
16+
"cookie-parser": "^1.4.0",
17+
"debug": "^2.2.0",
18+
"express": "^4.13.3",
19+
"jade": "^1.11.0",
20+
"mailgun": "^0.5.0",
21+
"morgan": "^1.6.1",
22+
"serve-favicon": "^2.3.0"
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
body {
15+
padding: 50px;
16+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
17+
}
18+
19+
a {
20+
color: #00B7FF;
21+
}

appengine/mailgun/routes/hello.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 router = express.Router();
18+
var config = require('../config');
19+
var Mailgun = require('mailgun').Mailgun;
20+
var mg = new Mailgun(config.mailgunApiKey);
21+
22+
var link = 'https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/mailgun'; // jshint ignore:line
23+
24+
// [START send]
25+
router.post('/', function(req, res, next) {
26+
var servername = '';
27+
var options = {};
28+
29+
mg.sendText(
30+
31+
req.body.email,
32+
'Hello World!',
33+
'Go to ' + link + ' to read about using Mailgun on Google App Engine.',
34+
servername,
35+
options,
36+
function (err) {
37+
if (err) {
38+
return next(new Error('Failed to send email!'));
39+
} else {
40+
return res.render('index', {
41+
title: 'Hello World! Express.js on Google App Engine.',
42+
sent: true
43+
});
44+
}
45+
}
46+
);
47+
});
48+
// [END send]
49+
50+
module.exports = router;

appengine/mailgun/routes/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 router = express.Router();
18+
19+
// [START hello_world]
20+
router.get('/', function(req, res) {
21+
res.render('index', {
22+
title: 'Hello World! Express.js on Google App Engine.'
23+
});
24+
});
25+
// [END hello_world]
26+
27+
module.exports = router;

0 commit comments

Comments
 (0)