From 31e03412b9210b4e313a138d13a761d3b49c3077 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 13 Sep 2016 15:15:18 -0500 Subject: [PATCH] Upgrade Sendgrid version in Compute sample (#211) * Update Sendgrid library to latest version --- computeengine/package.json | 2 +- computeengine/sendgrid.js | 32 +++++++++++++++++++++-------- computeengine/test/sendgrid.test.js | 25 +++++++++++++++------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/computeengine/package.json b/computeengine/package.json index aad2dda066..1f86f337c5 100644 --- a/computeengine/package.json +++ b/computeengine/package.json @@ -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" diff --git a/computeengine/sendgrid.js b/computeengine/sendgrid.js index 9f4c07812d..c2202e9ea2 100644 --- a/computeengine/sendgrid.js +++ b/computeengine/sendgrid.js @@ -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 || '' ); -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] diff --git a/computeengine/test/sendgrid.test.js b/computeengine/test/sendgrid.test.js index 92a5427596..1cf8d676af 100644 --- a/computeengine/test/sendgrid.test.js +++ b/computeengine/test/sendgrid.test.js @@ -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(); } };