我一直在尝试使用nodemailer。我被困。 它运行控制台日志,我没有任何错误或任何东西。 req.body充满了数据。
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport();
app.post('/contact-form', sendMail = function (req, res) {
transporter.sendMail({
from: 'req.body.contactEmail',
to: 'mymail@mail.com',
subject: 'Message from ' + req.body.contactEmail,
text: req.body.contactMsg + 'my contact information: ' + req.body.contactEmail + " " + req.body.contactNummer
}),function(error, response) {
if (error) {
console.log(error);
} else {
console.log("Message sent: " + response.message);
}
}
console.log(req.body.contactMsg);
});
答案 0 :(得分:1)
让它更容易去,并保持开启。 https://myaccount.google.com/security
向下滚动到此页面,您将获得允许不太安全的应用:ON,只需开启,它就能正常工作。这是完整的代码 -
var nodemailer = require('nodemailer'); var smtpTransport = require('nodemailer-smtp-transport');
//联系我们 app.post('/ contact',function(req,res){
var mailOpts, smtpTrans;
//Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems.
smtpTrans = nodemailer.createTransport(smtpTransport({
service: 'gmail',
// host:'smtp.gmail.com',
// port:465,
// secure:true,
auth: {
user: "xxxx@gmail.com",
pass: "xxxxxxx"
}
}));
var mailoutput = "<html>\n\
<body>\n\
<table>\n\
<tr>\n\
<td>Name: </td>" + req.body.form_name + "<td></td>\n\
</tr>\n\
<tr>\n\
<td>Email: </td><td>" + req.body.form_email + "</td>\n\
</tr>\n\
<tr>\n\
<td>MN: </td>" + req.body.form_phone + "<td></td>\n\
</tr>\n\
<tr>\n\
<td>Messge: </td>" + req.body.form_message + "<td></td>\n\
</tr>\n\
</table></body></html>";
//Mail options
mailOpts = {
to: "NameOfYourWebsite <xxxxxxxxxxxx@gmail.com>",
subject: req.body.form_subject,
html: mailoutput
};
smtpTrans.sendMail(mailOpts, function (error, res) {
if (error) {
// res.send("Email could not send due to error" +error);
return console.log(error);
}
});
//console.log('Message sent successfully!');
res.render('contact.ejs');
});
//console.log(query.sql);
});