redo email to use EOES, modify index & update express(?)
This commit is contained in:
48
routes/email.js
Normal file
48
routes/email.js
Normal 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}
|
||||
Reference in New Issue
Block a user