This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathemailUtil.ts
50 lines (46 loc) · 1.53 KB
/
emailUtil.ts
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
import config from "./config";
import { Recording } from "./models/Recording";
import { TrackTag } from "./models/TrackTag";
import log from "./logging";
import moment, { Moment } from "moment";
import { SMTPClient, Message } from "emailjs";
function alertBody(
recording: Recording,
tag: TrackTag,
camera: String
): string[] {
const dateTime = moment(recording.recordingDateTime)
.tz(config.timeZone)
.format("h:mma Do MMM");
let html = `<b>${camera} has detected a ${tag.what} - ${dateTime}</b>`;
html += `<a href="${config.server.recording_url_base}/${recording.id}/${tag.TrackId}?device=${recording.DeviceId}">View Recording</a>`;
html += "<br><p>Thanks,<br> Cacophony Team</p>";
let text = `${camera} has detected a ${tag.what} - ${dateTime}\r\n`;
text += `Go to ${config.server.recording_url_base}/${recording.id}/${tag.TrackId}?device=${recording.DeviceId} to view this recording\r\n`;
text += "Thanks, Cacophony Team";
return [html, text];
}
async function sendEmail(
html: string,
text: string,
to: string,
subject: string
): Promise<boolean> {
const client = new SMTPClient(config.smtpDetails);
log.info(`Sending email with subject ${subject} to ${to}`);
try {
const message = new Message({
text: text,
from: config.smtpDetails.from_name,
to: to,
subject: subject,
attachment: [{ data: html, alternative: true }]
});
await client.sendAsync(message);
} catch (err) {
log.error(err);
return false;
}
return true;
}
export { sendEmail, alertBody };