1
0
Fork 0
mirror of https://github.com/Ylianst/MeshCentral.git synced 2025-03-09 15:40:18 +00:00

Email device connect/disconnect notifications now work.

This commit is contained in:
Ylian Saint-Hilaire 2021-10-11 11:56:14 -07:00
parent 20b17b64b7
commit 4ad9619bfb
42 changed files with 1223 additions and 18 deletions

View file

@ -337,6 +337,53 @@ module.exports.CreateMeshMail = function (parent, domain) {
});
};
// Send device connect/disconnect notification mail
obj.sendDeviceNotifyMail = function (domain, username, email, connections, disconnections, language, loginkey) {
obj.checkEmail(email, function (checked) {
if (checked) {
parent.debug('email', "Sending device notification to " + email);
if ((parent.certificates == null) || (parent.certificates.CommonName == null) || (parent.certificates.CommonName.indexOf('.') == -1)) {
parent.debug('email', "Error: Server name not set."); // If the server name is not set, email not possible.
return;
}
var template = getTemplate('device-notify', domain, language);
if ((template == null) || (template.htmlSubject == null) || (template.txtSubject == null)) {
parent.debug('email', "Error: Failed to get mail template."); // No email template found
return;
}
// Set all the template replacement options and generate the final email text (both in txt and html formats).
var optionsHtml = { username: username, email: email, servername: domain.title ? domain.title : 'MeshCentral', header: true, footer: false };
var optionsTxt = { username: username, email: email, servername: domain.title ? domain.title : 'MeshCentral', header: true, footer: false };
if ((connections == null) || (connections.length == 0)) {
optionsHtml.connections = false;
optionsTxt.connections = false;
} else {
optionsHtml.connections = connections.join('<br />\r\n');
optionsTxt.connections = connections.join('\r\n');
}
if ((disconnections == null) || (disconnections.length == 0)) {
optionsHtml.disconnections = false;
optionsTxt.disconnections = false;
} else {
optionsHtml.disconnections = disconnections.join('<br />\r\n');
optionsTxt.disconnections = disconnections.join('\r\n');
}
// Get from field
var from = null;
if (obj.config.sendgrid && (typeof obj.config.sendgrid.from == 'string')) { from = obj.config.sendgrid.from; }
else if (obj.config.smtp && (typeof obj.config.smtp.from == 'string')) { from = obj.config.smtp.from; }
// Send the email
obj.pendingMails.push({ to: email, from: from, subject: mailReplacements(template.htmlSubject, domain, optionsTxt), text: mailReplacements(template.txt, domain, optionsTxt), html: mailReplacements(template.html, domain, optionsHtml) });
sendNextMail();
}
});
};
// Send out the next mail in the pending list
function sendNextMail() {
if ((obj.sendingMail == true) || (obj.pendingMails.length == 0)) { return; }
@ -463,7 +510,7 @@ module.exports.CreateMeshMail = function (parent, domain) {
// Add the user and start a timer
if (obj.deviceNotifications[user._id] == null) {
obj.deviceNotifications[user._id] = { nodes: {} };
obj.deviceNotifications[user._id].timer = setTimeout(function () { sendDeviceNotifications(user._id); }, 20000);
obj.deviceNotifications[user._id].timer = setTimeout(function () { sendDeviceNotifications(user._id); }, 5 * 60 * 1000); // 5 minute before email is sent
}
// Add the device
@ -527,7 +574,7 @@ module.exports.CreateMeshMail = function (parent, domain) {
// Add the user and start a timer
if (obj.deviceNotifications[user._id] == null) {
obj.deviceNotifications[user._id] = { nodes: {} };
obj.deviceNotifications[user._id].timer = setTimeout(function () { sendDeviceNotifications(user._id); }, 20000);
obj.deviceNotifications[user._id].timer = setTimeout(function () { sendDeviceNotifications(user._id); }, 5 * 60 * 1000); // 5 minute before email is sent
}
// Add the device
@ -617,9 +664,16 @@ module.exports.CreateMeshMail = function (parent, domain) {
connections.sort(sortCollator.compare);
disconnections.sort(sortCollator.compare);
// TODO: Send the email
//console.log('sendDeviceNotifications', connections, disconnections);
// Get the user and domain
const user = parent.webserver.users[userid];
if ((user == null) || (user.email == null) || (user.emailVerified !== true)) return;
const domain = obj.parent.config.domains[user.domain];
if (domain == null) return;
// Send the email
obj.sendDeviceNotifyMail(domain, user.name, user.email, connections, disconnections, 'us-en', null);
// Clean up
delete obj.deviceNotifications[userid];
}