nodemailer发邮件失败 unable to verify the first certificate
快过年了,要做个监控,需要用到发邮件的功能,下载了nodemailer,一路照着DEMO来,结果出现了个诡异的异常.
{ Error: unable to verify the first certificate
at Error (native)
at TLSSocket.<anonymous> (_tls_wrap.js:1092:38)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket._finishInit (_tls_wrap.js:610:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) code: 'ECONNECTION', command: 'CONN' }
查了半天,网上说各种的都有,就是没这个错的描述,没办法翻墙去问Google,查到了一个,说是因为TLS的问题,不是nodemailer的锅,那么TLS出了什么问题?
回查了一下TLS的说明.找到了这么一条.
rejectUnauthorized <boolean> If not false the server will reject any connection which is not authorized with the list of supplied CAs. This option only has an effect if requestCert is true. Defaults to true.
也就是说,如果我的邮件服务器的证书不在CA里,就会被拒绝,那么好了,看样子是要把rejectUnauthorized设置成true, 再回头查nodemailer的说明,还好创建链接时,有个TLS的配置节点,所以配置改成:
let transporter = nodemailer.createTransport({
host: 'mail.xxxxx.com',
port:465,
secure: true,
auth: {
user: "[email protected]",
pass: "***********"
},
tls:{rejectUnauthorized: false}
});
问题解决,哈哈,这个年终于可以安心的过了.