Currently, I am utilizing express to dispatch emails using the nodemailer module. Here is a snippet of my code:
app.post('/', (req, res) => {
const output = `
<p>You have a new contact request.</p>
<h3>Contact Details</h3>
<ul>
<li>Name:${req.body.name}</li>
<li>Email:${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
// setting up the transporter object with SMTP details
let transporter = nodeMailer.createTransport({
host: "mail.gmail.com",
port: 587,
secure: false,
auth: {
user: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8094808c8481ad8a8c808481c38e8280">[email protected]</a>',
pass: '2020'
},
tls: {
rejectUnauthorized: false
}
});
// sending mail through configured transport
let mailOptions = {
from: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="18756175797174587f79757174367b7775">[email protected]</a>",
to: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a27332c38232f242e0a2d272b232664292527">[email protected]</a>",
subject: "the subject",
html: output
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodeMailer.getTestMessageUrl(info));
res.render('index', { msg: 'email has been sent' });
res.redirect('back');
});});
Upon hitting the submit
button, the following code redirects to the Contact page:
res.redirect('back');
A message is then displayed using this code:
res.render('index', { msg: 'email has been sent' });
My objective now is to showcase the text in an alert box instead of using the previously mentioned method. I attempted the following approach:
res.render('index','<script>alert("email has been sent")</script>');
Unfortunately, this approach did not work at all. Moreover, I am not implementing ajax. Any thoughts or possible solutions?