-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from GoogleCloudPlatform/mailgun
Added mailgun example.
- Loading branch information
Showing
5 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
## Mailgun on Google App Engine with Node.js | ||
|
||
> [Mailgun](https://www.mailgun.com/): The Email Service For Developers | ||
This sample application demonstrates how to use | ||
[Express.js](http://expressjs.com) and | ||
[node-mailgun](http://github.com/shz/node-mailgun) to send transactional email | ||
on [Google App Engine](https://cloud.google.com/appengine). | ||
|
||
### 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. | ||
|
||
### Configure | ||
|
||
Add these lines to the `app.yaml` file in the root of your application: | ||
|
||
```yaml | ||
env_variables: | ||
PORT: 8080 | ||
MAILGUN_API_KEY: <your-mailgun-api-key> | ||
``` | ||
### Start the app locally | ||
``` | ||
$ export MAILGUN_API_KEY=<your-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 be set to the value specified in `app.yaml`. | ||
|
||
### 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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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 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); | ||
// [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 servername = ''; | ||
var options = {}; | ||
|
||
mg.sendText( | ||
// From | ||
'no-reply@appengine-mailgun-demo.com', | ||
// To | ||
req.body.email, | ||
// Subject | ||
'Hello World!', | ||
// Body | ||
'Mailgun on Google App Engine with Node.js', | ||
servername, | ||
options, | ||
function (err) { | ||
if (err) { | ||
return next(err); | ||
} else { | ||
// Render the index route on success | ||
return res.render('index', { | ||
sent: true | ||
}); | ||
} | ||
} | ||
); | ||
}); | ||
// [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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: <your-mailgun-api-key> | ||
# [END app_yaml] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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 | ||
body | ||
h1 Hello World! | ||
p Express.js + Mailgun 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") |