Trying to set up a contact form with nodemailer. Here's what's in my app.js:
// EMail configuration
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "myemailaddress",
pass: "xxx"
}
});
// CONTACT FORM
app.get('/contact', function (req, res) {
res.render("contact");
});
app.post('/contact', function (req, res) {
var mailOptions = {
from: req.body.email, // sender address
to: "myemailaddress", // list of receivers
subject: req.body.subject, // Subject line
text: req.body.message, // plaintext body
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // shut down the connection pool, no more messages
});
res.render("contact", { success: "building web app" });
});
And my contact.jade template looks like this:
form#contact-form(action="/contact", method="POST")
div.span5
p Full name:
input#name(type="text", name="name")
p Email address:
input#email(type="email", name="email")
p Subject:
input#subject(type="text", name="subject")
p Message:
textarea#message(type="text", name="message", rows="5")
p: button(type="submit") Send message
The email now works, but comes from myemailaddress rather than the one I enter into the email field on the template. Any ideas
How do we use Nodemailer? 8. Nodemailer is a Node.js module that makes it simple to send emails from a server. Whether you want to interact with your users or simply inform yourself when something goes wrong, the mail is one of your alternatives.
Whether you want to interact with your users or simply inform yourself when something goes wrong, the mail is one of your alternatives. The project began in 2010 when there was no logical way to send email messages, and it is now the default solution for most Node.js users.
If an attachment or message node tries to fetch something from a file the sending returns an error. If this field is also set in the transport options, then the value in mail data is ignored
**Memory leak warning!** When using readable streams as content and sending fails then Nodemailer does not abort the already opened but not yet finished stream, you need to do this yourself. Nodemailer only closes the streams it has opened itself (eg. file paths, URLs)
Gmail and many other email services don't allow you to send messages with various FROM
field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With