tianji/src/server/model/notification/provider/smtp.ts
2024-01-07 00:11:14 +08:00

52 lines
1.3 KiB
TypeScript

import { NotificationProvider } from './type';
import nodemailer from 'nodemailer';
import SMTPTransport from 'nodemailer/lib/smtp-transport';
import { htmlContentTokenizer } from '../token';
interface SMTPPayload {
hostname: string;
port: number;
security: boolean;
ignoreTLS: boolean;
username: string;
password: string;
from: string;
to: string;
cc?: string;
bcc?: string;
}
// Fork from https://github.com/louislam/uptime-kuma/blob/HEAD/server/notification-providers/smtp.js
export const smtp: NotificationProvider = {
send: async (notification, title, message) => {
const payload = notification.payload as unknown as SMTPPayload;
const config: SMTPTransport.Options = {
host: payload.hostname,
port: payload.port,
secure: payload.security,
ignoreTLS: payload.ignoreTLS,
};
if (payload.username || payload.password) {
config.auth = {
user: payload.username,
pass: payload.password,
};
}
const subject = title;
const bodyTextContent = htmlContentTokenizer.parse(message);
const transporter = nodemailer.createTransport(config);
await transporter.sendMail({
from: payload.from,
to: payload.to,
cc: payload.cc,
bcc: payload.bcc,
subject: subject,
html: bodyTextContent,
});
},
};