mirror of
https://github.com/moparisthebest/mail
synced 2025-02-16 23:20:09 -05:00
implement send to outbox and check outbox periodically
This commit is contained in:
parent
8add506135
commit
5b895cb61e
@ -27,7 +27,8 @@ define([], function() {
|
|||||||
port: 465,
|
port: 465,
|
||||||
host: 'smtp.gmail.com'
|
host: 'smtp.gmail.com'
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
checkOutboxInterval: 30000
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,7 +4,8 @@ define(function(require) {
|
|||||||
var angular = require('angular'),
|
var angular = require('angular'),
|
||||||
appController = require('js/app-controller'),
|
appController = require('js/app-controller'),
|
||||||
_ = require('underscore'),
|
_ = require('underscore'),
|
||||||
emailDao;
|
config = require('js/app-config').config,
|
||||||
|
emailDao, senderIntervalId;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Controller
|
// Controller
|
||||||
@ -91,6 +92,69 @@ define(function(require) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$scope.sendFirstFromOutbox = function() {
|
||||||
|
var dbType = 'email_OUTBOX',
|
||||||
|
outbox = _.findWhere($scope.folders, {
|
||||||
|
type: 'Outbox'
|
||||||
|
});
|
||||||
|
|
||||||
|
checkStorage();
|
||||||
|
|
||||||
|
function checkStorage() {
|
||||||
|
// get last item from outbox
|
||||||
|
emailDao._devicestorage.listItems(dbType, 0, null, function(err, pending) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update outbox folder count
|
||||||
|
outbox.count = pending.length;
|
||||||
|
$scope.$apply();
|
||||||
|
if (pending.length < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// sending the first one pending
|
||||||
|
send(pending[0]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function send(email) {
|
||||||
|
emailDao.smtpSend(email, function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
removeFromStorage(email.id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeFromStorage(id) {
|
||||||
|
if (!id) {
|
||||||
|
console.error('Cannot remove email from storage without a valid id!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete email from local storage
|
||||||
|
var key = dbType + '_' + id;
|
||||||
|
emailDao._devicestorage.removeList(key, function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.error(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
outbox.count = (outbox.count > 0) ? outbox.count - 1 : outbox.count;
|
||||||
|
$scope.$apply();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// Start
|
||||||
|
//
|
||||||
|
|
||||||
initFolders(function(folders) {
|
initFolders(function(folders) {
|
||||||
$scope.folders = folders;
|
$scope.folders = folders;
|
||||||
// select inbox as the current folder on init
|
// select inbox as the current folder on init
|
||||||
@ -113,6 +177,9 @@ define(function(require) {
|
|||||||
f.count = 0;
|
f.count = 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// start checking outbox periodically
|
||||||
|
startOutboxSender();
|
||||||
|
|
||||||
callback(folders);
|
callback(folders);
|
||||||
$scope.$apply();
|
$scope.$apply();
|
||||||
});
|
});
|
||||||
@ -141,6 +208,11 @@ define(function(require) {
|
|||||||
path: 'TRASH'
|
path: 'TRASH'
|
||||||
}]);
|
}]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function startOutboxSender() {
|
||||||
|
// start periodic checking of outbox
|
||||||
|
senderIntervalId = setInterval($scope.sendFirstFromOutbox, config.checkOutboxInterval);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -76,7 +76,7 @@ define(function(require) {
|
|||||||
$scope.ciphertextPreview = (body) ? aes.encrypt(body, key, iv) : '';
|
$scope.ciphertextPreview = (body) ? aes.encrypt(body, key, iv) : '';
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.sendEmail = function() {
|
$scope.sendToOutbox = function() {
|
||||||
var to, body, email;
|
var to, body, email;
|
||||||
|
|
||||||
// validate recipients
|
// validate recipients
|
||||||
@ -106,15 +106,18 @@ define(function(require) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
emailDao.smtpSend(email, function(err) {
|
// set an id for the email and store in outbox
|
||||||
|
email.id = util.UUID();
|
||||||
|
emailDao._devicestorage.storeList([email], 'email_OUTBOX', function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err);
|
console.error(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ps = $scope.$parent.$parent;
|
var ps = $scope.$parent.$parent;
|
||||||
ps.closeWriter();
|
ps.closeWriter();
|
||||||
ps.$apply();
|
ps.$apply();
|
||||||
|
ps.sendFirstFromOutbox();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
</div><!--/.body-->
|
</div><!--/.body-->
|
||||||
|
|
||||||
<div class="send-control">
|
<div class="send-control">
|
||||||
<button ng-click="sendEmail()" class="btn" ng-disabled="!to" tabindex="5">Send securely</button>
|
<button ng-click="sendToOutbox()" class="btn" ng-disabled="!to" tabindex="5">Send securely</button>
|
||||||
</div>
|
</div>
|
||||||
</div><!--/.write-view-->
|
</div><!--/.write-view-->
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user