2019-05-23 23:46:10 +09:00
|
|
|
import * as nodemailer from 'nodemailer';
|
2021-03-23 17:43:07 +09:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
2019-05-23 23:46:10 +09:00
|
|
|
import Logger from './logger';
|
2021-03-23 17:43:07 +09:00
|
|
|
import config from '@/config';
|
2019-05-23 23:46:10 +09:00
|
|
|
|
|
|
|
export const logger = new Logger('email');
|
|
|
|
|
2021-02-13 12:28:26 +09:00
|
|
|
export async function sendEmail(to: string, subject: string, html: string, text: string) {
|
2019-05-23 23:46:10 +09:00
|
|
|
const meta = await fetchMeta(true);
|
|
|
|
|
2021-03-06 13:23:59 +09:00
|
|
|
const iconUrl = `${config.url}/static-assets/mi-white.png`;
|
2021-02-11 17:47:30 +09:00
|
|
|
const emailSettingUrl = `${config.url}/settings/email`;
|
|
|
|
|
2019-05-23 23:46:10 +09:00
|
|
|
const enableAuth = meta.smtpUser != null && meta.smtpUser !== '';
|
|
|
|
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
|
|
host: meta.smtpHost,
|
|
|
|
port: meta.smtpPort,
|
|
|
|
secure: meta.smtpSecure,
|
|
|
|
ignoreTLS: !enableAuth,
|
2019-09-02 04:42:52 +09:00
|
|
|
proxy: config.proxySmtp,
|
2019-05-23 23:46:10 +09:00
|
|
|
auth: enableAuth ? {
|
|
|
|
user: meta.smtpUser,
|
|
|
|
pass: meta.smtpPass
|
|
|
|
} : undefined
|
|
|
|
} as any);
|
|
|
|
|
|
|
|
try {
|
2021-02-11 17:49:28 +09:00
|
|
|
// TODO: htmlサニタイズ
|
2019-05-23 23:46:10 +09:00
|
|
|
const info = await transporter.sendMail({
|
|
|
|
from: meta.email!,
|
|
|
|
to: to,
|
2021-02-11 17:47:30 +09:00
|
|
|
subject: subject,
|
|
|
|
text: text,
|
|
|
|
html: `<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>${ subject }</title>
|
|
|
|
<style>
|
|
|
|
html {
|
|
|
|
background: #eee;
|
|
|
|
}
|
|
|
|
|
|
|
|
body {
|
|
|
|
padding: 16px;
|
2021-02-13 12:28:26 +09:00
|
|
|
margin: 0;
|
|
|
|
font-family: sans-serif;
|
|
|
|
font-size: 14px;
|
2021-02-11 17:47:30 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
a {
|
|
|
|
text-decoration: none;
|
|
|
|
color: #86b300;
|
|
|
|
}
|
|
|
|
a:hover {
|
|
|
|
text-decoration: underline;
|
|
|
|
}
|
|
|
|
|
|
|
|
main {
|
|
|
|
max-width: 500px;
|
|
|
|
margin: 0 auto;
|
|
|
|
background: #fff;
|
|
|
|
color: #555;
|
|
|
|
}
|
|
|
|
main > header {
|
|
|
|
padding: 32px;
|
|
|
|
background: #86b300;
|
|
|
|
}
|
|
|
|
main > header > img {
|
|
|
|
max-width: 128px;
|
|
|
|
max-height: 28px;
|
2021-02-13 12:28:26 +09:00
|
|
|
vertical-align: bottom;
|
2021-02-11 17:47:30 +09:00
|
|
|
}
|
|
|
|
main > article {
|
|
|
|
padding: 32px;
|
|
|
|
}
|
|
|
|
main > article > h1 {
|
|
|
|
margin: 0 0 1em 0;
|
|
|
|
}
|
|
|
|
main > footer {
|
|
|
|
padding: 32px;
|
|
|
|
border-top: solid 1px #eee;
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
<img src="${ meta.logoImageUrl || meta.iconUrl || iconUrl }"/>
|
|
|
|
</header>
|
|
|
|
<article>
|
|
|
|
<h1>${ subject }</h1>
|
2021-02-13 12:28:26 +09:00
|
|
|
<div>${ html }</div>
|
2021-02-11 17:47:30 +09:00
|
|
|
</article>
|
|
|
|
<footer>
|
|
|
|
<a href="${ emailSettingUrl }">${ 'Email setting' }</a>
|
|
|
|
</footer>
|
|
|
|
</main>
|
|
|
|
<nav>
|
|
|
|
<a href="${ config.url }">${ config.host }</a>
|
|
|
|
</nav>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
2019-05-23 23:46:10 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
logger.info('Message sent: %s', info.messageId);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|