Answer the question
In order to leave comments, you need to log in
Configuring nodemailer mail notifications?
Hello! I have a feedback form on my site, I want that after submitting it, the form data will be sent to my mail provided by the hosting. It used to be set up for my mail via Gmail. And now, taking into account hosting, I don’t know how to register everything correctly
const dotenv = require("dotenv");
dotenv.config({ path: __dirname + "/.env" });
const express = require("express");
const bodyParser = require("body-parser");
const nodemailer = require("nodemailer");
const favicon = require("serve-favicon");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(favicon(__dirname + "/favicon.ico"));
app.post("/contacts", (req, res) => {
console.log(req.body);
nodemailer.createTestAccount((err, account) => {
const htmlEmail = `
<h3>Contact details</h3>
<ul>
<li>Имя: ${req.body.name}</li>
<li>Фамилия: ${req.body.lastName}</li>
<li>Email: ${req.body.email}</li>
<li>Телефон: ${req.body.phone}</li>
<li>Организация: ${req.body.organization}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
let transporter = nodemailer.createTransport({
host: "",
port: "",
secure: true,
requireTLS: true,
auth: {
user: process.env.REACT_APP_EMAIL,
pass: process.env.REACT_APP_PASS
}
});
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
let mailOptions = {
subject: "New message",
text: req.body.message,
html: htmlEmail
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err);
}
console.log("Message sent: %s", info.message);
console.log("Message URL: %s", nodemailer.getTestMessageUrl(info));
});
});
});
const PORT = process.env.PORT || 3001;
if (process.env.NODE_ENV === "production") {
// Serve any static files
app.use(express.static(path.join(__dirname, "/build")));
// Handle React routing, return all requests to React app
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "/build", "index.html"));
});
}
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question