summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/smtp.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-11-26 09:28:28 +0100
committerDaniel Baumann <daniel@debian.org>2024-11-26 12:25:58 +0100
commita1882b67c41fe9901a0cd8059b5cc78a5beadec0 (patch)
tree2a24507c67aa99a15416707b2f7e645142230ed8 /server/notification-providers/smtp.js
parentInitial commit. (diff)
downloaduptime-kuma-upstream.tar.xz
uptime-kuma-upstream.zip
Adding upstream version 2.0.0~beta.0+dfsg.upstream/2.0.0_beta.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'server/notification-providers/smtp.js')
-rw-r--r--server/notification-providers/smtp.js120
1 files changed, 120 insertions, 0 deletions
diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js
new file mode 100644
index 0000000..9f3defa
--- /dev/null
+++ b/server/notification-providers/smtp.js
@@ -0,0 +1,120 @@
+const nodemailer = require("nodemailer");
+const NotificationProvider = require("./notification-provider");
+const { DOWN } = require("../../src/util");
+const { Liquid } = require("liquidjs");
+
+class SMTP extends NotificationProvider {
+ name = "smtp";
+
+ /**
+ * @inheritdoc
+ */
+ async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
+ const okMsg = "Sent Successfully.";
+
+ const config = {
+ host: notification.smtpHost,
+ port: notification.smtpPort,
+ secure: notification.smtpSecure,
+ tls: {
+ rejectUnauthorized: !notification.smtpIgnoreTLSError || false,
+ }
+ };
+
+ // Fix #1129
+ if (notification.smtpDkimDomain) {
+ config.dkim = {
+ domainName: notification.smtpDkimDomain,
+ keySelector: notification.smtpDkimKeySelector,
+ privateKey: notification.smtpDkimPrivateKey,
+ hashAlgo: notification.smtpDkimHashAlgo,
+ headerFieldNames: notification.smtpDkimheaderFieldNames,
+ skipFields: notification.smtpDkimskipFields,
+ };
+ }
+
+ // Should fix the issue in https://github.com/louislam/uptime-kuma/issues/26#issuecomment-896373904
+ if (notification.smtpUsername || notification.smtpPassword) {
+ config.auth = {
+ user: notification.smtpUsername,
+ pass: notification.smtpPassword,
+ };
+ }
+
+ // default values in case the user does not want to template
+ let subject = msg;
+ let body = msg;
+ if (heartbeatJSON) {
+ body = `${msg}\nTime (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`;
+ }
+ // subject and body are templated
+ if ((monitorJSON && heartbeatJSON) || msg.endsWith("Testing")) {
+ // cannot end with whitespace as this often raises spam scores
+ const customSubject = notification.customSubject?.trim() || "";
+ const customBody = notification.customBody?.trim() || "";
+
+ const context = this.generateContext(msg, monitorJSON, heartbeatJSON);
+ const engine = new Liquid();
+ if (customSubject !== "") {
+ const tpl = engine.parse(customSubject);
+ subject = await engine.render(tpl, context);
+ }
+ if (customBody !== "") {
+ const tpl = engine.parse(customBody);
+ body = await engine.render(tpl, context);
+ }
+ }
+
+ // send mail with defined transport object
+ let transporter = nodemailer.createTransport(config);
+ await transporter.sendMail({
+ from: notification.smtpFrom,
+ cc: notification.smtpCC,
+ bcc: notification.smtpBCC,
+ to: notification.smtpTo,
+ subject: subject,
+ text: body,
+ });
+
+ return okMsg;
+ }
+
+ /**
+ * Generate context for LiquidJS
+ * @param {string} msg the message that will be included in the context
+ * @param {?object} monitorJSON Monitor details (For Up/Down/Cert-Expiry only)
+ * @param {?object} heartbeatJSON Heartbeat details (For Up/Down only)
+ * @returns {{STATUS: string, status: string, HOSTNAME_OR_URL: string, hostnameOrUrl: string, NAME: string, name: string, monitorJSON: ?object, heartbeatJSON: ?object, msg: string}} the context
+ */
+ generateContext(msg, monitorJSON, heartbeatJSON) {
+ // Let's start with dummy values to simplify code
+ let monitorName = "Monitor Name not available";
+ let monitorHostnameOrURL = "testing.hostname";
+
+ if (monitorJSON !== null) {
+ monitorName = monitorJSON["name"];
+ monitorHostnameOrURL = this.extractAddress(monitorJSON);
+ }
+
+ let serviceStatus = "⚠️ Test";
+ if (heartbeatJSON !== null) {
+ serviceStatus = (heartbeatJSON["status"] === DOWN) ? "🔴 Down" : "✅ Up";
+ }
+ return {
+ // for v1 compatibility, to be removed in v3
+ "STATUS": serviceStatus,
+ "NAME": monitorName,
+ "HOSTNAME_OR_URL": monitorHostnameOrURL,
+
+ // variables which are officially supported
+ "status": serviceStatus,
+ "name": monitorName,
+ "hostnameOrURL": monitorHostnameOrURL,
+ monitorJSON,
+ heartbeatJSON,
+ msg,
+ };
+ }
+}
+
+module.exports = SMTP;