diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/email.js | 54 | ||||
-rw-r--r-- | lib/sendgrid.js | 4 |
2 files changed, 29 insertions, 29 deletions
diff --git a/lib/email.js b/lib/email.js index ab25a45..3e9df13 100644 --- a/lib/email.js +++ b/lib/email.js @@ -3,6 +3,7 @@ var querystring = require('querystring'); var https = require('https'); var http = require('http'); +var mime = require('mime'); var _ = require('underscore'); var fs = require('fs'); var SmtpapiHeaders = require('./smtpapi_headers'); @@ -36,59 +37,59 @@ function Email(params) { */ Email.prototype.validate = function() { //TODO: add validation -} +}; Email.prototype.setHeaders = function(val) { if (_.isObject(val)) { this.headers = val; } -} +}; -Email.prototype.addHeaders = function(val){ +Email.prototype.addHeaders = function(val) { if (_.isObject(val)) { _.extend(this.headers, val); } -} +}; Email.prototype.addTo = function(to) { this.smtpapi.addTo(to); -} +}; Email.prototype.addSubVal = function(key, val) { this.smtpapi.addSubVal(key, val); -} +}; Email.prototype.setUniqueArgs = function(val) { this.smtpapi.setUniqueArgs(val); -} +}; Email.prototype.addUniqueArgs = function (val) { this.smtpapi.addUniqueArgs(val); -} +}; Email.prototype.setCategory = function(val) { this.smtpapi.setCategory(val); -} +}; Email.prototype.addCategory = function(val) { this.smtpapi.addCategory(val); -} +}; Email.prototype.setSection = function(val) { this.smtpapi.setSection(val); -} +}; Email.prototype.addSection = function(val) { this.smtpapi.addSection(val); -} +}; Email.prototype.addFilterSetting = function(filter, setting, val) { this.smtpapi.addFilterSetting(filter, setting, val); -} +}; Email.prototype.setFilterSetting = function(filters) { this.smtpapi.setFilterSetting(filters); -} +}; Email.prototype.addFile = function(file_object) { var file = file_object; @@ -96,26 +97,27 @@ Email.prototype.addFile = function(file_object) { file.type = 'content'; } else if (!_.isUndefined(file.path)) { file.type = 'path'; - if (_.isEmtpy(file.contentType)) { + if (_.isEmpty(file.contentType)) { file.contentType = mime.lookup(file.path); } } else if (!_.isUndefined(file.url)) { file.type = 'url'; - if (_.isEmtpy(file.contentType)) { + if (_.isEmpty(file.contentType)) { file.contentType = mime.lookup(file.url); + } } else { file.type = 'none'; file.contentType = 'none'; } this.files.push(file); -} +}; Email.prototype.processFiles = function(callback) { var self = this; var attachments_count = _.size(this.files); _(this.files).each(function(file) { if (file.type === 'path') { - fs.readFile(file, function(error, data) { + fs.readFile(file.path, function(error, data) { attachments_count--; if (error) { return callback(false, error); @@ -155,11 +157,9 @@ Email.prototype.processFiles = function(callback) { } if (attachments_count == 0) { callback(true); - } else { - callback(false); } }); -} +}; Email.prototype.toWebFormat = function() { var data = { @@ -177,7 +177,7 @@ Email.prototype.toWebFormat = function() { this.updateMissingTo(data); return data; -} +}; Email.prototype.toSmtpFormat = function() { var data = { @@ -193,8 +193,8 @@ Email.prototype.toSmtpFormat = function() { if (_.size(this.file_data) > 0) { var attachments = []; - _(this.file_data).each(function(v, k) { - attachments.push({filename: k, contents: v}); + _(this.files).each(function(file) { + attachments.push({filename: file.filename, contents: file.contents}); }); data.attachments = attachments; } @@ -202,7 +202,7 @@ Email.prototype.toSmtpFormat = function() { this.updateMissingTo(data); return data; -} +}; /* * There needs to be at least 1 to address, or else the mail won't send. @@ -214,10 +214,10 @@ Email.prototype.updateMissingTo = function(data) { if (_.isEmpty(data.to) && this.smtpapi.to && !_.isEmpty(this.smtpapi.to)) { data.to = this.from; } -} +}; Email.prototype.hasFiles = function() { return _(this.files).size() > 0; -} +}; module.exports = Email; diff --git a/lib/sendgrid.js b/lib/sendgrid.js index 0a8a0a9..870bd7b 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -186,8 +186,8 @@ SendGrid.prototype.getMultipartData = function(email, boundary) { data.push(new Buffer(encodeField(boundary, k, v))); }); - _(email.files).each(function(filepath, filename) { - data.push(encodeFile(boundary, mime.lookup(filepath), 'files[' + filename + ']', path.basename(filepath))); + _(email.files).each(function(file) { + data.push(encodeFile(boundary, file.contentType, 'files[' + file.filename + ']', file.filename)); data.push(new Buffer(email.file_data[filename])); data.push(new Buffer('\r\n')); }); |