Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/contact.js

33 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-08-21 12:41:34 +02:00
let express = require("express");
let router = express.Router();
let error = require("./utils/error");
let reCaptcha = require("../middlewares/reCaptcha");
let Message = require("emailjs").Message;
router.post("/", reCaptcha, async (req, res) => {
if (!req.body.firstName || !req.body.lastName || !req.body.email || !req.body.subject || ! req.body.message)
return error(req, res, "Invalid contact form !", 400, "Missing arg");
let config = req.app.get("config");
req.app.get("mailClient").send( new Message({
text:
`${req.body.firstName} ${req.body.lastName} <${req.body.email}> ${req.body.phoneNumber ? "["+req.body.phoneNumber+"] " : ""}- ${req.body.subject}
${req.body.message}`,
from: config.email.from,
to: config.email.contact,
cc: `${req.body.firstName} ${req.body.lastName} <${req.body.email}>`,
subject: res.__("contact")+": "+req.body.subject
}), (err, message) => {
if (err)
return error(req, res, "Fail to send message !", 500,
req.app.get("env") !== "production" ? err : undefined);
else
res.render("contact", {title: "SOD - Contact"});
});
});
module.exports = router;