added functional mailer
This commit is contained in:
45
backendV2/routes/app/services/mailer_v2.js
Normal file
45
backendV2/routes/app/services/mailer_v2.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import nodemailer from "nodemailer";
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
export function sendMail(username, message) {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.MAIL_HOST,
|
||||
port: process.env.MAIL_PORT,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: process.env.MAIL_USER,
|
||||
pass: process.env.MAIL_PASSWORD,
|
||||
},
|
||||
});
|
||||
|
||||
(async () => {
|
||||
const mailText = `Neue Kontaktanfrage im Ausleihsystem.\n\nBenutzername: ${username}\n\nNachricht:\n${message}`;
|
||||
|
||||
const mailHtml = `<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Neue Nachricht im Ausleihsystem</title>
|
||||
</head>
|
||||
<body style="font-family: Arial, sans-serif; line-height: 1.5; color: #222;">
|
||||
<h2>Neue Nachricht im Ausleihsystem</h2>
|
||||
<p><strong>Benutzername:</strong> ${username}</p>
|
||||
<p><strong>Nachricht:</strong></p>
|
||||
<p style="white-space: pre-line;">${message}</p>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
const info = await transporter.sendMail({
|
||||
from: '"Ausleihsystem" <noreply@mcs-medien.de>',
|
||||
to: process.env.MAIL_SENDEES_CONTACT,
|
||||
subject: "Sie haben eine neue Nachricht!",
|
||||
text: mailText,
|
||||
html: mailHtml,
|
||||
});
|
||||
|
||||
// debugging logs
|
||||
// console.log("Message sent:", info.messageId);
|
||||
})();
|
||||
// console.log("sendMailLoan called");
|
||||
}
|
||||
@@ -6,6 +6,7 @@ dotenv.config();
|
||||
|
||||
// database funcs import
|
||||
import { loginFunc, changePassword } from "./database/userMgmt.database.js";
|
||||
import { sendMail } from "./services/mailer_v2.js";
|
||||
|
||||
router.post("/login", async (req, res) => {
|
||||
const result = await loginFunc(req.body.username, req.body.password);
|
||||
@@ -35,4 +36,13 @@ router.post("/change-password", authenticate, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/contact", authenticate, async (req, res) => {
|
||||
const message = req.body.message;
|
||||
const username = req.user.username;
|
||||
|
||||
sendMail(username, message);
|
||||
|
||||
res.status(200).json({ message: "Contact message sent successfully" });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user