2021-09-07 14:42:46 +00:00
|
|
|
const nodemailer = require("nodemailer");
|
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
|
|
|
|
class SMTP extends NotificationProvider {
|
|
|
|
name = "smtp";
|
|
|
|
|
2023-08-11 07:46:41 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-09-07 14:42:46 +00:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 13:21:15 +00:00
|
|
|
const okMsg = "Sent Successfully.";
|
2021-09-07 14:42:46 +00:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
host: notification.smtpHost,
|
|
|
|
port: notification.smtpPort,
|
|
|
|
secure: notification.smtpSecure,
|
2021-10-28 03:10:09 +00:00
|
|
|
tls: {
|
2023-07-23 06:45:05 +00:00
|
|
|
rejectUnauthorized: !notification.smtpIgnoreTLSError || false,
|
2022-01-06 06:34:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fix #1129
|
|
|
|
if (notification.smtpDkimDomain) {
|
|
|
|
config.dkim = {
|
2021-12-19 05:30:53 +00:00
|
|
|
domainName: notification.smtpDkimDomain,
|
|
|
|
keySelector: notification.smtpDkimKeySelector,
|
|
|
|
privateKey: notification.smtpDkimPrivateKey,
|
|
|
|
hashAlgo: notification.smtpDkimHashAlgo,
|
|
|
|
headerFieldNames: notification.smtpDkimheaderFieldNames,
|
|
|
|
skipFields: notification.smtpDkimskipFields,
|
2022-01-06 06:34:45 +00:00
|
|
|
};
|
|
|
|
}
|
2021-09-07 14:42:46 +00: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 19:48:28 +00:00
|
|
|
|
2023-10-16 14:16:49 +00: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 08:07:25 +00:00
|
|
|
if ((monitorJSON && heartbeatJSON) || msg.endsWith("Testing")) {
|
2023-10-16 14:16:49 +00:00
|
|
|
// cannot end with whitespace as this often raises spam scores
|
|
|
|
const customSubject = notification.customSubject?.trim() || "";
|
|
|
|
const customBody = notification.customBody?.trim() || "";
|
2021-10-09 19:48:28 +00:00
|
|
|
|
2021-10-14 08:07:25 +00:00
|
|
|
if (customSubject !== "") {
|
2025-03-14 11:51:07 +00:00
|
|
|
subject = await this.renderTemplate(customSubject, msg, monitorJSON, heartbeatJSON);
|
2023-10-16 14:16:49 +00:00
|
|
|
}
|
|
|
|
if (customBody !== "") {
|
2025-03-14 11:51:07 +00:00
|
|
|
body = await this.renderTemplate(customBody, msg, monitorJSON, heartbeatJSON);
|
2021-10-14 08:07:25 +00:00
|
|
|
}
|
2021-09-07 14:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send mail with defined transport object
|
2023-10-16 14:16:49 +00:00
|
|
|
let transporter = nodemailer.createTransport(config);
|
2021-09-07 14:42:46 +00:00
|
|
|
await transporter.sendMail({
|
2021-09-08 17:13:09 +00:00
|
|
|
from: notification.smtpFrom,
|
|
|
|
cc: notification.smtpCC,
|
|
|
|
bcc: notification.smtpBCC,
|
2021-09-07 14:42:46 +00:00
|
|
|
to: notification.smtpTo,
|
2021-10-09 18:32:45 +00:00
|
|
|
subject: subject,
|
2023-10-16 14:16:49 +00:00
|
|
|
text: body,
|
2021-09-07 14:42:46 +00:00
|
|
|
});
|
|
|
|
|
2024-03-14 13:21:15 +00:00
|
|
|
return okMsg;
|
2021-09-07 14:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SMTP;
|