-
Notifications
You must be signed in to change notification settings - Fork 2k
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 #45 from GoogleCloudPlatform/sendgrid
Added sendgrid example app. Also used for Compute Engine snippet.
- Loading branch information
Showing
13 changed files
with
300 additions
and
2 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 |
---|---|---|
|
@@ -3,4 +3,7 @@ npm-debug.log | |
coverage/ | ||
|
||
test/encrypted/nodejs-docs-samples.json | ||
dump.rdb | ||
dump.rdb | ||
logs/ | ||
*.iml | ||
.idea/ |
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
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 @@ | ||
## Express.js + Sendgrid on Google App Engine | ||
|
||
> [Sendgrid][1]: Delivering your transactional and marketing email through one reliable platform. | ||
> | ||
> – sendgrid.com | ||
This sample application demonstrates how to use [Express.js][2] and | ||
[sendgrid-nodejs][3] to send transactional email on [Google App Engine][4]. | ||
|
||
Read the [Sendgrid on App Engine Tutorial][5] for how to run and deploy this | ||
sample app. | ||
|
||
You can also read the [Sendgrid documentation][6]. | ||
|
||
[1]: https://sendgrid.com/ | ||
[2]: http://expressjs.com | ||
[3]: https://github.com/sendgrid/sendgrid-nodejs | ||
[4]: https://cloud.google.com/appengine | ||
[5]: https://cloud.google.com/nodejs/resources/tools/sendgrid | ||
[6]: https://sendgrid.com/docs |
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,73 @@ | ||
// 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 Sendgrid = require('sendgrid')(process.env.SENDGRID_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.urlencoded({ extended: false })); | ||
|
||
// [START index] | ||
app.get('/', function(req, res) { | ||
res.render('index'); | ||
}); | ||
// [END index] | ||
|
||
// [START hello] | ||
app.post('/hello', function(req, res, next) { | ||
Sendgrid.send({ | ||
from: '[email protected]', // From address | ||
to: req.body.email, // To address | ||
subject: 'Hello World!', // Subject | ||
text: 'Sendgrid on Google App Engine with Node.js', // Content | ||
}, function (err) { | ||
if (err) { | ||
return next(err); | ||
} | ||
// Render the index route on success | ||
return res.render('index', { | ||
sent: true | ||
}); | ||
}); | ||
}); | ||
// [END hello] | ||
|
||
if (module === require.main) { | ||
// [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] | ||
} | ||
|
||
module.exports = app; |
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 | ||
env_variables: | ||
SENDGRID_API_KEY: <your-sendgrid-api-key> | ||
# [END app_yaml] | ||
skip_files: | ||
- ^(.*/)?.*/node_modules/.*$ |
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-sendgrid", | ||
"description": "An example of using Sendgrid in Node.js on Google App Engine.", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"engines": { | ||
"node": "~4.2" | ||
}, | ||
"scripts": { | ||
"start": "node app.js", | ||
"deploy": "gcloud preview app deploy app.yaml" | ||
}, | ||
"dependencies": { | ||
"body-parser": "^1.14.1", | ||
"express": "^4.13.3", | ||
"jade": "^1.11.0", | ||
"sendgrid": "^2.0.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 + Sendgrid 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") |
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,13 @@ | ||
{ | ||
"name": "computeengine-sendgrid", | ||
"description": "An example of sending an email with Sendgrid in Node.js on Google Compute Engine.", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"engines": { | ||
"node": "~4.2" | ||
}, | ||
"dependencies": { | ||
"sendgrid": "^2.0.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,33 @@ | ||
// 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'; | ||
|
||
// [START send] | ||
var Sendgrid = require('sendgrid')( | ||
process.env.SENDGRID_API_KEY || '<your-sendgrid-api-key>' | ||
); | ||
|
||
Sendgrid.send({ | ||
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', // From address | ||
to: '[email protected]', // 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] |
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
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
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,36 @@ | ||
// 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 assert = require('assert'); | ||
var request = require('supertest'); | ||
var app = require('../../appengine/sendgrid/app.js'); | ||
|
||
describe('sendgrid', function () { | ||
it('should return 200 and a "Hello World" message', function (done) { | ||
var message = 'Express.js + Sendgrid on Google App Engine'; | ||
|
||
request(app) | ||
.get('/') | ||
.expect(200) | ||
.expect(function (res) { | ||
assert( | ||
res.text.indexOf(message) !== -1, | ||
'Response should contain a "Hello World" message.\n' + | ||
'Found: ' + res.text | ||
); | ||
}) | ||
.end(done); | ||
}); | ||
}); |
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,39 @@ | ||
// 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 assert = require('assert'); | ||
var proxyquire = require('proxyquire').noPreserveCache(); | ||
process.env.SENDGRID_API_KEY = 'foo'; | ||
|
||
describe('sendgrid', function () { | ||
it('should send an email', function (done) { | ||
proxyquire('../../computeengine/sendgrid/sendmail.js', { | ||
sendgrid: function (key) { | ||
assert.equal(key, 'foo'); | ||
return { | ||
send: function (payload) { | ||
assert.deepEqual(payload, { | ||
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', | ||
to: '[email protected]', | ||
subject: 'test email from Node.js on Google Cloud Platform', | ||
text: 'Hello!\n\nThis a test email from Node.js.' | ||
}); | ||
done(); | ||
} | ||
}; | ||
} | ||
}); | ||
}); | ||
}); |