added mailer

This commit is contained in:
2026-01-27 20:37:50 +01:00
parent a551bf6f6f
commit 62468fe67b
2 changed files with 42 additions and 1 deletions

View File

@@ -1,9 +1,26 @@
import express from "express";
const router = express.Router();
import sendMail from "./mailer.js";
router.get("/", (req, res) => {
res.json({ message: "API is working!" }).status(200);
});
router.post("/new-message", (req, res) => {
const message = req.body.message;
const title = req.body.title;
const recipient = req.body.recipient;
sendMail(message, title, recipient)
.then(() => {
res.json({ status: "Message sent successfully" }).status(200);
})
.catch((error) => {
res
.json({ status: "Error sending message", error: error.message })
.status(500);
});
});
export default router;

24
backend/routes/mailer.js Normal file
View File

@@ -0,0 +1,24 @@
import nodemailer from "nodemailer";
export function sendMail(message, title, recipient, host, user, pass, from) {
const transporter = nodemailer.createTransport({
host: host,
port: port,
secure: 465,
auth: {
user: user,
pass: pass,
},
});
async () => {
const info = await transporter.sendMail({
from: from,
to: recipient,
subject: title,
text: message,
});
console.log("Message sent: %s", info.messageId);
};
}