diff --git a/meshcentral-config-schema.json b/meshcentral-config-schema.json index b6edad3a..9edc3e24 100644 --- a/meshcentral-config-schema.json +++ b/meshcentral-config-schema.json @@ -1051,7 +1051,8 @@ "properties": { "from": { "type": "string", "format": "email", "description": "Email address used in the messages from field." }, "apikey": { "type": "string", "description": "The SendGrid API key." }, - "verifyemail": { "type": "boolean", "default": true, "description": "When set to false, the email format and DNS MX record are not checked." } + "verifyemail": { "type": "boolean", "default": true, "description": "When set to false, the email format and DNS MX record are not checked." }, + "emailDelaySeconds": { "type": "integer", "default": 300, "description": "Time to wait before sending a device connection/disconnection notification email. If many events occur, they will be merged into a single email."} }, "required": [ "from", "apikey" ] }, @@ -1093,11 +1094,8 @@ }, "tlscertcheck": { "type": "boolean" }, "tlsstrict": { "type": "boolean" }, - "verifyemail": { - "type": "boolean", - "default": true, - "description": "When set to false, the email format and DNS MX record are not checked." - } + "verifyemail": { "type": "boolean", "default": true, "description": "When set to false, the email format and DNS MX record are not checked." }, + "emailDelaySeconds": { "type": "integer", "default": 300, "description": "Time to wait before sending a device connection/disconnection notification email. If many events occur, they will be merged into a single email."} }, "required": [ "from" ] }, @@ -1108,7 +1106,8 @@ "properties": { "newline": { "type": "string", "default": "unix", "description": "Possible values are unix or windows" }, "path": { "type": "string", "default": "sendmail", "description": "Path to the sendmail command" }, - "args": { "type": "array", "items": { "type": "string" }, "default": null, "description": "Array or arguments to pass to sendmail" } + "args": { "type": "array", "items": { "type": "string" }, "default": null, "description": "Array or arguments to pass to sendmail" }, + "emailDelaySeconds": { "type": "integer", "default": 300, "description": "Time to wait before sending a device connection/disconnection notification email. If many events occur, they will be merged into a single email."} } }, "authStrategies": { diff --git a/meshmail.js b/meshmail.js index 29368550..65f4b467 100644 --- a/meshmail.js +++ b/meshmail.js @@ -26,6 +26,7 @@ module.exports.CreateMeshMail = function (parent, domain) { obj.mailCookieEncryptionKey = null; obj.verifyemail = false; obj.domain = domain; + obj.emailDelay = 5 * 60 * 1000; // Default of 5 minute email delay. //obj.mailTemplates = {}; const sortCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }) const constants = (obj.parent.crypto.constants ? obj.parent.crypto.constants : require('constants')); // require('constants') is deprecated in Node 11.10, use require('crypto').constants instead. @@ -41,8 +42,10 @@ module.exports.CreateMeshMail = function (parent, domain) { obj.sendGridServer = require('@sendgrid/mail'); obj.sendGridServer.setApiKey(obj.config.sendgrid.apikey); if (obj.config.sendgrid.verifyemail == true) { obj.verifyemail = true; } + if ((typeof obj.config.sendgrid.emaildelayseconds == 'number') && (obj.config.sendgrid.emaildelayseconds > 0)) { obj.emailDelay = obj.config.sendgrid.emaildelayseconds * 1000; } } else if (obj.config.smtp != null) { // Setup SMTP mail server + if ((typeof obj.config.smtp.emaildelayseconds == 'number') && (obj.config.smtp.emaildelayseconds > 0)) { obj.emailDelay = obj.config.smtp.emaildelayseconds * 1000; } if (obj.config.smtp.name == 'console') { // This is for debugging, the mails will be displayed on the console obj.smtpServer = 'console'; @@ -70,6 +73,7 @@ module.exports.CreateMeshMail = function (parent, domain) { } } else if (obj.config.sendmail != null) { // Setup Sendmail + if ((typeof obj.config.sendmail.emaildelayseconds == 'number') && (obj.config.sendmail.emaildelayseconds > 0)) { obj.emailDelay = obj.config.sendmail.emaildelayseconds * 1000; } const nodemailer = require('nodemailer'); var options = { sendmail: true }; if (typeof obj.config.sendmail.newline == 'string') { options.newline = obj.config.sendmail.newline; } @@ -78,6 +82,8 @@ module.exports.CreateMeshMail = function (parent, domain) { obj.smtpServer = nodemailer.createTransport(options); } + console.log('obj.emailDelay', obj.emailDelay); + // Get the correct mail template object function getTemplate(name, domain, lang) { parent.debug('email', 'Getting mail template for: ' + name + ', lang: ' + lang); @@ -584,7 +590,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); }, 5 * 60 * 1000); // 5 minute before email is sent + obj.deviceNotifications[user._id].timer = setTimeout(function () { sendDeviceNotifications(user._id); }, obj.emailDelay); } // Add the device @@ -648,7 +654,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); }, 5 * 60 * 1000); // 5 minute before email is sent + obj.deviceNotifications[user._id].timer = setTimeout(function () { sendDeviceNotifications(user._id); }, obj.emailDelay); } // Add the device