Reputation: 21
I'm trying to create a email form to add to a website using nodemailer. I have successfully managed to send the email to the desired the email and tried to notify the user that the message was sent using an alert but that doesn't seem to work.
My code:
app.post("/", function (req, res) {
console.log(req.body.email + " " + req.body.message);
var mailOptions = {
from: ****@gmail.com',
to: ****@gmail.com',
subject: '[WEBSITE] Email from '+ req.body.email,
text: req.body.message
}
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
alert("Your mail sent successfully!")
console.log("email sent: " + info);
}
})
});
I followed the w3shcools page on nodemailer to get the email working. The html part is a barebones HTML form I created for the purpose of testing the code.
HTML:
<form action="/" method="post">
<input type="text" name="email" placeholder="Your email">
<textarea name="message" placeholder="Your message" cols="30" rows="10"></textarea>
<button type="submit">Send</button>
</form>
</div>
My question is how do you give a simple popup alert to the user saying the mail was sent successfully
Upvotes: 0
Views: 795
Reputation: 1855
alert on server code will do nothing. you can send the json and handle it on the client side or you can pass in the script tag in response with the alert.
Try this this should work. (But this is not a recommended approach).
app.post("/", function (req, res) {
console.log(req.body.email + " " + req.body.message);
var mailOptions = {
from: ****@gmail.com',
to: ****@gmail.com',
subject: '[WEBSITE] Email from '+ req.body.email,
text: req.body.message
}
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
res.send(`<script>alert("Email Sent Successfully.")</script>`);
console.log("email sent: " + info);
}
})
});
Upvotes: 0