diff options
-rw-r--r-- | .jshintrc | 30 | ||||
-rw-r--r-- | lib/email.js | 20 | ||||
-rw-r--r-- | lib/file_handler.js | 6 | ||||
-rw-r--r-- | lib/sendgrid.js | 38 | ||||
-rw-r--r-- | lib/smtpapi_headers.js | 26 | ||||
-rw-r--r-- | package.json | 4 | ||||
-rw-r--r-- | test/lib/smtpapi_header.test.js | 2 |
7 files changed, 85 insertions, 41 deletions
diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..a332273 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,30 @@ +{ + "node" : true, + "strict" : true, + "globalstrict" : true, + "loopfunc" : false, + "noarg" : true, + "regexp" : false, + "regexdash" : false, + "undef" : true, + "unused" : true, + "noempty" : true, + "nonew" : true, + "nomen" : false, + "onevar" : false, + "onecase" : true, + "plusplus" : false, + "proto" : false, + "sub" : true, + "trailing" : true, + "white" : false, + "predef": [ + "describe", + "it", + "before", + "beforeEach", + "after", + "afterEach", + "expect" + ] +} diff --git a/lib/email.js b/lib/email.js index dfa6382..293f974 100644 --- a/lib/email.js +++ b/lib/email.js @@ -2,7 +2,7 @@ var SmtpapiHeaders = require('./smtpapi_headers'); var FileHandler = require('./file_handler'); -var _ = require('underscore'); +var _ = require('lodash'); var request = require('request'); var fs = require('fs'); @@ -46,10 +46,10 @@ function Email(params) { }); this.to = first_to; } - if(params.toname != null) { + if(params.toname !== null) { this.toname = params.toname; } - if(params.fromname != null) { + if(params.fromname !== null) { this.fromname = params.fromname; } // auto handle calling the constructor for the file handler @@ -200,7 +200,8 @@ Email.prototype.toWebFormat = function() { text : this.text, html : this.html, headers : JSON.stringify(this.headers) - } + }; + if (this.bcc) { web.bcc = this.bcc; } if (this.html) { web.html = this.html; } if (this.toname) { web.toname = this.toname; } @@ -211,14 +212,13 @@ Email.prototype.toWebFormat = function() { if (this.files) { this.files.forEach(function(file) { - var content; if (file.url) { file.content = request(file.url); } else if (file.path) { file.content = fs.createReadStream(file.path); } - + web['files[' + file.filename + ']'] = { filename : file.filename, content : file.content || " ", @@ -245,13 +245,13 @@ Email.prototype.toSmtpFormat = function() { body : this.text, html : this.html, reply_to : this.replyto, - headers : this.headers - }; + headers : this.headers + }; smtp.headers['x-smtpapi'] = this.smtpapi.toJson(); this._formatFilesForNodeMailer(this.files); - + this.updateMissingTo(smtp); smtp.attachments = this.files; @@ -264,7 +264,7 @@ Email.prototype._formatFilesForNodeMailer = function(files) { files.forEach(function(file) { self._formatFileForNodeMailer(file); }); - } + } }; Email.prototype._formatFileForNodeMailer = function(file) { diff --git a/lib/file_handler.js b/lib/file_handler.js index 56e41a6..1b19d63 100644 --- a/lib/file_handler.js +++ b/lib/file_handler.js @@ -1,6 +1,8 @@ +"use strict"; + var http = require('http'); var mime = require('mime'); -var _ = require('underscore'); +var _ = require('lodash'); var url = require('url'); var fs = require('fs'); @@ -128,6 +130,6 @@ FileHandler.prototype.loadContent = function(callback) { } else { callback(true, 'Upload method not supported'); } -} +}; module.exports = FileHandler; diff --git a/lib/sendgrid.js b/lib/sendgrid.js index d0b3375..6014277 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -24,7 +24,7 @@ var Sendgrid = function(api_user, api_key, options) { } web.apply( this, arguments ); - } + }; /* * Sends an email via web and returns true if the @@ -35,8 +35,8 @@ var Sendgrid = function(api_user, api_key, options) { * @param {Function} callback A function to call when the processing is done. * This parameter is optional. */ - var web = function(email, callback) { - var callback = callback || function() { }; + var web = function(email, cb) { + var callback = cb || function() { }; if (email.constructor !== Email) { email = new Email(email); } @@ -55,15 +55,15 @@ var Sendgrid = function(api_user, api_key, options) { * @param {Function} callback A function to call when the processing is done. * This parameter is optional. */ - var smtp = function(email, nodeMailerOptions, callback) { + var smtp = function(email, nodeMailerOptions, cb) { // Support a callback without nodeMailerOptions - if (! callback && typeof nodeMailerOptions === "function") { + if (! cb && typeof nodeMailerOptions === "function") { callback = nodeMailerOptions; nodeMailerOptions = null; } - var callback = callback || function() { }; + var callback = cb || function() { }; if (email.constructor !== Email) { email = new Email(email); @@ -108,6 +108,12 @@ var Sendgrid = function(api_user, api_key, options) { form['api_key'] = api_key; var reqForm = req.form(); + + var _reqFormAppend = function(field, value) { + console.log('fields', field, value); + reqForm.append(field, value); + }; + for (var field in form) { var value = form[field]; if (value && value.filename) { @@ -118,11 +124,12 @@ var Sendgrid = function(api_user, api_key, options) { } else { try { if(!Array.isArray(value)){ - reqForm.append(field, value); + //reqForm.append(field, value); + _reqFormAppend.bind(this)(field, value); }else{ - value.forEach(function(each){ - reqForm.append(field, each); - }); + console.log('field', field, value); + value.forEach(_reqFormAppend.bind(this, field)); + } } catch(err) {} } @@ -133,13 +140,14 @@ var Sendgrid = function(api_user, api_key, options) { // SMTP settings var smtp_settings = { host: "smtp.sendgrid.net", - port: parseInt(_this.port), + port: parseInt(_this.port, 10), requiresAuth: true, auth: { user: api_user, pass: api_key } - } + }; + if (smtp_settings.port == 465) { smtp_settings['secureConnection'] = true; } @@ -152,13 +160,15 @@ var Sendgrid = function(api_user, api_key, options) { _.extend(smtpParams, nodeMailerOptions); } - smtpTransport.sendMail(smtpParams, function(error, response) { + smtpTransport.sendMail(smtpParams, function(error) { + //Returns: error, response; response unused + smtpTransport.close(); if(error) { return callback(new Error(error.message), null);} return callback(null, {'message': 'success'}); }); - } + }; /* diff --git a/lib/smtpapi_headers.js b/lib/smtpapi_headers.js index e87b41e..410fe0b 100644 --- a/lib/smtpapi_headers.js +++ b/lib/smtpapi_headers.js @@ -1,4 +1,6 @@ -var _ = require('underscore'); +"use strict"; + +var _ = require('lodash'); function SmtpapiHeaders() { this.to = []; @@ -15,7 +17,7 @@ SmtpapiHeaders.prototype.addTo = function(to) { } else { this.to.push(to); } -} +}; SmtpapiHeaders.prototype.addSubVal = function(key, val) { if (_.isArray(val)) { @@ -23,19 +25,19 @@ SmtpapiHeaders.prototype.addSubVal = function(key, val) { } else { this.sub[key] = [val]; } -} +}; SmtpapiHeaders.prototype.setUniqueArgs = function(val) { if (_.isObject(val)) { this.unique_args = val; } -} +}; SmtpapiHeaders.prototype.addUniqueArgs = function(val) { if (_.isObject(val)) { _.extend(this.unique_args, val); } -} +}; SmtpapiHeaders.prototype.setCategory = function(val) { if (_.isArray(val)) { @@ -43,7 +45,7 @@ SmtpapiHeaders.prototype.setCategory = function(val) { } else { this.category = [val]; } -} +}; SmtpapiHeaders.prototype.addCategory = function(val) { if (_.isArray(val)) { @@ -51,23 +53,23 @@ SmtpapiHeaders.prototype.addCategory = function(val) { } else { this.category.push(val); } -} +}; SmtpapiHeaders.prototype.setSection = function(val){ if (_.isObject(val)) { this.section = val; } -} +}; SmtpapiHeaders.prototype.addSection = function(val){ if (_.isObject(val)) { _.extend(this.section, val); } -} +}; SmtpapiHeaders.prototype.setFilterSetting = function(filters) { this.filters = filters; -} +}; SmtpapiHeaders.prototype.addFilterSetting = function(filter, setting, val) { if (!this.filters[filter]) { @@ -79,7 +81,7 @@ SmtpapiHeaders.prototype.addFilterSetting = function(filter, setting, val) { } this.filters[filter]['settings'][setting] = val; -} +}; SmtpapiHeaders.prototype.toJson = function() { var data = _.clone(this); @@ -90,6 +92,6 @@ SmtpapiHeaders.prototype.toJson = function() { }); var json = JSON.stringify(data); return json.replace(/(["\]}])([,:])(["\[{])/, '$1$2 $3'); -} +}; module.exports = SmtpapiHeaders; diff --git a/package.json b/package.json index 7447972..4f80285 100644 --- a/package.json +++ b/package.json @@ -19,10 +19,10 @@ "node": ">= 0.4.7" }, "dependencies": { - "underscore": "1.4.4", "nodemailer": "0.4.4", "mime": "1.2.9", - "request": "2.22.0" + "request": "2.22.0", + "lodash": "2.0.0" }, "devDependencies": { "dotenv": "0.0.2", diff --git a/test/lib/smtpapi_header.test.js b/test/lib/smtpapi_header.test.js index c10384b..6528267 100644 --- a/test/lib/smtpapi_header.test.js +++ b/test/lib/smtpapi_header.test.js @@ -1,5 +1,5 @@ var SmtpapiHeaders = require('../../lib/smtpapi_headers'); -var _ = require('underscore'); +var _ = require('lodash'); describe('SmtpapiHeader', function() { var header; |