forked from pankleks/pm2-health
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mail.js
68 lines (68 loc) · 2.52 KB
/
Mail.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mail = void 0;
const Mailer = require("nodemailer");
const Fs = require("fs");
const os_1 = require("os");
const Log_1 = require("./Log");
class Mail {
constructor(_config) {
this._config = _config;
this._template = "<p><!-- body --></p><p><!-- timeStamp --></p>";
if (!this._config.smtp)
this._config.smtp = { disabled: true };
if (this._config.smtp.disabled === true)
return; // don't analyze config if disabled
if (!this._config.smtp)
throw new Error(`[smtp] not set`);
if (!this._config.smtp.host)
throw new Error(`[smtp.host] not set`);
if (!this._config.smtp)
throw new Error(`[smtp.port] not set`);
try {
this._template = Fs.readFileSync("Template.html", "utf8");
}
catch (_a) {
(0, Log_1.info)(`Template.html not found`);
}
}
configChanged() {
if (!this._config.mailTo)
throw new Error(`[mailTo] not set`);
}
async send(message) {
if (this._config.smtp.disabled === true) {
(0, Log_1.debug)("mail sending is disbled in config");
return;
}
const temp = {
host: this._config.smtp.host,
port: this._config.smtp.port,
tls: { rejectUnauthorized: false },
secure: this._config.smtp.secure === true,
auth: null,
name: typeof this._config.smtp.clientHostName == "string" && this._config.smtp.clientHostName ? this._config.smtp.clientHostName : null
};
if (this._config.smtp.user)
temp.auth = {
user: this._config.smtp.user,
pass: this._config.smtp.password
};
const transport = Mailer.createTransport(temp), headers = {};
if (message.priority)
headers["importance"] = message.priority;
await transport.sendMail({
to: this._config.mailTo,
from: this._config.smtp.from || this._config.smtp.user,
replyTo: this._config.replyTo,
subject: `pm2-health: ${(0, os_1.hostname)()}, ${message.subject}`,
html: this._template
.replace(/<!--\s*body\s*-->/, message.body)
.replace(/<!--\s*timeStamp\s*-->/, new Date().toISOString()),
attachments: message.attachements,
headers
});
}
}
exports.Mail = Mail;
//# sourceMappingURL=Mail.js.map