redo email to use EOES, modify index & update express(?)

This commit is contained in:
Enstrayed
2024-10-16 20:35:44 -07:00
parent 0bf467a1c8
commit fa2ef6927a
5 changed files with 186 additions and 103 deletions

48
routes/email.js Normal file
View File

@@ -0,0 +1,48 @@
import { app, globalConfig } from "../index.js" // Get globals from index
import { checkToken } from "../liberals/auth.js"
import { logRequest } from "../liberals/logging.js"
import * as nodemailer from 'nodemailer'
const transporter = nodemailer.createTransport({
host: globalConfig.email.host,
port: 587,
secure: false,
auth: {
user: globalConfig.email.username,
pass: globalConfig.email.password
}
})
app.post("/api/sendemail", (rreq,rres) => {
checkToken(rreq.get("Authorization"),"email").then(authRes => {
if (authRes === false) {
rres.sendStatus(401)
} else if (authRes === true) {
if (rreq.body == undefined || rreq.body.recipient == undefined) { // 2024-05-11: Turbo bodge check to make sure request JSON is valid, probably wont work but whatever
rres.sendStatus(400)
} else {
transporter.sendMail({
from: "Enstrayed API <enstrayedapi@enstrayed.com>",
to: rreq.body.recipient,
subject: rreq.body.subject ?? "Subject Not Set",
text: rreq.body.message ?? "Message Not Set"
}).then(transportResponse => {
if (transportResponse.response.slice(0,1) === "2") {
logRequest(rres,rreq,200,transportResponse.response)
rres.status(200).send(transportResponse.response)
} else {
logRequest(rres,rreq,400,transportResponse.response)
rres.status(400).send(transportResponse.response)
}
}).catch(transportError => {
logRequest(rres,rreq,500,transportError)
rres.sendStatus(500)
})
}
}
})
})
export {app}