diff --git a/lib/email.js b/lib/email.js index e77061a90..100d2c637 100644 --- a/lib/email.js +++ b/lib/email.js @@ -128,9 +128,9 @@ Email.prototype.toWebFormat = function() { // there needs to be at least 1 to address. // If it is missing, just copy over the from. - if (_.isEmpty(data.to)) { - data.to = data.from; - } + // Only do this is we have a value for the to + // address on this.smtpapi + this.checkAddTo(data); return data; } @@ -156,14 +156,20 @@ Email.prototype.toSmtpFormat = function() { } // there needs to be at least 1 to address. - // If it is missing, just copy the sender. - if (_.isEmpty(data.to)) { - data.to = data.sender; - } + // If it is missing, just copy over the from. + // Only do this is we have a value for the to + // address on this.smtpapi + this.checkAddTo(data); return data; } +Email.prototype.checkAddTo = function(data) { + if (_.isEmpty(data.to) && this.smtpapi.to && !_.isEmpty(this.smtpapi.to)) { + data.to = data.from; + } +} + Email.prototype.hasFiles = function() { return _(this.files).size() > 0; } diff --git a/test/lib/email.test.js b/test/lib/email.test.js index 2a29cfb48..57b8cf9a9 100644 --- a/test/lib/email.test.js +++ b/test/lib/email.test.js @@ -46,6 +46,12 @@ describe('Email', function () { expect(smtpFormat.body).to.equal(text_params.text); }); + it('should not have a to address if there is no to or no smtpapi.to set via Smtp Api', function() { + var email = new Email({from: 'test@test.com', subject: 'testing', text: 'testing'}); + var smtpFormat = email.toSmtpFormat(); + expect(smtpFormat.to).to.be.empty; + }); + it('should support file attachments', function() { var email = new Email(); email.addFile('file1', files[0]);