Skip to content

Commit 31e0341

Browse files
Ace Nassrijmdobry
authored andcommitted
Upgrade Sendgrid version in Compute sample (GoogleCloudPlatform#211)
* Update Sendgrid library to latest version
1 parent 97aa4fa commit 31e0341

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

computeengine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"googleapis": "^12.2.0",
1414
"nodemailer": "^2.4.1",
1515
"nodemailer-smtp-transport": "^2.5.0",
16-
"sendgrid": "^2.0.0"
16+
"sendgrid": "^4.0.1"
1717
},
1818
"devDependencies": {
1919
"mocha": "^3.0.2"

computeengine/sendgrid.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,33 @@
1414
'use strict';
1515

1616
// [START send]
17+
// This sample is based off of https://www.npmjs.com/package/sendgrid#without-mail-helper-class
1718
var Sendgrid = require('sendgrid')(
1819
process.env.SENDGRID_API_KEY || '<your-sendgrid-api-key>'
1920
);
2021

21-
Sendgrid.send({
22-
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', // From address
23-
to: '[email protected]', // To address
24-
subject: 'test email from Node.js on Google Cloud Platform', // Subject
25-
text: 'Hello!\n\nThis a test email from Node.js.' // Content
26-
}, function (err, json) {
27-
if (err) {
28-
return console.log(err);
22+
var request = Sendgrid.emptyRequest({
23+
method: 'POST',
24+
path: '/v3/mail/send',
25+
body: {
26+
personalizations: [{
27+
to: [{ email: '[email protected]' }],
28+
subject: 'Sendgrid test email from Node.js on Google Cloud Platform'
29+
}],
30+
from: { email: '[email protected]' },
31+
content: [{
32+
type: 'text/plain',
33+
value: 'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.'
34+
}]
2935
}
30-
console.log(json);
36+
});
37+
38+
Sendgrid.API(request, function (error, response) {
39+
if (error) {
40+
console.log('Mail not sent; see error message below.');
41+
} else {
42+
console.log('Mail sent successfully!');
43+
}
44+
console.log(response);
3145
});
3246
// [END send]

computeengine/test/sendgrid.test.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ describe('computeengine:sendgrid', function () {
2222
sendgrid: function (key) {
2323
assert.equal(key, 'foo');
2424
return {
25-
send: function (payload, cb) {
26-
assert.deepEqual(payload, {
27-
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM',
28-
29-
subject: 'test email from Node.js on Google Cloud Platform',
30-
text: 'Hello!\n\nThis a test email from Node.js.'
25+
emptyRequest: function (x) {
26+
return x;
27+
},
28+
API: function (request, cb) {
29+
assert.deepEqual(request, {
30+
method: 'POST',
31+
path: '/v3/mail/send',
32+
body: {
33+
personalizations: [{
34+
to: [{ email: '[email protected]' }],
35+
subject: 'Sendgrid test email from Node.js on Google Cloud Platform'
36+
}],
37+
from: { email: '[email protected]' },
38+
content: [{
39+
type: 'text/plain',
40+
value: 'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.'
41+
}]
42+
}
3143
});
32-
cb('done');
3344
done();
3445
}
3546
};

0 commit comments

Comments
 (0)