diff options
author | Elmer Thomas <elmer@ThinkingSerious.com> | 2015-10-13 10:27:25 -0700 |
---|---|---|
committer | Elmer Thomas <elmer@ThinkingSerious.com> | 2015-10-13 10:27:25 -0700 |
commit | a5d9f8985136d948a8c47e525192a87ed60f61bf (patch) | |
tree | 74f7d8ec5d7d9d0643574e6190b008009d847992 | |
parent | 2cb34372ef0f31352f7c90015a45e1200cb849da (diff) | |
parent | 7b0688796ed0a7e04c696f7af9695f57fdca5bad (diff) | |
download | sendgrid-nodejs-a5d9f8985136d948a8c47e525192a87ed60f61bf.zip sendgrid-nodejs-a5d9f8985136d948a8c47e525192a87ed60f61bf.tar.gz sendgrid-nodejs-a5d9f8985136d948a8c47e525192a87ed60f61bf.tar.bz2 |
Merge pull request #187 from sendgrid/tb-ccwebapiv2.0.0
Change to param to use Web API instead of X-SMTPAPI header
-rw-r--r-- | .travis.yml | 4 | ||||
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | README.md | 35 | ||||
-rw-r--r-- | lib/email.js | 12 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/lib/email.test.js | 53 | ||||
-rw-r--r-- | test/lib/sendgrid.test.js | 2 |
7 files changed, 93 insertions, 19 deletions
diff --git a/.travis.yml b/.travis.yml index 608411b..2223725 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ node_js: - "0.12" - "iojs" before_install: -- npm install -g npm@next +- npm install -g npm@2 notifications: hipchat: rooms: @@ -13,6 +13,6 @@ notifications: template: - '<a href="https://travis-ci.org/%{repository}/builds/%{build_id}">%{repository} Build %{build_number}</a> on branch <i>%{branch}</i> by %{author}: <strong>%{message}</strong> - <a href="https://github.com/sendgrid/docs/commits/%{commit}">View on GitHub</a>' + <a href="https://github.com/%{repository}/commits/%{commit}">View on GitHub</a>' format: html notify: true diff --git a/CHANGELOG.md b/CHANGELOG.md index c88d54f..57b822b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Change Log All notable changes to this project will be documented in this file. +## [2.0.0] - +## Fixed +- Add cc now uses the WebAPI instead of the SMTPApi. Read disclaimer for details + ## [1.9.1] - 2015-7-20 ### Changed - Pinned request version to be less than `2.59.0` because it broke something @@ -5,6 +5,20 @@ This nodejs module allows you to quickly and easily send emails through SendGrid [](https://travis-ci.org/sendgrid/sendgrid-nodejs) [](http://badge.fury.io/js/sendgrid) +WARNING: This module was recently upgraded from [1.9.x](https://github.com/sendgrid/sendgrid-nodejs/tree/v1.9.1) to 2.X. There were API breaking changes for various method names. See [usage](https://github.com/sendgrid/sendgrid-nodejs#usage) for up to date method names. + +## PLEASE READ THIS + +**TLDR: If you upgrade and don't change your code appropriately, things *WILL* break.** + +One of the most notable changes is how addTo() behaves. We are now using our Web API parameters instead of the X-SMTPAPI header. What this means is that if you call addTo() multiple times for an email, ONE email will be sent with each email address visible to everyone. To utilize the original behavior of having an individual personalized email sent to each recipient you must now use addSmtpapiTo(). This will break substitutions if there is more than one To address added unless you update to use addSmtpapiTo(). + +Smtpapi addressing methods cannot be mixed with non Smtpapi addressing methods. Meaning you cannot currently use Cc and Bcc with addSmtpapiTo(). + +The send() method now raises a \SendGrid\Exception by default if the response code is not 200 and returns an instance of \SendGrid\Response. + +## Sample + ```javascript var sendgrid = require('sendgrid')(sendgrid_api_key); sendgrid.send({ @@ -161,6 +175,8 @@ email.subject = "This is a subject"; You can add one or multiple TO addresses using `addTo`. +**Note**: One of the most notable changes is how `addTo()` behaves. We are now using our Web API parameters instead of the X-SMTPAPI header. What this means is that if you call `addTo()` multiple times for an email, **ONE** email will be sent with each email address visible to everyone. In oder to use the header, please call the `addSmtpapiTo()` method. + ```javascript var email = new sendgrid.Email(); email.addTo('foo@bar.com'); @@ -168,16 +184,33 @@ email.addTo('another@another.com'); sendgrid.send(email, function(err, json) { }); ``` -NOTE: This is different than setting an array on `to`. The array on `to` will show everyone the to addresses it was sent to. Using addTo will not. Usually, you'll want to use `addTo`. +#### addSmtpapiTo + + +```javascript +var email = new sendgrid.Email() +email.addSmtpapiTo('test@test.com'); +sendgrid.send(email, function(err, json) { }); +``` #### setTos +**Note**: The `setTos()` method now utilizes the Web API as opposed to using the X-SMTPAPI header. Please refer to the note posted on the top of this page. In order to use the header, you will need to use the `setSmtpapiTos()` method. + ```javascript var email = new sendgrid.Email(); email.setTos(['foo@bar.com', 'another@another.com']); sendgrid.send(email, function(err, json) { }); ``` +#### setSmtpapiTos + +```javascript +var email = new sendgrid.Email(); +email.setSmtpapiTos(["test@test.com","test2@test.com"]); +sendgrid.send(email, function(err, json) { }); +``` + #### setFrom ```javascript diff --git a/lib/email.js b/lib/email.js index 5f6be7a..292043d 100644 --- a/lib/email.js +++ b/lib/email.js @@ -59,12 +59,22 @@ Email.prototype.setHeaders = function(object_literal) { }; Email.prototype.addTo = function(to) { + this.to.push(to); + return this; +}; + +Email.prototype.setSmtpapiTos = function(tos) { + this.smtpapi.setTos(tos); + return this; +}; + +Email.prototype.addSmtpapiTo = function(to) { this.smtpapi.addTo(to); return this; }; Email.prototype.setTos = function(tos) { - this.smtpapi.setTos(tos); + this.to = tos; return this; }; diff --git a/package.json b/package.json index 9156915..3628092 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ ], "name": "sendgrid", "description": "Official SendGrid NodeJS library.", - "version": "1.9.2", + "version": "2.0.0", "homepage": "http://sendgrid.com", "repository": { "type": "git", diff --git a/test/lib/email.test.js b/test/lib/email.test.js index 120a3da..60f29f3 100644 --- a/test/lib/email.test.js +++ b/test/lib/email.test.js @@ -45,18 +45,6 @@ describe('Email', function () { expect(format.to).to.equal(payload.to); }); - it('should not have multiple TOs if as an array but also set on smtpapi via addTo', function() { - var payload = Object.create(default_payload); - payload.to = ['david.tomberlin@sendgrid.com', 'otherguy@sendgrid.com']; - var email = new Email(payload); - email.addTo(payload.to[0]); - email.addTo(payload.to[1]); - - var format = email.toWebFormat(); - - expect(format.to).to.equal(payload.from); - }); - it('should have multiple BCCs if as an array', function() { var payload = Object.create(default_payload); payload.bcc = ['david.tomberlin@sendgrid.com', 'otherguy@sendgrid.com']; @@ -119,11 +107,50 @@ describe('Email', function () { var payload = Object.create(default_payload); payload.to = ""; var email = new Email(payload); - email.addTo("test@test.com"); + email.addSmtpapiTo("test@test.com"); var format = email.toWebFormat(); + expect(format.to).to.not.be.empty; expect(JSON.parse(format['x-smtpapi']).to).to.not.be.empty; + }); + + it('should have to addresses if there is no tos set but there are smtpapi tos set', function() { + var payload = Object.create(default_payload); + payload.to = ""; + var email = new Email(payload); + email.setSmtpapiTos(["test@test.com", "test2@test.com"]); + var format = email.toWebFormat(); + + expect(format.to).to.not.be.empty; + expect(JSON.parse(format['x-smtpapi']).to).to.not.be.empty; + expect(JSON.parse(format['x-smtpapi']).to).to.be.an.array; + }); + + it('should have a to address using addTo if there is no smtpapi to', function(){ + var payload = Object.create(default_payload); + payload.to = ""; + var email = new Email(payload); + email.addTo('test@test.com'); + email.addTo('test1@test.com'); + var format = email.toWebFormat(); + + expect(format.to).to.not.be.empty; + expect(format.to[0]).to.equal('test@test.com'); + expect(format.to[1]).to.equal('test1@test.com'); + }); + + it('should have a to addresses using setTos if there is no smtpapi to', function(){ + var payload = Object.create(default_payload); + payload.to = ""; + var email = new Email(payload); + email.setTos(['test@test.com', 'test1@test.com']); + + var format = email.toWebFormat(); + expect(format.to).to.not.be.empty; + expect(format.to).to.be.an.array; + expect(format.to[0]).to.equal('test@test.com'); + expect(format.to[1]).to.equal('test1@test.com'); }); it("should set a fromname if one is provided", function() { diff --git a/test/lib/sendgrid.test.js b/test/lib/sendgrid.test.js index 9b5654a..ad293e0 100644 --- a/test/lib/sendgrid.test.js +++ b/test/lib/sendgrid.test.js @@ -12,7 +12,7 @@ var nock = require('nock'); describe('SendGrid', function () { it('version should be set', function() { var sendgrid = SendGrid(API_USER, API_KEY); - expect(sendgrid.version).to.equal("1.9.2"); + expect(sendgrid.version).to.equal("2.0.0"); }); it('should be an instance of SendGrid', function() { |