2023-12-05 08:12:10 +01:00
|
|
|
import * as nodemailer from "nodemailer";
|
2023-01-13 05:40:33 +01:00
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
2023-10-30 09:58:37 +01:00
|
|
|
import Logger from "@/services/logger.js";
|
2023-12-05 08:12:10 +01:00
|
|
|
import config from "@/config/index.js";
|
2024-02-21 14:23:24 +01:00
|
|
|
import { inspect } from "node:util";
|
2019-05-23 16:46:10 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export const logger = new Logger("email");
|
2019-05-23 16:46:10 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export async function sendEmail(
|
|
|
|
to: string,
|
|
|
|
subject: string,
|
|
|
|
html: string,
|
|
|
|
text: string,
|
|
|
|
) {
|
2019-05-23 16:46:10 +02:00
|
|
|
const meta = await fetchMeta(true);
|
|
|
|
|
2021-03-06 05:23:59 +01:00
|
|
|
const iconUrl = `${config.url}/static-assets/mi-white.png`;
|
2021-02-11 09:47:30 +01:00
|
|
|
const emailSettingUrl = `${config.url}/settings/email`;
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const enableAuth = meta.smtpUser != null && meta.smtpUser !== "";
|
2019-05-23 16:46:10 +02:00
|
|
|
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
|
|
host: meta.smtpHost,
|
|
|
|
port: meta.smtpPort,
|
|
|
|
secure: meta.smtpSecure,
|
|
|
|
ignoreTLS: !enableAuth,
|
2019-09-01 21:42:52 +02:00
|
|
|
proxy: config.proxySmtp,
|
2023-01-13 05:40:33 +01:00
|
|
|
auth: enableAuth
|
|
|
|
? {
|
|
|
|
user: meta.smtpUser,
|
|
|
|
pass: meta.smtpPass,
|
2024-03-28 06:25:33 +01:00
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
: undefined,
|
2019-05-23 16:46:10 +02:00
|
|
|
} as any);
|
|
|
|
|
|
|
|
try {
|
2023-04-27 05:04:58 +02:00
|
|
|
const info = await transporter.sendMail({
|
|
|
|
from: meta.email!,
|
|
|
|
to: to,
|
|
|
|
subject: subject,
|
|
|
|
text: text,
|
|
|
|
html: `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>${subject}</title>
|
2021-11-12 13:11:21 +01:00
|
|
|
</head>
|
2023-04-27 05:21:30 +02:00
|
|
|
<body style="background: #191724; padding: 16px; margin: 0; font-family: sans-serif; font-size: 14px;">
|
2023-05-10 23:29:43 +02:00
|
|
|
<main style="max-width: 500px; margin: 0 auto; background: #1f1d2e; color: #e0def4; border-radius: 20px;">
|
|
|
|
<header style="padding: 32px; background: #31748f; color: #e0def4; display: flex; border-radius: 20px;">
|
2023-04-27 05:21:30 +02:00
|
|
|
<img src="${meta.logoImageUrl || meta.iconUrl || iconUrl}" style="max-width: 128px; max-height: 72px; vertical-align: bottom; margin-right: 16px;"/>
|
|
|
|
<h1 style="margin: 0 0 1em 0;">${meta.name}</h1>
|
2021-11-12 13:11:21 +01:00
|
|
|
</header>
|
2023-04-27 05:21:30 +02:00
|
|
|
<article style="padding: 32px;">
|
2023-05-10 23:27:46 +02:00
|
|
|
<h1 style="color: #ebbcba !important;">${subject}</h1>
|
|
|
|
<div style="color: #e0def4;">${html}</div>
|
2021-11-12 13:11:21 +01:00
|
|
|
</article>
|
2023-04-27 05:21:30 +02:00
|
|
|
<footer style="padding: 32px; border-top: solid 1px #26233a;">
|
2023-05-10 23:27:46 +02:00
|
|
|
<a href="${emailSettingUrl}" style="color: #9ccfd8 !important;">${"Email Settings"}</a>
|
2021-11-12 13:11:21 +01:00
|
|
|
</footer>
|
|
|
|
</main>
|
2023-04-27 05:21:30 +02:00
|
|
|
<nav style="box-sizing: border-box; max-width: 500px; margin: 16px auto 0 auto; padding: 0 32px;">
|
2023-05-10 23:27:46 +02:00
|
|
|
<a href="${config.url}" style="color: #9ccfd8 !important;">${config.host}</a>
|
2021-11-12 13:11:21 +01:00
|
|
|
</nav>
|
|
|
|
</body>
|
|
|
|
</html>`,
|
2019-05-23 16:46:10 +02:00
|
|
|
});
|
|
|
|
|
2022-02-02 18:41:22 +01:00
|
|
|
logger.info(`Message sent: ${info.messageId}`);
|
|
|
|
} catch (err) {
|
2024-02-21 14:23:24 +01:00
|
|
|
logger.error(inspect(err));
|
2022-02-02 18:41:22 +01:00
|
|
|
throw err;
|
2019-05-23 16:46:10 +02:00
|
|
|
}
|
|
|
|
}
|