我有一个联系表,希望该表可以将电子邮件发送给我的网站所有者。 服务器是可与Express和NodeMailer中间件一起使用的Node V8,并且设置如下:
const nodemailer = require('nodemailer');
const mailer = nodemailer.createTransport(
{host:'ns0.ovh.net',
port:587,
secure : false,
auth:{
user:'frederic.merle@montardplomberie.fr',
pass:'xyz'
}
}
).verify(function(error, success){
if (error){
console.log('server is not ready to take messages')
}else{
console.log('server is ready to take messages')
}
})
api.post('/mail', (req, res) =>{
console.log("prepare to send mail request")
if(req.body.company === null){
req.body.company ="aucun";
}
if(req.body.subject === null){
req.body.subject="aucun";
}
const mailOptions = {
from: req.body.from,
to: 'frederic.montard@montardplomberie.fr',
subject: 'Message reçu de www.montardplomberie.fr',
text : req.body.msg,
html : '<b>De: </b>' + req.body.name +' '+ req.body.surname + '<br>' +
'<b>Société: </b>' + req.body.company + '<br>'+
'<b>Objet : </b>' + req.body.subject + '<br>' +
'<b>Message : </b><br>' + req.body.msg + '<br>' +
'<b>Coordonnées : </b>' + req.body.tel + ' /<br> ' +req.body.email
};
mailer.sendMail(mailOptions, error => console.log(error), info => console.log('Message sent: '+ info.messageId));
mailer.close();
})
但是,当我的请求被触发时,会出现错误500,并且终端显示以下内容:
server is ready to take messages
prepare to send mail request
TypeError: Cannot read property 'sendMail' of undefined
at api.post (/home/phmerle/sandbox/gestsoft/backend/server.js:98:12)
at Layer.handle [as handle_request] (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/layer.js:95:5)
at next (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/layer.js:95:5)
at /home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/index.js:335:12)
at next (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/index.js:174:3)
at router (/home/phmerle/sandbox/gestsoft/backend/node_modules/express/lib/router/index.js:47:12)
已经深入到nodemailers文档中,似乎transport.sendMail(在我的情况下为mailer.sendMail)应该是正确的使用方法,并且数据和回调以正确的方式传递给该方法(我认为)。我做错了什么?
答案 0 :(得分:0)
发现了!在nodemailer.createTransport和sendMail函数按预期工作后,我只是删除了verify函数。