summaryrefslogtreecommitdiff
path: root/src/services/send-email.ts
blob: 4814808392b4d410427f7670b13bcec95a781e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as nodemailer from 'nodemailer';
import { fetchMeta } from '../misc/fetch-meta';
import Logger from './logger';
import config from '../config';

export const logger = new Logger('email');

export async function sendEmail(to: string, subject: string, html: string, text: string) {
	const meta = await fetchMeta(true);

	const iconUrl = `${config.url}/static-assets/mi-white.png`;
	const emailSettingUrl = `${config.url}/settings/email`;

	const enableAuth = meta.smtpUser != null && meta.smtpUser !== '';

	const transporter = nodemailer.createTransport({
		host: meta.smtpHost,
		port: meta.smtpPort,
		secure: meta.smtpSecure,
		ignoreTLS: !enableAuth,
		proxy: config.proxySmtp,
		auth: enableAuth ? {
			user: meta.smtpUser,
			pass: meta.smtpPass
		} : undefined
	} as any);

	try {
		// TODO: htmlサニタイズ
		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>
					<style>
						html {
							background: #eee;
						}

						body {
							padding: 16px;
							margin: 0;
							font-family: sans-serif;
							font-size: 14px;
						}

						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;
									vertical-align: bottom;
								}
							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>
							<div>${ html }</div>
						</article>
						<footer>
							<a href="${ emailSettingUrl }">${ 'Email setting' }</a>
						</footer>
					</main>
					<nav>
						<a href="${ config.url }">${ config.host }</a>
					</nav>
				</body>
			</html>
			`
		});

		logger.info('Message sent: %s', info.messageId);
	} catch (e) {
		logger.error(e);
		throw e;
	}
}