Node.js plesk - 使用 nodemailer 发送电子邮件不工作

Node.js plesk - Sending emails with nodemailer not working

我在新 ubuntu plesk 根服务器上使用 node.js 和 nodemailer。

尝试发送这样的电子邮件时:

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
  }
});

...我收到此错误消息:

{ 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' }

如何通过 plesk 根服务器发送电子邮件?

...如果我使用端口 465,我会收到此错误:

{ 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' }  

添加 sendmail: true (https://nodemailer.com/transports/sendmail/) 解决了问题:

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
  }
});