2021-09-07 10:42:46 -04:00
|
|
|
const nodemailer = require("nodemailer");
|
|
|
|
const NotificationProvider = require("./notification-provider");
|
2022-04-16 13:39:49 -04:00
|
|
|
const { DOWN } = require("../../src/util");
|
2023-10-16 10:16:49 -04:00
|
|
|
const { Liquid } = require("liquidjs");
|
2021-09-07 10:42:46 -04:00
|
|
|
|
|
|
|
class SMTP extends NotificationProvider {
|
|
|
|
|
|
|
|
name = "smtp";
|
|
|
|
|
2023-08-11 03:46:41 -04:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-09-07 10:42:46 -04:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
host: notification.smtpHost,
|
|
|
|
port: notification.smtpPort,
|
|
|
|
secure: notification.smtpSecure,
|
2021-10-27 23:10:09 -04:00
|
|
|
tls: {
|
2023-07-23 02:45:05 -04:00
|
|
|
rejectUnauthorized: !notification.smtpIgnoreTLSError || false,
|
2022-01-06 01:34:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fix #1129
|
|
|
|
if (notification.smtpDkimDomain) {
|
|
|
|
config.dkim = {
|
2021-12-19 00:30:53 -05:00
|
|
|
domainName: notification.smtpDkimDomain,
|
|
|
|
keySelector: notification.smtpDkimKeySelector,
|
|
|
|
privateKey: notification.smtpDkimPrivateKey,
|
|
|
|
hashAlgo: notification.smtpDkimHashAlgo,
|
|
|
|
headerFieldNames: notification.smtpDkimheaderFieldNames,
|
|
|
|
skipFields: notification.smtpDkimskipFields,
|
2022-01-06 01:34:45 -05:00
|
|
|
};
|
|
|
|
}
|
2021-09-07 10:42:46 -04:00
|
|
|
|
|
|
|
// 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 15:48:28 -04:00
|
|
|
|
2023-10-16 10:16:49 -04:00
|
|
|
// 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
|
2021-10-14 04:07:25 -04:00
|
|
|
if ((monitorJSON && heartbeatJSON) || msg.endsWith("Testing")) {
|
2023-10-16 10:16:49 -04:00
|
|
|
// cannot end with whitespace as this often raises spam scores
|
|
|
|
const customSubject = notification.customSubject?.trim() || "";
|
|
|
|
const customBody = notification.customBody?.trim() || "";
|
2021-10-09 15:48:28 -04:00
|
|
|
|
2023-10-16 10:16:49 -04:00
|
|
|
const context = this.generateContext(msg, monitorJSON, heartbeatJSON);
|
|
|
|
const engine = new Liquid();
|
2021-10-14 04:07:25 -04:00
|
|
|
if (customSubject !== "") {
|
2023-10-16 10:16:49 -04:00
|
|
|
const tpl = engine.parse(customSubject);
|
|
|
|
subject = await engine.render(tpl, context);
|
|
|
|
}
|
|
|
|
if (customBody !== "") {
|
|
|
|
const tpl = engine.parse(customBody);
|
|
|
|
body = await engine.render(tpl, context);
|
2021-10-14 04:07:25 -04:00
|
|
|
}
|
2021-09-07 10:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// send mail with defined transport object
|
2023-10-16 10:16:49 -04:00
|
|
|
let transporter = nodemailer.createTransport(config);
|
2021-09-07 10:42:46 -04:00
|
|
|
await transporter.sendMail({
|
2021-09-08 13:13:09 -04:00
|
|
|
from: notification.smtpFrom,
|
|
|
|
cc: notification.smtpCC,
|
|
|
|
bcc: notification.smtpBCC,
|
2021-09-07 10:42:46 -04:00
|
|
|
to: notification.smtpTo,
|
2021-10-09 14:32:45 -04:00
|
|
|
subject: subject,
|
2023-10-16 10:16:49 -04:00
|
|
|
text: body,
|
2021-09-07 10:42:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return "Sent Successfully.";
|
|
|
|
}
|
2023-10-16 10:16:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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"];
|
|
|
|
|
|
|
|
if (monitorJSON["type"] === "http" || monitorJSON["type"] === "keyword" || monitorJSON["type"] === "json-query") {
|
|
|
|
monitorHostnameOrURL = monitorJSON["url"];
|
|
|
|
} else {
|
|
|
|
monitorHostnameOrURL = monitorJSON["hostname"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
2021-09-07 10:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SMTP;
|