diff --git a/backend/routes/api.route.js b/backend/routes/api.route.js index 7ffa12b..67aae71 100644 --- a/backend/routes/api.route.js +++ b/backend/routes/api.route.js @@ -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; diff --git a/backend/routes/mailer.js b/backend/routes/mailer.js new file mode 100644 index 0000000..da4f8b4 --- /dev/null +++ b/backend/routes/mailer.js @@ -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); + }; +}