2023-01-13 05:40:33 +01:00
|
|
|
import * as nodemailer from "nodemailer";
|
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
|
|
|
import Logger from "./logger.js";
|
|
|
|
import config from "@/config/index.js";
|
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,
|
|
|
|
}
|
|
|
|
: undefined,
|
2019-05-23 16:46:10 +02:00
|
|
|
} as any);
|
|
|
|
|
|
|
|
try {
|
2021-02-11 09:49:28 +01:00
|
|
|
// TODO: htmlサニタイズ
|
2019-05-23 16:46:10 +02:00
|
|
|
const info = await transporter.sendMail({
|
|
|
|
from: meta.email!,
|
|
|
|
to: to,
|
2021-02-11 09:47:30 +01:00
|
|
|
subject: subject,
|
|
|
|
text: text,
|
|
|
|
html: `<!doctype html>
|
2021-11-12 13:11:21 +01:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
2023-01-13 05:40:33 +01:00
|
|
|
<title>${subject}</title>
|
2021-11-12 13:11:21 +01:00
|
|
|
<style>
|
|
|
|
html {
|
|
|
|
background: #eee;
|
|
|
|
}
|
2021-02-11 09:47:30 +01:00
|
|
|
|
2021-11-12 13:11:21 +01:00
|
|
|
body {
|
|
|
|
padding: 16px;
|
|
|
|
margin: 0;
|
|
|
|
font-family: sans-serif;
|
|
|
|
font-size: 14px;
|
|
|
|
}
|
2021-02-11 09:47:30 +01:00
|
|
|
|
2021-11-12 13:11:21 +01:00
|
|
|
a {
|
|
|
|
text-decoration: none;
|
2022-07-25 03:36:55 +02:00
|
|
|
color: #31748f;
|
2021-11-12 13:11:21 +01:00
|
|
|
}
|
|
|
|
a:hover {
|
|
|
|
text-decoration: underline;
|
|
|
|
}
|
2021-02-11 09:47:30 +01:00
|
|
|
|
2021-11-12 13:11:21 +01:00
|
|
|
main {
|
|
|
|
max-width: 500px;
|
|
|
|
margin: 0 auto;
|
2022-07-25 03:36:55 +02:00
|
|
|
background: #e0def4;
|
|
|
|
color: #6e6a86;
|
2021-11-12 13:11:21 +01:00
|
|
|
}
|
|
|
|
main > header {
|
|
|
|
padding: 32px;
|
2022-07-25 03:36:55 +02:00
|
|
|
background: #31748f;
|
2021-11-12 13:11:21 +01:00
|
|
|
}
|
|
|
|
main > header > img {
|
|
|
|
max-width: 128px;
|
|
|
|
max-height: 28px;
|
|
|
|
vertical-align: bottom;
|
|
|
|
}
|
|
|
|
main > article {
|
|
|
|
padding: 32px;
|
|
|
|
}
|
|
|
|
main > article > h1 {
|
|
|
|
margin: 0 0 1em 0;
|
|
|
|
}
|
|
|
|
main > footer {
|
|
|
|
padding: 32px;
|
|
|
|
border-top: solid 1px #eee;
|
|
|
|
}
|
2021-02-11 09:47:30 +01:00
|
|
|
|
2021-11-12 13:11:21 +01:00
|
|
|
nav {
|
|
|
|
box-sizing: border-box;
|
|
|
|
max-width: 500px;
|
|
|
|
margin: 16px auto 0 auto;
|
|
|
|
padding: 0 32px;
|
|
|
|
}
|
|
|
|
nav > a {
|
|
|
|
color: #888;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<main>
|
|
|
|
<header>
|
2023-01-13 05:40:33 +01:00
|
|
|
<img src="${meta.logoImageUrl || meta.iconUrl || iconUrl}"/>
|
2021-11-12 13:11:21 +01:00
|
|
|
</header>
|
|
|
|
<article>
|
2023-01-13 05:40:33 +01:00
|
|
|
<h1>${subject}</h1>
|
|
|
|
<div>${html}</div>
|
2021-11-12 13:11:21 +01:00
|
|
|
</article>
|
|
|
|
<footer>
|
2023-01-13 05:40:33 +01:00
|
|
|
<a href="${emailSettingUrl}">${"Email setting"}</a>
|
2021-11-12 13:11:21 +01:00
|
|
|
</footer>
|
|
|
|
</main>
|
|
|
|
<nav>
|
2023-01-13 05:40:33 +01:00
|
|
|
<a href="${config.url}">${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) {
|
|
|
|
logger.error(err as Error);
|
|
|
|
throw err;
|
2019-05-23 16:46:10 +02:00
|
|
|
}
|
|
|
|
}
|