Nodemailer TypeError : Cannot read property 'sendMail' of undefined

Nodemailer TypeError : Cannot read property 'sendMail' of undefined

我有一个联系表单,预计会向我的网站所有者发送电子邮件。 服务器是一个使用 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: &nbsp;</b>' + req.body.name +'&nbsp;'+ req.body.surname + '<br>' +
                '<b>Société: &nbsp;</b>' + req.body.company + '<br>'+
                '<b>Objet :&nbsp;</b>' + req.body.subject + '<br>' +
                '<b>Message :&nbsp;</b><br>' + req.body.msg + '<br>' +
                '<b>Coordonnées :&nbsp;</b>' + req.body.tel + '&nbsp;/<br>&nbsp' +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)应该是正确的使用方法,数据和回调以正确的方式传递给方法(在我看来).我做错了什么?

发现了!我只是在 nodemailer.createTransport 之后删除了验证功能,并且 sendMail 功能按预期工作。