From 072e86542a648a5ce7115898dcfabe65639aecfc Mon Sep 17 00:00:00 2001 From: LouisLam Date: Sat, 10 Jul 2021 01:08:08 +0800 Subject: [PATCH] implement smtp --- package-lock.json | 5 ++++ package.json | 1 + server/notification.js | 29 ++++++++++++++++-- src/components/NotificationDialog.vue | 42 ++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index d0a518585..379c15e56 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2261,6 +2261,11 @@ } } }, + "nodemailer": { + "version": "6.6.2", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.2.tgz", + "integrity": "sha512-YSzu7TLbI+bsjCis/TZlAXBoM4y93HhlIgo0P5oiA2ua9Z4k+E2Fod//ybIzdJxOlXGRcHIh/WaeCBehvxZb/Q==" + }, "nopt": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz", diff --git a/package.json b/package.json index a1e706665..7f193d6f6 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "express": "^4.17.1", "form-data": "^4.0.0", "jsonwebtoken": "^8.5.1", + "nodemailer": "^6.6.2", "password-hash": "^1.2.2", "redbean-node": "0.0.20", "socket.io": "^4.0.2", diff --git a/server/notification.js b/server/notification.js index d5adb5d48..4c6601c61 100644 --- a/server/notification.js +++ b/server/notification.js @@ -1,6 +1,7 @@ const axios = require("axios"); const {R} = require("redbean-node"); const FormData = require('form-data'); +const nodemailer = require("nodemailer"); class Notification { static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { @@ -42,15 +43,14 @@ class Notification { } let res = await axios.post(notification.webhookURL, finalData, config) - - console.log(res.data) - return true; } catch (error) { console.log(error) return false; } + } else if (notification.type === "smtp") { + return await Notification.smtp(notification, msg) } else { throw new Error("Notification type is not supported") } @@ -91,6 +91,29 @@ class Notification { await R.trash(bean) } + + static async smtp(notification, msg) { + + let transporter = nodemailer.createTransport({ + host: notification.smtpHost, + port: notification.smtpPort, + secure: notification.smtpSecure, + auth: { + user: notification.smtpUsername, + pass: notification.smtpPassword, + }, + }); + + // send mail with defined transport object + let info = await transporter.sendMail({ + from: `"Uptime Kuma" <${notification.smtpFrom}>`, + to: notification.smtpTo, + subject: msg, + text: msg, + }); + + return true; + } } module.exports = { diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index bdc537359..b6b92bf19 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -15,7 +15,7 @@ @@ -82,6 +82,46 @@ +