Skip to content

Commit fadbcae

Browse files
committed
Merge pull request #19 from GoogleCloudPlatform/mailgun
Added mailgun example.
2 parents e941b43 + 1bda05e commit fadbcae

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

appengine/mailgun/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Mailgun on Google App Engine with Node.js
2+
3+
> [Mailgun](https://www.mailgun.com/): The Email Service For Developers
4+
5+
This sample application demonstrates how to use
6+
[Express.js](http://expressjs.com) and
7+
[node-mailgun](http://github.com/shz/node-mailgun) to send transactional email
8+
on [Google App Engine](https://cloud.google.com/appengine).
9+
10+
### Sign up for Mailgun
11+
12+
1. Sign up for a [Mailgun account](https://mailgun.com/signup).
13+
1. Add a [new domain](https://mailgun.com/app/domains).
14+
1. Find your API key in your new domain's settings.
15+
16+
### Configure
17+
18+
Add these lines to the `app.yaml` file in the root of your application:
19+
20+
```yaml
21+
env_variables:
22+
PORT: 8080
23+
MAILGUN_API_KEY: <your-mailgun-api-key>
24+
```
25+
26+
### Start the app locally
27+
28+
```
29+
$ export MAILGUN_API_KEY=<your-mailgun-api-key>
30+
$ npm start
31+
```
32+
33+
Now visit http://localhost:8080 and try sending yourself an email.
34+
35+
When the app is deployed to Google Cloud Platform the `MAILGUN_API_KEY`
36+
environment variable will be set to the value specified in `app.yaml`.
37+
38+
### Deploy
39+
40+
Ensure your gcloud sdk is setup by running:
41+
42+
```
43+
$ gcloud init
44+
```
45+
46+
For convenience, you can use an npm script to run the `gcloud` command. Add
47+
these lines to your `package.json` file:
48+
49+
```json
50+
"scripts": {
51+
"start": "node app.js",
52+
"deploy": "gcloud preview app deploy app.yaml --promote"
53+
}
54+
```
55+
56+
At the terminal you can now run the following command to deploy your
57+
application:
58+
59+
```
60+
$ npm deploy
61+
```

appengine/mailgun/app.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+

appengine/mailgun/app.yaml

+21
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+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
api_version: 1
18+
env_variables:
19+
PORT: 8080
20+
MAILGUN_API_KEY: <your-mailgun-api-key>
21+
# [END app_yaml]

appengine/mailgun/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 app.js",
12+
"deploy": "gcloud preview app deploy app.yaml --promote"
13+
},
14+
"dependencies": {
15+
"body-parser": "^1.14.1",
16+
"express": "^4.13.3",
17+
"jade": "^1.11.0",
18+
"mailgun": "^0.5.0"
19+
}
20+
}

appengine/mailgun/views/index.jade

+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+
doctype html
15+
html
16+
head
17+
title= title
18+
body
19+
h1 Hello World!
20+
p Express.js + Mailgun on Google App Engine.
21+
hr
22+
if sent
23+
p Email sent!
24+
else
25+
form(name="hello", action="/hello", method="post")
26+
input(type="email", placeholder="Enter your email to send yourself a Hello World message", name="email", style="width: 50%; margin-right: 15px;")
27+
input(type="submit", value="Send")

0 commit comments

Comments
 (0)