summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/send-grid.js
blob: 3489f63850f0bf02bf63972e533d20e7c9d0ebbe (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
const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class SendGrid extends NotificationProvider {
    name = "SendGrid";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";

        try {
            let config = {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: `Bearer ${notification.sendgridApiKey}`,
                },
            };

            let personalizations = {
                to: [{ email: notification.sendgridToEmail }],
            };

            // Add CC recipients if provided
            if (notification.sendgridCcEmail) {
                personalizations.cc = notification.sendgridCcEmail
                    .split(",")
                    .map((email) => ({ email: email.trim() }));
            }

            // Add BCC recipients if provided
            if (notification.sendgridBccEmail) {
                personalizations.bcc = notification.sendgridBccEmail
                    .split(",")
                    .map((email) => ({ email: email.trim() }));
            }

            let data = {
                personalizations: [ personalizations ],
                from: { email: notification.sendgridFromEmail.trim() },
                subject:
          notification.sendgridSubject ||
          "Notification from Your Uptime Kuma",
                content: [
                    {
                        type: "text/plain",
                        value: msg,
                    },
                ],
            };

            await axios.post(
                "https://api.sendgrid.com/v3/mail/send",
                data,
                config
            );
            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = SendGrid;