2021-09-07 16:42:46 +02:00
|
|
|
const nodemailer = require("nodemailer");
|
|
|
|
const NotificationProvider = require("./notification-provider");
|
2021-10-12 23:24:34 +02:00
|
|
|
const { DOWN, UP } = require("../../src/util");
|
2021-09-07 16:42:46 +02:00
|
|
|
|
|
|
|
class SMTP extends NotificationProvider {
|
|
|
|
|
|
|
|
name = "smtp";
|
|
|
|
|
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
host: notification.smtpHost,
|
|
|
|
port: notification.smtpPort,
|
|
|
|
secure: notification.smtpSecure,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
};
|
|
|
|
}
|
2021-10-09 20:32:45 +02:00
|
|
|
// Lets start with default subject
|
|
|
|
let subject = msg;
|
2021-10-09 21:48:28 +02:00
|
|
|
|
2021-10-09 20:32:45 +02:00
|
|
|
// Our subject cannot end with whitespace it's often raise spam score
|
|
|
|
let customsubject = notification.customsubject.trim()
|
2021-10-09 21:48:28 +02:00
|
|
|
|
2021-10-09 20:32:45 +02:00
|
|
|
// If custom subject is not empty, change subject for notification
|
|
|
|
if (customsubject !== "") {
|
2021-10-12 23:24:34 +02:00
|
|
|
|
|
|
|
// Replace "MACROS" with coresponding variable
|
2021-10-09 21:48:28 +02:00
|
|
|
let replaceName = new RegExp("{NAME}", "g");
|
|
|
|
let replaceHostname = new RegExp("{HOSTNAME}", "g");
|
|
|
|
let replaceStatus = new RegExp("{STATUS}", "g");
|
|
|
|
|
2021-10-12 23:24:34 +02:00
|
|
|
// Lets start with dummy values to simplify code
|
|
|
|
let monitorName = "Test"
|
|
|
|
let monitorHostname = "example.com"
|
|
|
|
let serviceStatus = "⚠️ Test";
|
2021-10-09 21:48:28 +02:00
|
|
|
|
|
|
|
if (monitorJSON !== null) {
|
2021-10-12 23:24:34 +02:00
|
|
|
monitorName = monitorJSON["name"];
|
|
|
|
monitorHostname = monitorJSON["hostname"];
|
2021-10-09 21:48:28 +02:00
|
|
|
}
|
2021-10-12 23:24:34 +02:00
|
|
|
|
2021-10-09 21:48:28 +02:00
|
|
|
if (heartbeatJSON !== null) {
|
2021-10-13 07:32:09 +02:00
|
|
|
serviceStatus = heartbeatJSON["status"] == DOWN ? "🔴 Down" : "✅ Up";
|
2021-10-09 21:48:28 +02:00
|
|
|
}
|
2021-10-13 07:32:09 +02:00
|
|
|
|
2021-10-12 23:24:34 +02:00
|
|
|
// Break replace to one by line for better readability
|
2021-10-13 07:32:09 +02:00
|
|
|
customsubject = customsubject.replace(replaceStatus, serviceStatus);
|
|
|
|
customsubject = customsubject.replace(replaceName, monitorName);
|
|
|
|
customsubject = customsubject.replace(replaceHostname, monitorHostname);
|
2021-10-09 21:48:28 +02:00
|
|
|
|
2021-10-09 20:32:45 +02:00
|
|
|
subject = customsubject
|
|
|
|
}
|
2021-09-07 16:42:46 +02:00
|
|
|
|
|
|
|
let transporter = nodemailer.createTransport(config);
|
|
|
|
|
|
|
|
let bodyTextContent = msg;
|
|
|
|
if (heartbeatJSON) {
|
|
|
|
bodyTextContent = `${msg}\nTime (UTC): ${heartbeatJSON["time"]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send mail with defined transport object
|
|
|
|
await transporter.sendMail({
|
2021-09-08 19:13:09 +02:00
|
|
|
from: notification.smtpFrom,
|
|
|
|
cc: notification.smtpCC,
|
|
|
|
bcc: notification.smtpBCC,
|
2021-09-07 16:42:46 +02:00
|
|
|
to: notification.smtpTo,
|
2021-10-09 20:32:45 +02:00
|
|
|
subject: subject,
|
2021-09-07 16:42:46 +02:00
|
|
|
text: bodyTextContent,
|
2021-09-08 19:13:09 +02:00
|
|
|
tls: {
|
|
|
|
rejectUnauthorized: notification.smtpIgnoreTLSError || false,
|
|
|
|
},
|
2021-09-07 16:42:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return "Sent Successfully.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SMTP;
|