uptime-kuma/server/notification-providers/smtp.js

78 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2021-09-07 14:42:46 +00:00
const nodemailer = require("nodemailer");
const NotificationProvider = require("./notification-provider");
class SMTP extends NotificationProvider {
name = "smtp";
/**
* @inheritdoc
*/
2021-09-07 14:42:46 +00:00
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";
2021-09-07 14:42:46 +00:00
const config = {
host: notification.smtpHost,
port: notification.smtpPort,
secure: notification.smtpSecure,
tls: {
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,
};
}
// 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() || "";
if (customSubject !== "") {
subject = await this.renderTemplate(customSubject, msg, monitorJSON, heartbeatJSON);
}
if (customBody !== "") {
body = await this.renderTemplate(customBody, msg, monitorJSON, heartbeatJSON);
}
2021-09-07 14:42:46 +00:00
}
// send mail with defined transport object
let transporter = nodemailer.createTransport(config);
2021-09-07 14:42:46 +00:00
await transporter.sendMail({
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,
text: body,
2021-09-07 14:42:46 +00:00
});
return okMsg;
2021-09-07 14:42:46 +00:00
}
}
module.exports = SMTP;