smtp send mail via postfix and nodejs nodemailer connection closed

12,715

Solution 1

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'.

Solution 2

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: '[email protected]',
       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" [email protected]

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.

Share:
12,715
skovrec
Author by

skovrec

Updated on June 04, 2022

Comments

  • skovrec
    skovrec almost 2 years

    i have problem of remote sending via smtp server using nodejs module nodemailer

    host: 'mail.5cpa.ru'
    port: 587
    secure: false (true tried also)
    

    smtp server is mine, and i can send mails through thenderbird, using same setting

    log

    server 220 5cpa.ru ESMTP Postfix (Ubuntu)
    client EHLO vonica
    server 250-5cpa.ru
    250-PIPELINING
    250-SIZE 15728640
    250-ETRN
    250-STARTTLS
    250-ENHANCEDSTATUSCODES
    250-8BITMIME
    250 DSN
    client STARTTLS
    server 220 2.0.0 Ready to start TLS
    socket Closing connection to the server
    

    thanks!