added functional mailer
This commit is contained in:
@@ -17,7 +17,7 @@ export const ContactPage = () => {
|
|||||||
|
|
||||||
const sendMessage = async () => {
|
const sendMessage = async () => {
|
||||||
// Logic to send the message
|
// Logic to send the message
|
||||||
const result = await fetch(`${API_BASE}/contact`, {
|
const result = await fetch(`${API_BASE}/api/users/contact`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
||||||
|
|||||||
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
|
// database funcs import
|
||||||
import { loginFunc, changePassword } from "./database/userMgmt.database.js";
|
import { loginFunc, changePassword } from "./database/userMgmt.database.js";
|
||||||
|
import { sendMail } from "./services/mailer_v2.js";
|
||||||
|
|
||||||
router.post("/login", async (req, res) => {
|
router.post("/login", async (req, res) => {
|
||||||
const result = await loginFunc(req.body.username, req.body.password);
|
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;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user