diff --git a/server/notification-providers/send-grid.js b/server/notification-providers/send-grid.js new file mode 100644 index 0000000000..3489f63850 --- /dev/null +++ b/server/notification-providers/send-grid.js @@ -0,0 +1,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; diff --git a/server/notification.js b/server/notification.js index 26daeb0428..e7977eb4af 100644 --- a/server/notification.js +++ b/server/notification.js @@ -68,6 +68,7 @@ const GtxMessaging = require("./notification-providers/gtx-messaging"); const Cellsynt = require("./notification-providers/cellsynt"); const Onesender = require("./notification-providers/onesender"); const Wpush = require("./notification-providers/wpush"); +const SendGrid = require("./notification-providers/send-grid"); class Notification { @@ -153,6 +154,7 @@ class Notification { new GtxMessaging(), new Cellsynt(), new Wpush(), + new SendGrid() ]; for (let item of list) { if (! item.name) { diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index ec86d15f8a..f6d7280299 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -165,6 +165,7 @@ export default { "whapi": "WhatsApp (Whapi)", "gtxmessaging": "GtxMessaging", "Cellsynt": "Cellsynt", + "SendGrid": "SendGrid" }; // Put notifications here if it's not supported in most regions or its documentation is not in English diff --git a/src/components/notifications/SendGrid.vue b/src/components/notifications/SendGrid.vue new file mode 100644 index 0000000000..18118f4690 --- /dev/null +++ b/src/components/notifications/SendGrid.vue @@ -0,0 +1,47 @@ + + + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 35a3a920ad..efa2af5c49 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -66,6 +66,7 @@ import Whapi from "./Whapi.vue"; import Cellsynt from "./Cellsynt.vue"; import WPush from "./WPush.vue"; import SIGNL4 from "./SIGNL4.vue"; +import SendGrid from "./SendGrid.vue"; /** * Manage all notification form. @@ -139,7 +140,8 @@ const NotificationFormList = { "whapi": Whapi, "gtxmessaging": GtxMessaging, "Cellsynt": Cellsynt, - "WPush": WPush + "WPush": WPush, + "SendGrid": SendGrid, }; export default NotificationFormList; diff --git a/src/lang/en.json b/src/lang/en.json index c07e06fab4..5bfc3bd920 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -1051,5 +1051,7 @@ "From":"From", "Can be found on:": "Can be found on: {0}", "The phone number of the recipient in E.164 format.": "The phone number of the recipient in E.164 format.", - "Either a text sender ID or a phone number in E.164 format if you want to be able to receive replies.":"Either a text sender ID or a phone number in E.164 format if you want to be able to receive replies." + "Either a text sender ID or a phone number in E.164 format if you want to be able to receive replies.":"Either a text sender ID or a phone number in E.164 format if you want to be able to receive replies.", + "SendGrid API Key": "SendGrid API Key", + "Separate multiple email addresses with commas": "Separate multiple email addresses with commas" }