2018-01-22 13:01:38 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
angular.module('Application').controller('EmailController', ['$scope', '$location', '$timeout', '$rootScope', 'Client', 'AppStore', function ($scope, $location, $timeout, $rootScope, Client, AppStore) {
|
2018-01-22 13:01:38 -08:00
|
|
|
Client.onReady(function () { if (!Client.getUserInfo().admin) $location.path('/'); });
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.ready = false;
|
|
|
|
|
$scope.refreshBusy = true;
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.client = Client;
|
|
|
|
|
$scope.user = Client.getUserInfo();
|
|
|
|
|
$scope.config = Client.getConfig();
|
2018-01-23 12:30:35 +01:00
|
|
|
|
|
|
|
|
$scope.domains = [];
|
2018-01-23 15:11:33 +01:00
|
|
|
$scope.users = [];
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.selectedDomain = null;
|
|
|
|
|
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.expectedDnsRecords = {
|
|
|
|
|
mx: { },
|
|
|
|
|
dkim: { },
|
|
|
|
|
spf: { },
|
|
|
|
|
dmarc: { },
|
|
|
|
|
ptr: { }
|
|
|
|
|
};
|
|
|
|
|
$scope.expectedDnsRecordsTypes = [
|
|
|
|
|
{ name: 'MX', value: 'mx' },
|
|
|
|
|
{ name: 'DKIM', value: 'dkim' },
|
|
|
|
|
{ name: 'SPF', value: 'spf' },
|
|
|
|
|
{ name: 'DMARC', value: 'dmarc' },
|
|
|
|
|
{ name: 'PTR', value: 'ptr' }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$scope.showView = function (view) {
|
|
|
|
|
// wait for dialog to be fully closed to avoid modal behavior breakage when moving to a different view already
|
|
|
|
|
$('.modal').on('hidden.bs.modal', function () {
|
|
|
|
|
$('.modal').off('hidden.bs.modal');
|
|
|
|
|
$location.path(view);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('.modal').modal('hide');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.isProvider = function (provider) {
|
|
|
|
|
return $scope.mailRelay.relay.provider === provider;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.catchall = {
|
|
|
|
|
addresses: [],
|
2018-01-23 15:11:33 +01:00
|
|
|
availableAddresses: [],
|
2018-01-22 13:01:38 -08:00
|
|
|
busy: false,
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.catchall.busy = true;
|
|
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
Client.setCatchallAddresses($scope.selectedDomain.domain, $scope.catchall.addresses, function (error) {
|
2018-01-22 13:01:38 -08:00
|
|
|
if (error) console.error('Unable to add catchall address.', error);
|
|
|
|
|
|
|
|
|
|
$scope.catchall.busy = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.toggleEmail = function () {
|
|
|
|
|
if ($scope.selectedDomain.mailConfig.enabled) {
|
|
|
|
|
$scope.email.disable();
|
|
|
|
|
$scope.refreshDomain();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
// show warning first
|
|
|
|
|
$('#enableEmailModal').modal('show');
|
|
|
|
|
};
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.enableEmail = function () {
|
|
|
|
|
$('#enableEmailModal').modal('hide');
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
Client.enableMailForDomain($scope.selectedDomain.domain, true , function (error) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
$scope.refreshDomain();
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.disableEmail = function () {
|
|
|
|
|
Client.enableMailForDomain($scope.selectedDomain.domain, false , function (error) {
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
$scope.refreshDomain();
|
|
|
|
|
});
|
2018-01-22 13:01:38 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.mailRelayPresets = [
|
|
|
|
|
{ provider: 'cloudron-smtp', name: 'Built-in SMTP server' },
|
|
|
|
|
{ provider: 'external-smtp', name: 'External SMTP server', host: '', port: 587 },
|
|
|
|
|
{ provider: 'ses-smtp', name: 'Amazon SES', host: 'email-smtp.us-east-1.amazonaws.com', port: 587 },
|
|
|
|
|
{ provider: 'google-smtp', name: 'Google', host: 'smtp.gmail.com', port: 587 },
|
|
|
|
|
{ provider: 'mailgun-smtp', name: 'Mailgun', host: 'smtp.mailgun.org', port: 587 },
|
|
|
|
|
{ provider: 'postmark-smtp', name: 'Postmark', host: 'smtp.postmarkapp.com', port: 587 },
|
|
|
|
|
{ provider: 'sendgrid-smtp', name: 'SendGrid', host: 'smtp.sendgrid.net', port: 587, username: 'apikey' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$scope.mailRelay = {
|
|
|
|
|
error: null,
|
|
|
|
|
success: false,
|
|
|
|
|
busy: false,
|
|
|
|
|
preset: $scope.mailRelayPresets[0],
|
|
|
|
|
|
|
|
|
|
presetChanged: function () {
|
|
|
|
|
$scope.mailRelay.error = null;
|
|
|
|
|
|
|
|
|
|
$scope.mailRelay.relay.provider = $scope.mailRelay.preset.provider;
|
|
|
|
|
$scope.mailRelay.relay.host = $scope.mailRelay.preset.host;
|
|
|
|
|
$scope.mailRelay.relay.port = $scope.mailRelay.preset.port;
|
|
|
|
|
$scope.mailRelay.relay.username = '';
|
|
|
|
|
$scope.mailRelay.relay.password = '';
|
|
|
|
|
$scope.mailRelay.relay.serverApiToken = '';
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// form data to be set on load
|
|
|
|
|
relay: {
|
|
|
|
|
provider: 'cloudron-smtp',
|
|
|
|
|
host: '',
|
|
|
|
|
port: 25,
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
|
|
|
|
serverApiToken: ''
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.mailRelay.error = null;
|
|
|
|
|
$scope.mailRelay.busy = true;
|
|
|
|
|
$scope.mailRelay.success = false;
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
provider: $scope.mailRelay.relay.provider,
|
|
|
|
|
host: $scope.mailRelay.relay.host,
|
|
|
|
|
port: $scope.mailRelay.relay.port
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// fill in provider specific username/password usage
|
|
|
|
|
if (data.provider === 'postmark-smtp') {
|
|
|
|
|
data.username = $scope.mailRelay.relay.serverApiToken;
|
|
|
|
|
data.password = $scope.mailRelay.relay.serverApiToken;
|
|
|
|
|
} else if (data.provider === 'sendgrid-smtp') {
|
|
|
|
|
data.username = 'apikey';
|
|
|
|
|
data.password = $scope.mailRelay.relay.serverApiToken;
|
|
|
|
|
} else {
|
|
|
|
|
data.username = $scope.mailRelay.relay.username;
|
|
|
|
|
data.password = $scope.mailRelay.relay.password;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
Client.setMailRelay($scope.selectedDomain.domain, data, function (error) {
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.mailRelay.busy = false;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
$scope.mailRelay.error = error.message;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.selectedDomain.relay = data;
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.mailRelay.success = true;
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.refreshDomain();
|
|
|
|
|
|
|
|
|
|
// clear success indicator after 3sec
|
|
|
|
|
$timeout(function () { $scope.mailRelay.success = false; }, 3000);
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.testEmail = {
|
|
|
|
|
busy: false,
|
|
|
|
|
error: {},
|
|
|
|
|
|
|
|
|
|
mailTo: '',
|
|
|
|
|
|
|
|
|
|
clearForm: function () {
|
|
|
|
|
$scope.testEmail.mailTo = '';
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
|
$scope.testEmail.error = {};
|
|
|
|
|
$scope.testEmail.busy = false;
|
|
|
|
|
|
|
|
|
|
$scope.testEmail.mailTo = $scope.user.email;
|
|
|
|
|
|
|
|
|
|
$('#testEmailModal').modal('show');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.testEmail.error = {};
|
|
|
|
|
$scope.testEmail.busy = true;
|
|
|
|
|
|
|
|
|
|
Client.sentTestMail($scope.testEmail.mailTo, function (error) {
|
|
|
|
|
$scope.testEmail.busy = false;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
$scope.testEmail.error.generic = error.message;
|
|
|
|
|
console.error(error);
|
|
|
|
|
$('#inputTestMailTo').focus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$('#testEmailModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
function collapseDnsRecords() {
|
|
|
|
|
$scope.expectedDnsRecordsTypes.forEach(function (record) {
|
|
|
|
|
var type = record.value;
|
|
|
|
|
$('#collapse_dns_' + type).collapse('hide');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#collapse_outbound_smtp').collapse('hide');
|
|
|
|
|
$('#collapse_rbl').collapse('hide');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showExpectedDnsRecords() {
|
|
|
|
|
// open the record details if they are not correct
|
|
|
|
|
$scope.expectedDnsRecordsTypes.forEach(function (record) {
|
|
|
|
|
var type = record.value;
|
|
|
|
|
$scope.expectedDnsRecords[type] = $scope.selectedDomain.mailStatus.dns[type] || {};
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
if (!$scope.expectedDnsRecords[type].status) {
|
|
|
|
|
$('#collapse_dns_' + type).collapse('show');
|
|
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
2018-01-23 12:30:35 +01:00
|
|
|
|
|
|
|
|
if (!$scope.selectedDomain.mailStatus.relay.status) {
|
|
|
|
|
$('#collapse_outbound_smtp').collapse('show');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$scope.selectedDomain.mailStatus.rbl.status) {
|
|
|
|
|
$('#collapse_rbl').collapse('show');
|
|
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
}
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.refreshDomain = function () {
|
|
|
|
|
$scope.refreshBusy = true;
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
collapseDnsRecords();
|
|
|
|
|
|
|
|
|
|
Client.getMailConfigForDomain($scope.selectedDomain.domain, function (error, mailConfig) {
|
|
|
|
|
if (error) {
|
|
|
|
|
$scope.refreshBusy = false;
|
|
|
|
|
return console.error(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pre-fill the form
|
|
|
|
|
$scope.mailRelay.relay.provider = mailConfig.relay.provider;
|
|
|
|
|
$scope.mailRelay.relay.host = mailConfig.relay.host;
|
|
|
|
|
$scope.mailRelay.relay.port = mailConfig.relay.port;
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.mailRelay.relay.username = '';
|
|
|
|
|
$scope.mailRelay.relay.password = '';
|
|
|
|
|
$scope.mailRelay.relay.serverApiToken = '';
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
if (mailConfig.relay.provider === 'postmark-smtp') {
|
|
|
|
|
$scope.mailRelay.relay.serverApiToken = mailConfig.relay.username;
|
|
|
|
|
} else if (mailConfig.relay.provider === 'sendgrid-smtp') {
|
|
|
|
|
$scope.mailRelay.relay.serverApiToken = mailConfig.relay.password;
|
2018-01-22 13:01:38 -08:00
|
|
|
} else {
|
2018-01-23 12:30:35 +01:00
|
|
|
$scope.mailRelay.relay.username = mailConfig.relay.username;
|
|
|
|
|
$scope.mailRelay.relay.password = mailConfig.relay.password;
|
2018-01-22 13:01:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < $scope.mailRelayPresets.length; i++) {
|
2018-01-23 12:30:35 +01:00
|
|
|
if ($scope.mailRelayPresets[i].provider === mailConfig.relay.provider) {
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.mailRelay.preset = $scope.mailRelayPresets[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
// catch-all, only allow users with a Cloudron email address
|
|
|
|
|
$scope.catchall.availableAddresses = $scope.users.filter(function (u) { return !!u.username && !!u.email; }).map(function (u) { return u.username; });
|
|
|
|
|
|
|
|
|
|
// dedupe in case to avoid angular breakage
|
|
|
|
|
$scope.catchall.addresses = mailConfig.catchAll.filter(function(item, pos, self) {
|
|
|
|
|
return self.indexOf(item) == pos;
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-23 12:41:52 +01:00
|
|
|
// amend to selected domain to be available for the UI
|
|
|
|
|
$scope.selectedDomain.mailConfig = mailConfig;
|
|
|
|
|
$scope.selectedDomain.mailStatus = {};
|
|
|
|
|
|
|
|
|
|
// we will fetch the status without blocking the ui
|
2018-01-23 12:30:35 +01:00
|
|
|
Client.getMailStatusForDomain($scope.selectedDomain.domain, function (error, mailStatus) {
|
2018-01-23 12:41:52 +01:00
|
|
|
$scope.refreshBusy = false;
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:41:52 +01:00
|
|
|
if (error) return console.error(error);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:41:52 +01:00
|
|
|
$scope.selectedDomain.mailStatus = mailStatus;
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
console.log($scope.selectedDomain)
|
|
|
|
|
|
2018-01-23 12:30:35 +01:00
|
|
|
showExpectedDnsRecords();
|
|
|
|
|
});
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
2018-01-23 12:30:35 +01:00
|
|
|
};
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 12:38:19 +01:00
|
|
|
$scope.refreshStatus = function () {
|
|
|
|
|
$scope.refreshBusy = true;
|
|
|
|
|
|
|
|
|
|
Client.getMailStatusForDomain($scope.selectedDomain.domain, function (error, mailStatus) {
|
|
|
|
|
if (error) {
|
|
|
|
|
$scope.refreshBusy = false;
|
|
|
|
|
return console.error(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// overwrite the selected domain status to be available for the UI
|
|
|
|
|
$scope.selectedDomain.mailStatus = mailStatus;
|
|
|
|
|
|
|
|
|
|
showExpectedDnsRecords();
|
|
|
|
|
|
|
|
|
|
$scope.refreshBusy = false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
Client.onReady(function () {
|
|
|
|
|
Client.getUsers(function (error, users) {
|
|
|
|
|
if (error) return console.error('Unable to get user listing.', error);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
$scope.users = users;
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
Client.getDomains(function (error, domains) {
|
|
|
|
|
if (error) return console.error('Unable to get domain listing.', error);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
$scope.domains = domains;
|
|
|
|
|
$scope.selectedDomain = $scope.domains[0];
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
$scope.refreshDomain();
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2018-01-23 15:11:33 +01:00
|
|
|
$scope.ready = true;
|
|
|
|
|
});
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// setup all the dialog focus handling
|
|
|
|
|
['testEmailModal'].forEach(function (id) {
|
|
|
|
|
$('#' + id).on('shown.bs.modal', function () {
|
|
|
|
|
$(this).find("[autofocus]:first").focus();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('.modal-backdrop').remove();
|
|
|
|
|
}]);
|