mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-09 19:04:36 +08:00
feat: add notification provider SendGrid
(#5205)
Co-authored-by: Frank Elsinga <frank@elsinga.de>
This commit is contained in:
65
server/notification-providers/send-grid.js
Normal file
65
server/notification-providers/send-grid.js
Normal file
@@ -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;
|
Reference in New Issue
Block a user