implement smtp

This commit is contained in:
LouisLam 2021-07-10 01:08:08 +08:00
parent 0ad04d1468
commit 072e86542a
4 changed files with 73 additions and 4 deletions

5
package-lock.json generated
View File

@ -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": { "nopt": {
"version": "3.0.6", "version": "3.0.6",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz", "resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz",

View File

@ -16,6 +16,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"nodemailer": "^6.6.2",
"password-hash": "^1.2.2", "password-hash": "^1.2.2",
"redbean-node": "0.0.20", "redbean-node": "0.0.20",
"socket.io": "^4.0.2", "socket.io": "^4.0.2",

View File

@ -1,6 +1,7 @@
const axios = require("axios"); const axios = require("axios");
const {R} = require("redbean-node"); const {R} = require("redbean-node");
const FormData = require('form-data'); const FormData = require('form-data');
const nodemailer = require("nodemailer");
class Notification { class Notification {
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
@ -42,15 +43,14 @@ class Notification {
} }
let res = await axios.post(notification.webhookURL, finalData, config) let res = await axios.post(notification.webhookURL, finalData, config)
console.log(res.data)
return true; return true;
} catch (error) { } catch (error) {
console.log(error) console.log(error)
return false; return false;
} }
} else if (notification.type === "smtp") {
return await Notification.smtp(notification, msg)
} else { } else {
throw new Error("Notification type is not supported") throw new Error("Notification type is not supported")
} }
@ -91,6 +91,29 @@ class Notification {
await R.trash(bean) 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 = { module.exports = {

View File

@ -15,7 +15,7 @@
<select class="form-select" id="type" v-model="notification.type"> <select class="form-select" id="type" v-model="notification.type">
<option value="telegram">Telegram</option> <option value="telegram">Telegram</option>
<option value="webhook">Webhook</option> <option value="webhook">Webhook</option>
<option value="email">Email</option> <option value="smtp">Email (SMTP)</option>
<option value="discord">Discord</option> <option value="discord">Discord</option>
</select> </select>
</div> </div>
@ -82,6 +82,46 @@
</div> </div>
</template> </template>
<template v-if="notification.type === 'smtp'">
<div class="mb-3">
<label for="hostname" class="form-label">Hostname</label>
<input type="text" class="form-control" id="hostname" required v-model="notification.smtpHost">
</div>
<div class="mb-3">
<label for="port" class="form-label">Port</label>
<input type="number" class="form-control" id="port" v-model="notification.smtpPort" required min="0" max="65535" step="1">
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value="" id="secure" v-model="notification.smtpSecure">
<label class="form-check-label" for="secure">
Secure
</label>
</div>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" required v-model="notification.smtpUsername" autocomplete="false">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" required v-model="notification.smtpPassword" autocomplete="false">
</div>
<div class="mb-3">
<label for="from-email" class="form-label">From Email</label>
<input type="email" class="form-control" id="from-email" required v-model="notification.smtpFrom" autocomplete="false">
</div>
<div class="mb-3">
<label for="to-email" class="form-label">To Email</label>
<input type="email" class="form-control" id="to-email" required v-model="notification.smtpTo" autocomplete="false">
</div>
</template>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">