diff --git a/src/js/controller/write.js b/src/js/controller/write.js index 56b9a3b..8818522 100644 --- a/src/js/controller/write.js +++ b/src/js/controller/write.js @@ -153,20 +153,13 @@ define(function(require) { }; $scope.sendToOutbox = function() { - var to, email; - - // validate recipients - to = $scope.to.replace(/\s/g, '').split(/[,;]/); - if (!to || to.length < 1) { - $scope.onError({ - errMsg: 'Seperate recipients with a comma!', - sync: true - }); - return; - } + var email; + // build email model for smtp-client email = { - to: [], // list of receivers + to: [], + cc: [], + bcc: [], subject: $scope.subject, // Subject line body: $scope.body // use parsed plaintext body }; @@ -174,13 +167,34 @@ define(function(require) { name: '', address: emailDao._account.emailAddress }]; - to.forEach(function(address) { - email.to.push({ - name: '', - address: address - }); - }); + // validate recipients and gather public keys + email.receiverKeys = []; // gather public keys for emailDao._encrypt + + appendReceivers($scope.to, email.to); + appendReceivers($scope.cc, email.cc); + appendReceivers($scope.bcc, email.bcc); + + function appendReceivers(srcField, destField) { + srcField.forEach(function(recipient) { + // validate address + if (!util.validateEmailAddress(recipient.address)) { + return; + } + + // append address to email model + destField.push({ + address: recipient.address + }); + + // add public key to list of recipient keys + if (recipient.key && recipient.key.publicKey) { + email.receiverKeys.push(recipient.key.publicKey); + } + }); + } + + // persist the email locally for later smtp transmission emailDao.store(email, function(err) { if (err) { $scope.onError(err); diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 8352354..ebe818c 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -882,51 +882,17 @@ define(function(require) { return; } - // validate email addresses - for (var i = email.to.length - 1; i >= 0; i--) { - if (!util.validateEmailAddress(email.to[i].address)) { - callback({ - errMsg: 'Invalid recipient: ' + email.to[i].address - }); - return; - } - } - - if (!util.validateEmailAddress(email.from[0].address)) { - callback({ - errMsg: 'Invalid sender: ' + email.from - }); - return; - } - - // only support single recipient for e-2-e encryption - // check if receiver has a public key - self._keychain.getReceiverPublicKey(email.to[0].address, function(err, receiverPubkey) { + // public key found... encrypt and send + self._encrypt({ + email: email, + keys: email.receiverKeys // this Array is set in writer controller + }, function(err, email) { if (err) { callback(err); return; } - // validate public key - if (!receiverPubkey) { - callback({ - errMsg: 'User has no public key yet!' - }); - return; - } - - // public key found... encrypt and send - self._encrypt({ - email: email, - keys: [receiverPubkey.publicKey] - }, function(err, email) { - if (err) { - callback(err); - return; - } - - self._smtpClient.send(email, callback); - }); + self._smtpClient.send(email, callback); }); };