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