node.jsplesknodemailerplesk-onyx

Node.js plesk - Sending emails with nodemailer not working


I'm using node.js with nodemailer on a fresh ubuntu plesk root server.

When trying to send an email like this:

module.exports = {
    SMTP_HOST: 'host',
    SMTP_USER: 'username',
    SMTP_PASS: 'password',
};

let transporter = nodemailer.createTransport({
  host: SMTP_HOST,
  //port: 465,
  port: 587,
  secure: true,
  auth: {
      user: SMTP_USER,
      pass: SMTP_PASS
  },
  tls:{
    rejectUnauthorized:false  // if on local
  }
});

... I'm getting this error message:

{ Error: connect ECONNREFUSED ip-adress:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNECTION',
syscall: 'connect',
address: 'ip-adress',
port: 587,
command: 'CONN' }

How can I send emails via a plesk root server?

... if I use port 465 I'm getting this error:

{ Error: Data command failed: 550 5.7.1 Command rejected
at SMTPConnection._formatError (/home/project/node_modules/nodemailer/lib/smtp-connection/index.js:605:19)
at SMTPConnection._actionDATA (/home/project/node_modules/nodemailer/lib/smtp-connection/index.js:1454:34)
at SMTPConnection._responseActions.push.str (/home/project/node_modules/nodemailer/lib/smtp-connection/index.js:1426:26)
at SMTPConnection._processResponse (/home/project/node_modules/nodemailer/lib/smtp-connection/index.js:764:20)
at SMTPConnection._onData (/home/project/node_modules/nodemailer/lib/smtp-connection/index.js:570:14)
at TLSSocket._socket.on.chunk (/home/project/node_modules/nodemailer/lib/smtp-connection/index.js:522:47)
at TLSSocket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
code: 'EENVELOPE',
response: '550 5.7.1 Command rejected',
responseCode: 550,
command: 'DATA' }  

Solution

  • Adding sendmail: true (https://nodemailer.com/transports/sendmail/) solved the issue:

    let transporter = nodemailer.createTransport({
      sendmail: true,
      newline: 'unix',
      path: '/usr/sbin/sendmail',
      host: SMTP_HOST,
      //port: 465,
      port: 587,
      secure: true,
      auth: {
          user: SMTP_USER,
          pass: SMTP_PASS
      },
      tls:{
        rejectUnauthorized:false  // if on local
      }
    });