smtp send mail via postfix and nodejs nodemailer connection closed

心不动则不痛 提交于 2019-11-30 17:02:16

I had similar problem (maybe the same) and I figure out that this happens when i use self signed SSL certificate. I fixed this by adding "rejectUnauthorized" option to tls:

var transporter = nodemailer.createTransport(smtpTransport({
    host: host,
    port: 25,
    auth: {
        user: user,
        pass: pass
    },
    tls:{
        rejectUnauthorized: false
    }
}));

The error is applied to the callback of "sendMail" function and is not emitted as 'error'.

I don't have enough reputation to reply above, but you could also check the latest syntax from Nodemailer, ie:

 const smtpConfig = {
     host: 'smtp.gmail.com',
     port: 465,
     secure: true, // use SSL
     auth: {
       user: 'user@gmail.com',
       pass: 'pass'
     }
 }

note:

 secure: true // use SSL

You might not need that TLS work-around if you tell it to use SSL.

Reference: https://nodemailer.com/2-0-0-beta/setup-smtp/

Mine is currently running off postfix and mailx, and I only had to put in the hostname:

 const transporter = nodemailer.createTransport({
          name: 'localhost'
 })

That should work as long as you can perform this test (type in shell):

 echo "Email Body Test" | mail -s "Email Subject Test" you@yourdomain.com

If it doesn't work, investigate your firewall, port 25 (probably not the issue in this case, but in other similar cases).

Also confirm the user running node has permission to use the mail program.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!