Skip to content

Commit

Permalink
Upgrade Sendgrid version in Compute sample (#211)
Browse files Browse the repository at this point in the history
* Update Sendgrid library to latest version
  • Loading branch information
Ace Nassri authored and jmdobry committed Sep 13, 2016
1 parent aeae10f commit 8ebb25b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"googleapis": "^12.2.0",
"nodemailer": "^2.4.1",
"nodemailer-smtp-transport": "^2.5.0",
"sendgrid": "^2.0.0"
"sendgrid": "^4.0.1"
},
"devDependencies": {
"mocha": "^3.0.2"
Expand Down
32 changes: 23 additions & 9 deletions compute/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,33 @@
'use strict';

// [START send]
// This sample is based off of https://www.npmjs.com/package/sendgrid#without-mail-helper-class
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@EXAMPLE.COM', // 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) {
return console.log(err);
var request = Sendgrid.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [{
to: [{ email: 'to_email@example.com' }],
subject: 'Sendgrid test email from Node.js on Google Cloud Platform'
}],
from: { email: 'from_email@example.com' },
content: [{
type: 'text/plain',
value: 'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.'
}]
}
console.log(json);
});

Sendgrid.API(request, function (error, response) {
if (error) {
console.log('Mail not sent; see error message below.');
} else {
console.log('Mail sent successfully!');
}
console.log(response);
});
// [END send]
25 changes: 18 additions & 7 deletions compute/test/sendgrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ describe('computeengine:sendgrid', function () {
sendgrid: function (key) {
assert.equal(key, 'foo');
return {
send: function (payload, cb) {
assert.deepEqual(payload, {
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM',
to: 'EMAIL@EXAMPLE.COM',
subject: 'test email from Node.js on Google Cloud Platform',
text: 'Hello!\n\nThis a test email from Node.js.'
emptyRequest: function (x) {
return x;
},
API: function (request, cb) {
assert.deepEqual(request, {
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [{
to: [{ email: 'to_email@example.com' }],
subject: 'Sendgrid test email from Node.js on Google Cloud Platform'
}],
from: { email: 'from_email@example.com' },
content: [{
type: 'text/plain',
value: 'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.'
}]
}
});
cb('done');
done();
}
};
Expand Down

0 comments on commit 8ebb25b

Please sign in to comment.