diff options
author | scottmotte <scott@scottmotte.com> | 2013-12-30 16:15:49 -0800 |
---|---|---|
committer | scottmotte <scott@scottmotte.com> | 2013-12-30 16:15:49 -0800 |
commit | e5ca93d5f49111a150573ff59a9ea8ac6cf34fff (patch) | |
tree | e66aa7a880a73006c5146baaa941426ae595c746 | |
parent | f51edfaf6f53ebf5715702a015328c59b116606c (diff) | |
download | sendgrid-nodejs-e5ca93d5f49111a150573ff59a9ea8ac6cf34fff.zip sendgrid-nodejs-e5ca93d5f49111a150573ff59a9ea8ac6cf34fff.tar.gz sendgrid-nodejs-e5ca93d5f49111a150573ff59a9ea8ac6cf34fff.tar.bz2 |
Get with the latest and greatest smtpapi-nodejs lib
-rw-r--r-- | README.md | 135 | ||||
-rw-r--r-- | lib/email.js | 51 | ||||
-rw-r--r-- | test/integration/sendgrid.test.js | 18 |
3 files changed, 105 insertions, 99 deletions
@@ -86,13 +86,6 @@ sendgrid.send(payload, function(err, json) { }); ``` -## Advanced Usage - -There are two additional objects built into this library that will help you use this library as a power user. - -+ Email -+ SmtpapiHeaders - ### Email Email helps you more powerfully prepare your message to be sent. @@ -102,8 +95,7 @@ To get started create an Email object where `params` is a javascript object. You ```javascript var sendgrid = require('sendgrid')(api_user, api_key); -var Email = sendgrid.Email; -var email = new Email(params); +var email = new sendgrid.Email(params); ``` #### Sample @@ -112,8 +104,7 @@ Here is a sample for using it. ```javascript var sendgrid = require('sendgrid')(api_user, api_key); -var Email = sendgrid.Email; -var email = new Email({ +var email = new sendgrid.Email({ to: 'person@somewhere.com', from: 'you@yourself', subject: 'What was Wenger thinking sending Walcott on that early?', @@ -163,8 +154,7 @@ You can set params like you would for any standard JavaScript object. ```javascript var sendgrid = require('sendgrid')(api_user, api_key); -var Email = sendgrid.Email; -var email = new Email({to: 'person@email.com'}); +var email = new sendgrid.Email({to: 'person@email.com'}); email.to = "different@email.com"; email.replyto = "reply-here@email.com"; email.subject = "This is a subject"; @@ -175,7 +165,7 @@ email.subject = "This is a subject"; You can add one or multiple TO addresses using `addTo`. ```javascript -var email = new Email(); +var email = new sendgrid.Email(); email.addTo('foo@bar.com'); email.addTo('another@another.com'); sendgrid.send(email, function(err, json) { }); @@ -186,7 +176,7 @@ NOTE: This is different than setting an array on `to`. The array on `to` will sh #### setFrom ```javascript -var email = new Email(); +var email = new sendgrid.Email(); email.setFrom('foo@bar.com'); sendgrid.send(email, function(err, json) { }); ``` @@ -194,7 +184,7 @@ sendgrid.send(email, function(err, json) { }); #### setSubject ```javascript -var email = new Email(); +var email = new sendgrid.Email(); email.setSubject('Some subject'); sendgrid.send(email, function(err, json) { }); ``` @@ -202,7 +192,7 @@ sendgrid.send(email, function(err, json) { }); #### setText ```javascript -var email = new Email(); +var email = new sendgrid.Email(); email.setText('Some text'); sendgrid.send(email, function(err, json) { }); ``` @@ -210,113 +200,120 @@ sendgrid.send(email, function(err, json) { }); #### setHtml ```javascript -var email = new Email(); +var email = new sendgrid.Email(); email.setHtml('<h1>Some html</h1>'); sendgrid.send(email, function(err, json) { }); ``` -#### setHeaders +#### addHeaders -You can set custom headers. +You can add custom headers. This will ADD rather than SET headers. ```javascript -var email = new Email(); +var email = new sendgrid.Email(); email.setHeaders({full: 'hearts'}); // headers = {full: 'hearts'} -email.setHeaders({mask: 'salesman'}); // headers = {mask: 'salesman'} +email.addHeaders({spin: 'attack'}); // headers = {full: 'hearts', spin: 'attack'} +email.addHeaders({mask: 'salesman'}); // headers = {full: 'hearts', spin: 'attack', mask: 'salesman'} sendgrid.send(email, function(err, json) { }); ``` -#### addHeaders +#### setHeaders -You can add custom headers. This will ADD rather than SET headers. +You can set custom headers. ```javascript -var email = new Email(); +var email = new sendgrid.Email(); email.setHeaders({full: 'hearts'}); // headers = {full: 'hearts'} -email.addHeaders({spin: 'attack'}); // headers = {full: 'hearts', spin: 'attack'} -email.addHeaders({mask: 'salesman'}); // headers = {full: 'hearts', spin: 'attack', mask: 'salesman'} +email.setHeaders({mask: 'salesman'}); // headers = {mask: 'salesman'} sendgrid.send(email, function(err, json) { }); ``` -#### addSubVal +#### addSubstitution ```javascript -var email = new Email(); -email.addSubVal('keep', 'secret'); // sub = {keep: ['secret']} -email.addSubVal('other', ['one', 'two']); // sub = {keep: ['secret'], other: ['one', 'two']} +var email = new sendgrid.Email(); +email.addSubstitution('keep', 'secret'); // sub = {keep: ['secret']} +email.addSubstitution('other', ['one', 'two']); // sub = {keep: ['secret'], other: ['one', 'two']} ``` -#### setSection +#### setSubstitutions ```javascript -var email = new Email(); -email.setSection({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'} +var email = new sendgrid.Email(); +email.setSubstitutions('keep', 'secret'); // sub = {keep: ['secret']} +email.setSubstitutions('other', ['one', 'two']); // sub = {keep: ['secret'], other: ['one', 'two']} ``` #### addSection ```javascript -var email = new Email(); -email.setSection({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'} -email.addSection({'-bomber-': 'Only for sad vikings.'}); // section = {'-charge-': 'This ship is useless.', +var email = new sendgrid.Email(); +email.addSection({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'} ``` -#### setUniqueArgs +#### setSections ```javascript -var email = new Email(); -email.setUniqueArgs({cow: 'chicken'}); // unique_args = {cow: 'chicken'} -email.setUniqueArgs({dad: 'proud'}); // unique_args = {dad: 'proud'} +var email = new sendgrid.Email(); +email.setSections({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'} ``` -#### addUniqueArgs +#### addUniqueArg ```javascript -var email = new Email(); -email.setUniqueArgs({cow: 'chicken'}); // unique_args = {cow: 'chicken'} -email.addUniqueArgs({cat: 'dog'}); // unique_args = {cow: 'chicken', cat: 'dog'} +var email = new sendgrid.Email(); +email.setUniqueArg({cow: 'chicken'}); // unique_args = {cow: 'chicken'} +email.addUniqueArg({cat: 'dog'}); // unique_args = {cow: 'chicken', cat: 'dog'} ``` -#### setFilterSetting - -You can set a filter using an object literal. +#### setUniqueArgs ```javascript -var email = new Email(); -email.setFilterSetting({ - 'footer': { - 'setting': { - 'enable': 1, - 'text/plain': 'You can haz footers!' - } - } -}); +var email = new sendgrid.Email(); +email.setUniqueArgs({cow: 'chicken'}); // unique_args = {cow: 'chicken'} +email.setUniqueArgs({dad: 'proud'}); // unique_args = {dad: 'proud'} ``` -#### setCategory +#### addCategory ```javascript -var email = new Email(); -email.setCategory('tactics'); // category = ['tactics'] -email.setCategory('snowball-fight'); // category = ['snowball-fight'] +var email = new sendgrid.Email(); +email.addCategory('tactics'); // category = ['tactics'] +email.addCategory('advanced'); // category = ['tactics', 'advanced'] ``` -#### addCategory +#### setCategories ```javascript -var email = new Email(); -email.setCategory('tactics'); // category = ['tactics'] -email.addCategory('advanced'); // category = ['tactics', 'advanced'] +var email = new sendgrid.Email(); +email.setCategories(['tactics']); // category = ['tactics'] +email.setCategories(['snowball-fight']); // category = ['snowball-fight'] ``` -#### addFilterSetting +#### addFilter Alternatively, you can add filter settings one at a time. ```javascript -var email = new Email(); -email.addFilterSetting('footer', 'enable', 1); -email.addFilterSetting('footer', 'text/html', '<strong>boo</strong>'); +var email = new sendgrid.Email(); +email.addFilter('footer', 'enable', 1); +email.addFilter('footer', 'text/html', '<strong>boo</strong>'); +``` + +#### setFilters + +You can set a filter using an object literal. + +```javascript +var email = new sendgrid.Email(); +email.setFilters({ + 'footer': { + 'setting': { + 'enable': 1, + 'text/plain': 'You can haz footers!' + } + } +}); ``` #### addFile diff --git a/lib/email.js b/lib/email.js index 45c30fc..f0b2a00 100644 --- a/lib/email.js +++ b/lib/email.js @@ -52,15 +52,15 @@ function Email(params) { } } -Email.prototype.setHeaders = function(val) { +Email.prototype.addHeaders = function(val) { if (_.isObject(val)) { - this.headers = val; + _.extend(this.headers, val); } }; -Email.prototype.addHeaders = function(val) { +Email.prototype.setHeaders = function(val) { if (_.isObject(val)) { - _.extend(this.headers, val); + this.headers = val; } }; @@ -68,6 +68,10 @@ Email.prototype.addTo = function(to) { this.smtpapi.addTo(to); }; +Email.prototype.setTos = function(tos) { + this.smtpapi.setTos(tos); +}; + Email.prototype.setFrom = function(from) { this.from = from; }; @@ -84,40 +88,45 @@ Email.prototype.setHtml = function(html) { this.html = html; }; -Email.prototype.addSubVal = function(key, val) { - this.smtpapi.addSubVal(key, val); +Email.prototype.addSubstitution = function(key, val) { + this.smtpapi.addSubstitution(key, val); }; -Email.prototype.setUniqueArgs = function(val) { - this.smtpapi.setUniqueArgs(val); +Email.prototype.setSubstitutions = function(substitutions) { + this.smtpapi.setSubstitutions(substitutions); }; -Email.prototype.addUniqueArgs = function (val) { - this.smtpapi.addUniqueArgs(val); +Email.prototype.addUniqueArg = function (object_literal) { + this.smtpapi.addUniqueArg(object_literal); }; -Email.prototype.setCategory = function(val) { - this.smtpapi.setCategory(val); +Email.prototype.setUniqueArgs = function(object_literal) { + this.smtpapi.setUniqueArgs(object_literal); }; Email.prototype.addCategory = function(val) { this.smtpapi.addCategory(val); }; -Email.prototype.setSection = function(val) { - this.smtpapi.setSection(val); +Email.prototype.setCategories = function(val) { + this.smtpapi.setCategories(val); }; -Email.prototype.addSection = function(val) { - this.smtpapi.addSection(val); +Email.prototype.addSection = function(object_literal) { + this.smtpapi.addSection(object_literal); }; -Email.prototype.addFilterSetting = function(filter, setting, val) { - this.smtpapi.addFilterSetting(filter, setting, val); +Email.prototype.setSections = function(object_literal) { + this.smtpapi.setSections(object_literal); +}; + + +Email.prototype.addFilter = function(filter, setting, val) { + this.smtpapi.addFilter(filter, setting, val); }; -Email.prototype.setFilterSetting = function(filters) { - this.smtpapi.setFilterSetting(filters); +Email.prototype.setFilters = function(filters) { + this.smtpapi.setFilters(filters); }; Email.prototype.addFile = function(file_object) { @@ -128,7 +137,7 @@ Email.prototype.toWebFormat = function() { var web = { to : this.to, from : this.from, - 'x-smtpapi' : this.smtpapi.toJsonString(), + 'x-smtpapi' : this.smtpapi.jsonString(), subject : this.subject, text : this.text, html : this.html, diff --git a/test/integration/sendgrid.test.js b/test/integration/sendgrid.test.js index 4f11eb1..f2f8d43 100644 --- a/test/integration/sendgrid.test.js +++ b/test/integration/sendgrid.test.js @@ -2,8 +2,8 @@ process.env.NODE_ENV = 'test'; var dotenv = require('dotenv')(); dotenv.load(); -var API_USER = process.env.API_USER || 'some_sendgrid_username'; -var API_KEY = process.env.API_KEY || 'some_sendgrid_password'; +var API_USER = process.env.SENDGRID_USERNAME || 'some_sendgrid_username'; +var API_KEY = process.env.SENDGRID_PASSWORD || 'some_sendgrid_password'; var default_payload = { to : process.env.TO || "hello@example.com", from : process.env.FROM || "swift@sendgrid.com", @@ -345,8 +345,8 @@ describe('SendGrid #skip', function () { payload.subject += "handles filters"; var email = new Email(payload); - email.addFilterSetting('footer', 'enable', 1); - email.addFilterSetting('footer', 'text/plain', 'This is mah footer!'); + email.addFilter('footer', 'enable', 1); + email.addFilter('footer', 'text/plain', 'This is mah footer!'); sendgrid.send(email, function(err, json) { expect(err).to.be.null; expect(json.message).to.equal('success'); @@ -359,8 +359,8 @@ describe('SendGrid #skip', function () { payload.subject += "handles filters with unicode parameters"; var email = new Email(payload); - email.addFilterSetting('footer', 'enable', 1); - email.addFilterSetting('footer', 'text/plain', 'This is mah footer with a ✔ in it!'); + email.addFilter('footer', 'enable', 1); + email.addFilter('footer', 'text/plain', 'This is mah footer with a ✔ in it!'); sendgrid.send(email, function(err, json) { expect(err).to.be.null; expect(json.message).to.equal('success'); @@ -373,7 +373,7 @@ describe('SendGrid #skip', function () { payload.subject += "handles substitution values"; var email = new Email(payload); - email.addSubVal('-name-',['Panda', 'Cow']); + email.addSubstitution('-name-',['Panda', 'Cow']); email.html = 'You are a <strong>-name-</strong>'; sendgrid.send(email, function(err, json) { expect(err).to.be.null; @@ -388,8 +388,8 @@ describe('SendGrid #skip', function () { var email = new Email(payload); //mail.addTo(setup.multi_to); - email.addSubVal('-name-', ['Kyle', 'David']); - email.addSubVal('-meme-', ['-kyleSection-', '-davidSection-']); + email.addSubstitution('-name-', ['Kyle', 'David']); + email.addSubstitution('-meme-', ['-kyleSection-', '-davidSection-']); email.addSection({'-kyleSection-': 'I heard you liked batman so I killed your parents'}); email.addSection({'-davidSection-': 'Metal gear?!!?!!!!eleven'}); email.html = "Yo -name-!<br /> Here's a meme for you:<br /> -meme-"; |