From 5e3ea3293c5d1f37ba65ab94182075b788608b45 Mon Sep 17 00:00:00 2001 From: Lukas <35193662+NixNotCastey@users.noreply.github.com> Date: Sat, 9 Oct 2021 20:32:45 +0200 Subject: [PATCH 1/6] Very basic email subject customization --- server/notification-providers/smtp.js | 10 +++++++++- src/components/notifications/SMTP.vue | 5 +++++ src/languages/en.js | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index ecb583eb..e5fd53e4 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -20,6 +20,14 @@ class SMTP extends NotificationProvider { pass: notification.smtpPassword, }; } + // Lets start with default subject + let subject = msg; + // Our subject cannot end with whitespace it's often raise spam score + let customsubject = notification.customsubject.trim() + // If custom subject is not empty, change subject for notification + if (customsubject !== "") { + subject = customsubject + } let transporter = nodemailer.createTransport(config); @@ -34,7 +42,7 @@ class SMTP extends NotificationProvider { cc: notification.smtpCC, bcc: notification.smtpBCC, to: notification.smtpTo, - subject: msg, + subject: subject, text: bodyTextContent, tls: { rejectUnauthorized: notification.smtpIgnoreTLSError || false, diff --git a/src/components/notifications/SMTP.vue b/src/components/notifications/SMTP.vue index 72934cda..165d39c6 100644 --- a/src/components/notifications/SMTP.vue +++ b/src/components/notifications/SMTP.vue @@ -43,6 +43,11 @@ +
+ + +
+
diff --git a/src/languages/en.js b/src/languages/en.js index 2ce8f46b..b59cdb73 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -201,6 +201,7 @@ export default { secureOptionTLS: "TLS (465)", "Ignore TLS Error": "Ignore TLS Error", "From Email": "From Email", + "Custom Email subject": "Custom Email Subject (leave blank for default one)", "To Email": "To Email", smtpCC: "CC", smtpBCC: "BCC", From 792f3c7c5c17a2db80e7f9638e272b4607f3d4c6 Mon Sep 17 00:00:00 2001 From: Lukas <35193662+NixNotCastey@users.noreply.github.com> Date: Sat, 9 Oct 2021 21:48:28 +0200 Subject: [PATCH 2/6] Add support for values of Name, Hostname and Status --- server/notification-providers/smtp.js | 30 +++++++++++++++++++++++++++ src/components/notifications/SMTP.vue | 4 ++-- src/languages/en.js | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index e5fd53e4..2bbec584 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -22,10 +22,40 @@ class SMTP extends NotificationProvider { } // Lets start with default subject let subject = msg; + // Our subject cannot end with whitespace it's often raise spam score let customsubject = notification.customsubject.trim() + // If custom subject is not empty, change subject for notification if (customsubject !== "") { + + // Replace "MACROS" with coresponding variable + let replaceName = new RegExp("{NAME}", "g"); + let replaceHostname = new RegExp("{HOSTNAME}", "g"); + let replaceStatus = new RegExp("{STATUS}", "g"); + + let serviceStatus; + + if (monitorJSON !== null) { + customsubject = customsubject.replace(replaceName,monitorJSON["name"]); + customsubject = customsubject.replace(replaceHostname,monitorJSON["hostname"]); + } else { + // Insert dummy values during test + customsubject = customsubject.replace(replaceName,"Test"); + customsubject = customsubject.replace(replaceHostname,"example.com"); + } + if (heartbeatJSON !== null) { + if (heartbeatJSON["status"] === 0) { + serviceStatus = "🔴 Down" + } else { + serviceStatus = "✅ Up" + } + customsubject = customsubject.replace(replaceStatus,serviceStatus); + } else { + // Insert dummy values during test + customsubject = customsubject.replace(replaceStatus,"TEST"); + } + subject = customsubject } diff --git a/src/components/notifications/SMTP.vue b/src/components/notifications/SMTP.vue index 165d39c6..01bdf860 100644 --- a/src/components/notifications/SMTP.vue +++ b/src/components/notifications/SMTP.vue @@ -44,8 +44,8 @@
- - + +
diff --git a/src/languages/en.js b/src/languages/en.js index b59cdb73..0e8e9230 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -201,7 +201,7 @@ export default { secureOptionTLS: "TLS (465)", "Ignore TLS Error": "Ignore TLS Error", "From Email": "From Email", - "Custom Email subject": "Custom Email Subject (leave blank for default one)", + "Email Subject": "Subject (leave blank for default one)", "To Email": "To Email", smtpCC: "CC", smtpBCC: "BCC", From 30d8aadf12af55198679960685130321507af865 Mon Sep 17 00:00:00 2001 From: Lukas <35193662+NixNotCastey@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:24:34 +0200 Subject: [PATCH 3/6] Slightly refactor --- server/notification-providers/smtp.js | 33 ++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index 2bbec584..7def0694 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -1,5 +1,6 @@ const nodemailer = require("nodemailer"); const NotificationProvider = require("./notification-provider"); +const { DOWN, UP } = require("../../src/util"); class SMTP extends NotificationProvider { @@ -28,33 +29,29 @@ class SMTP extends NotificationProvider { // If custom subject is not empty, change subject for notification if (customsubject !== "") { - - // Replace "MACROS" with coresponding variable + + // Replace "MACROS" with coresponding variable let replaceName = new RegExp("{NAME}", "g"); let replaceHostname = new RegExp("{HOSTNAME}", "g"); let replaceStatus = new RegExp("{STATUS}", "g"); - let serviceStatus; + // Lets start with dummy values to simplify code + let monitorName = "Test" + let monitorHostname = "example.com" + let serviceStatus = "⚠️ Test"; if (monitorJSON !== null) { - customsubject = customsubject.replace(replaceName,monitorJSON["name"]); - customsubject = customsubject.replace(replaceHostname,monitorJSON["hostname"]); - } else { - // Insert dummy values during test - customsubject = customsubject.replace(replaceName,"Test"); - customsubject = customsubject.replace(replaceHostname,"example.com"); + monitorName = monitorJSON["name"]; + monitorHostname = monitorJSON["hostname"]; } + if (heartbeatJSON !== null) { - if (heartbeatJSON["status"] === 0) { - serviceStatus = "🔴 Down" - } else { - serviceStatus = "✅ Up" - } - customsubject = customsubject.replace(replaceStatus,serviceStatus); - } else { - // Insert dummy values during test - customsubject = customsubject.replace(replaceStatus,"TEST"); + serviceStatus = heartbeatJSON["status"] == DOWN ? "🔴 Down":"✅ Up"; } + // Break replace to one by line for better readability + customsubject = customsubject.replace(replaceStatus,serviceStatus); + customsubject = customsubject.replace(replaceName,monitorName); + customsubject = customsubject.replace(replaceHostname,monitorHostname); subject = customsubject } From 330cd6e058b77cda9444482d184474af48a39424 Mon Sep 17 00:00:00 2001 From: Lukas <35193662+NixNotCastey@users.noreply.github.com> Date: Wed, 13 Oct 2021 07:32:09 +0200 Subject: [PATCH 4/6] Minor rehabilitanty impedyment Co-authored-by: Adam Stachowicz --- server/notification-providers/smtp.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index 7def0694..dd1cd10a 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -46,12 +46,13 @@ class SMTP extends NotificationProvider { } if (heartbeatJSON !== null) { - serviceStatus = heartbeatJSON["status"] == DOWN ? "🔴 Down":"✅ Up"; + serviceStatus = heartbeatJSON["status"] == DOWN ? "🔴 Down" : "✅ Up"; } + // Break replace to one by line for better readability - customsubject = customsubject.replace(replaceStatus,serviceStatus); - customsubject = customsubject.replace(replaceName,monitorName); - customsubject = customsubject.replace(replaceHostname,monitorHostname); + customsubject = customsubject.replace(replaceStatus, serviceStatus); + customsubject = customsubject.replace(replaceName, monitorName); + customsubject = customsubject.replace(replaceHostname, monitorHostname); subject = customsubject } From 89b34b57484d5dfba7913b9bf2c56c1ec5c80763 Mon Sep 17 00:00:00 2001 From: Lukas <35193662+NixNotCastey@users.noreply.github.com> Date: Wed, 13 Oct 2021 18:05:18 +0200 Subject: [PATCH 5/6] Use double curly brackets and sanity check for customSubject --- server/notification-providers/smtp.js | 24 ++++++++++++++---------- src/components/notifications/SMTP.vue | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index dd1cd10a..a74b48cc 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -21,19 +21,23 @@ class SMTP extends NotificationProvider { pass: notification.smtpPassword, }; } - // Lets start with default subject + // Lets start with default subject and empty string for custom one let subject = msg; + let customSubject = ""; // Our subject cannot end with whitespace it's often raise spam score - let customsubject = notification.customsubject.trim() + // Once I got "Cannot read property 'trim' of undefined", better be safe than sorry + if (notification.customSubject) { + customSubject = notification.customSubject.trim() + } // If custom subject is not empty, change subject for notification - if (customsubject !== "") { + if (customSubject !== "") { // Replace "MACROS" with coresponding variable - let replaceName = new RegExp("{NAME}", "g"); - let replaceHostname = new RegExp("{HOSTNAME}", "g"); - let replaceStatus = new RegExp("{STATUS}", "g"); + let replaceName = new RegExp("{{NAME}}", "g"); + let replaceHostname = new RegExp("{{HOSTNAME}}", "g"); + let replaceStatus = new RegExp("{{STATUS}}", "g"); // Lets start with dummy values to simplify code let monitorName = "Test" @@ -50,11 +54,11 @@ class SMTP extends NotificationProvider { } // Break replace to one by line for better readability - customsubject = customsubject.replace(replaceStatus, serviceStatus); - customsubject = customsubject.replace(replaceName, monitorName); - customsubject = customsubject.replace(replaceHostname, monitorHostname); + customSubject = customSubject.replace(replaceStatus, serviceStatus); + customSubject = customSubject.replace(replaceName, monitorName); + customSubject = customSubject.replace(replaceHostname, monitorHostname); - subject = customsubject + subject = customSubject } let transporter = nodemailer.createTransport(config); diff --git a/src/components/notifications/SMTP.vue b/src/components/notifications/SMTP.vue index 01bdf860..79efd9f9 100644 --- a/src/components/notifications/SMTP.vue +++ b/src/components/notifications/SMTP.vue @@ -45,7 +45,7 @@
- +
From 83388819273e49e8773646a90cb6de3f0bfebd6d Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Thu, 14 Oct 2021 16:07:25 +0800 Subject: [PATCH 6/6] [SMTP] change {{HOSTNAME}} to {{HOSTNAME_OR_URL}}, support for http montior type, some UI improvements --- server/notification-providers/smtp.js | 69 ++++++++++++++++----------- src/components/notifications/SMTP.vue | 17 +++++-- src/languages/en.js | 2 +- src/pages/EditMonitor.vue | 1 + 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index a74b48cc..60068eb7 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -23,42 +23,53 @@ class SMTP extends NotificationProvider { } // Lets start with default subject and empty string for custom one let subject = msg; - let customSubject = ""; - // Our subject cannot end with whitespace it's often raise spam score - // Once I got "Cannot read property 'trim' of undefined", better be safe than sorry - if (notification.customSubject) { - customSubject = notification.customSubject.trim() - } + // Change the subject if: + // - The msg ends with "Testing" or + // - Actual Up/Down Notification + if ((monitorJSON && heartbeatJSON) || msg.endsWith("Testing")) { + let customSubject = ""; - // If custom subject is not empty, change subject for notification - if (customSubject !== "") { + // Our subject cannot end with whitespace it's often raise spam score + // Once I got "Cannot read property 'trim' of undefined", better be safe than sorry + if (notification.customSubject) { + customSubject = notification.customSubject.trim(); + } - // Replace "MACROS" with coresponding variable - let replaceName = new RegExp("{{NAME}}", "g"); - let replaceHostname = new RegExp("{{HOSTNAME}}", "g"); - let replaceStatus = new RegExp("{{STATUS}}", "g"); + // If custom subject is not empty, change subject for notification + if (customSubject !== "") { - // Lets start with dummy values to simplify code - let monitorName = "Test" - let monitorHostname = "example.com" - let serviceStatus = "⚠️ Test"; + // Replace "MACROS" with corresponding variable + let replaceName = new RegExp("{{NAME}}", "g"); + let replaceHostnameOrURL = new RegExp("{{HOSTNAME_OR_URL}}", "g"); + let replaceStatus = new RegExp("{{STATUS}}", "g"); - if (monitorJSON !== null) { - monitorName = monitorJSON["name"]; - monitorHostname = monitorJSON["hostname"]; - } + // Lets start with dummy values to simplify code + let monitorName = "Test"; + let monitorHostnameOrURL = "testing.hostname"; + let serviceStatus = "⚠️ Test"; - if (heartbeatJSON !== null) { - serviceStatus = heartbeatJSON["status"] == DOWN ? "🔴 Down" : "✅ Up"; - } - - // Break replace to one by line for better readability - customSubject = customSubject.replace(replaceStatus, serviceStatus); - customSubject = customSubject.replace(replaceName, monitorName); - customSubject = customSubject.replace(replaceHostname, monitorHostname); + if (monitorJSON !== null) { + monitorName = monitorJSON["name"]; - subject = customSubject + if (monitorJSON["type"] === "http" || monitorJSON["type"] === "keyword") { + monitorHostnameOrURL = monitorJSON["url"]; + } else { + monitorHostnameOrURL = monitorJSON["hostname"]; + } + } + + if (heartbeatJSON !== null) { + serviceStatus = (heartbeatJSON["status"] === DOWN) ? "🔴 Down" : "✅ Up"; + } + + // Break replace to one by line for better readability + customSubject = customSubject.replace(replaceStatus, serviceStatus); + customSubject = customSubject.replace(replaceName, monitorName); + customSubject = customSubject.replace(replaceHostnameOrURL, monitorHostnameOrURL); + + subject = customSubject; + } } let transporter = nodemailer.createTransport(config); diff --git a/src/components/notifications/SMTP.vue b/src/components/notifications/SMTP.vue index 79efd9f9..483917e3 100644 --- a/src/components/notifications/SMTP.vue +++ b/src/components/notifications/SMTP.vue @@ -43,11 +43,6 @@
-
- - -
-
@@ -62,6 +57,18 @@
+ +
+ + +
+ (leave blank for default one)
+ {{NAME}}: Service Name
+ {{HOSTNAME_OR_URL}}: Hostname or URL
+ {{URL}}: URL
+ {{STATUS}}: Status
+
+