From b951a6f1f48a62af2bc4549e1162f478ab4600ab Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 13 Sep 2016 11:50:40 -0700 Subject: [PATCH 1/2] Update Sendgrid library to latest version --- computeengine/package.json | 2 +- computeengine/sendgrid.js | 34 +++++++++++++++++++++-------- computeengine/test/sendgrid.test.js | 25 +++++++++++++++------ 3 files changed, 44 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..ffcf075f43 100644 --- a/computeengine/sendgrid.js +++ b/computeengine/sendgrid.js @@ -14,19 +14,35 @@ '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.statusCode); + console.log(response.body); + console.log(response.headers); }); // [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(); } }; From f15aaa5145cdee149d8dd72c747f365b394ca5b3 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 13 Sep 2016 11:53:58 -0700 Subject: [PATCH 2/2] Log entire response --- computeengine/sendgrid.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/computeengine/sendgrid.js b/computeengine/sendgrid.js index ffcf075f43..c2202e9ea2 100644 --- a/computeengine/sendgrid.js +++ b/computeengine/sendgrid.js @@ -41,8 +41,6 @@ Sendgrid.API(request, function (error, response) { } else { console.log('Mail sent successfully!'); } - console.log(response.statusCode); - console.log(response.body); - console.log(response.headers); + console.log(response); }); // [END send]