diff options
Diffstat (limited to 'lib/email.js')
-rw-r--r-- | lib/email.js | 65 |
1 files changed, 36 insertions, 29 deletions
diff --git a/lib/email.js b/lib/email.js index f4b7f76..dca2b11 100644 --- a/lib/email.js +++ b/lib/email.js @@ -1,7 +1,5 @@ "use strict"; -var https = require('https'); -var http = require('http'); var Step = require('step'); var SmtpapiHeaders = require('./smtpapi_headers'); var FileHandler = require('./file_handler'); @@ -213,33 +211,42 @@ Email.prototype.processFiles = function(callback) { }; /** - * This method returns the email object is a format to be consumed by the SendGrid.send + * This method returns the email object is a format to be consumed by the SendGrid.web * using the web api * - * @see SendGrid.send + * @see SendGrid.web */ Email.prototype.toWebFormat = function() { - var data = { - to: this.to, - from: this.from, - 'x-smtpapi': this.smtpapi.toJson(), - subject: this.subject, - text: this.text, - html: this.html, - bcc: this.bcc, - replyto: this.replyto, - headers: JSON.stringify(this.headers) - }; - - if (this.fromname != null) { - data.fromname = this.fromname; + var web = { + to : this.to, + from : this.from, + 'x-smtpapi' : this.smtpapi.toJson(), + subject : this.subject, + text : this.text, + html : this.html, + headers : JSON.stringify(this.headers) } - if (this.toname != null) { - data.toname = this.toname; + if (this.bcc) { web.bcc = this.bcc; } + if (this.html) { web.html = this.html; } + if (this.toname) { web.toname = this.toname; } + if (this.fromname) { web.fromname = this.fromname; } + if (this.replyto) { web.replyto = this.replyto; } + + this.updateMissingTo(web); + + if (this.files) { + for (var index in this.files) { + var file = this.files[index]; + web['files[' + file.filename + ']'] = { + filename : file.filename, + content : file.content || " ", + contentType : file.contentType, + cid : file.cid + }; + } } - this.updateMissingTo(data); - return data; + return web; }; /** @@ -249,7 +256,7 @@ Email.prototype.toWebFormat = function() { * @see SendGrid.smtp */ Email.prototype.toSmtpFormat = function() { - var data = { + var smtp = { to : this.to, sender : this.from, subject : this.subject, @@ -259,14 +266,14 @@ Email.prototype.toSmtpFormat = function() { headers : this.headers }; - data.headers['x-smtpapi'] = this.smtpapi.toJson(); + smtp.headers['x-smtpapi'] = this.smtpapi.toJson(); this._formatFilesForNodeMailer(this.files); - this.updateMissingTo(data); - data.attachments = this.files; + this.updateMissingTo(smtp); + smtp.attachments = this.files; - return data; + return smtp; }; Email.prototype._formatFilesForNodeMailer = function(files) { @@ -303,7 +310,7 @@ Email.prototype._formatFileForNodeMailer = function(file) { * @param {object} data The data parameter to send via Rest or SMTP */ Email.prototype.updateMissingTo = function(data) { - if (_.isEmpty(data.to) && this.smtpapi.to && !_.isEmpty(this.smtpapi.to)) { + if (data.to.length <= 0 && this.smtpapi.to && this.smtpapi.to.length > 0) { data.to = this.from; } }; @@ -314,7 +321,7 @@ Email.prototype.updateMissingTo = function(data) { * @return boolean */ Email.prototype.hasFiles = function() { - return _(this.files).size() > 0; + return this.files.length > 0; }; // export the object as the only object in this module |