nodemailer smtp-server给421 mydomain.com你说得太早错误

问题描述

我正在使用http://nodemailer.com/extras/smtp-server/运行SMTP服务器以接受所有邮件提交。
邮件提交代理使用STARTTLS时,出现以下错误

5|producer  | [2020-10-09 07:28:52] DEBUG [#ff7cqlwi7rat6z2k] C: EHLO qa.mydomain.com
5|producer  | [2020-10-09 07:28:52] DEBUG [#ff7cqlwi7rat6z2k] S: 421 mydomain.com You talk too soon
5|producer  | [2020-10-09 07:28:52] INFO  [#ff7cqlwi7rat6z2k] Connection closed to 91.198.201.301

但是,仅在某些客户端上会发生这种情况,而我尝试使用其他一些工具,并且它将连接升级到TLS没问题。

下面是我的服务器配置选项。

SMTPServerOptions = { 
  secure: false,hideSTARTTLS:true,authOptional: true,debug: true,logger: true,onAuth,onData
}

if(conf.tls) {
  SMTPServerOptions.ca = fs.readFileSync('./certificates/chain.pem','ascii')
  SMTPServerOptions.key = fs.readFileSync('./certificates/privkey.pem','ascii')
  SMTPServerOptions.cert = fs.readFileSync('./certificates/cert.pem','ascii')
}
//creating new SMTP object
const server = new SMTPServer(SMTPServerOptions);

server.on('error',err => {
  error(err)
  throw err 
});

server.listen(conf.server_port);


解决方法

能够通过在 nodemailer smtp-server 模块中注释部分代码来解决这个问题。只是在这里发布,以便它可以帮助其他正在寻求相同答案的人。

某些 SMTP 客户端,例如我使用的客户端,在连接后不会等待服务器响应,而是向服务器发送 EHLO 或 HELO 命令。从模块的源代码来看,这些客户端被视为早期谈话者,并阻止连接以避免垃圾邮件。
评论超时函数并发出 connectionReady() 事件立即解决了问题。

     /**
     * Initiates the connection. Checks connection limits and reverse resolves client hostname. The client
     * is not allowed to send anything before init has finished otherwise 'You talk too soon' error is returned
     */
    init() {
        // Setup event handlers for the socket
        this._setListeners(() => {
            // Check that connection limit is not exceeded
            if (this._server.options.maxClients && this._server.connections.size > this._server.options.maxClients) {
                return this.send(421,this.name + ' Too many connected clients,try again in a moment');
            }
            // Keep a small delay for detecting early talkers
            //setTimeout(() => this.connectionReady(),100);

            // no need to detect the early talkers
            this.connectionReady();
        });
    }

如果需要时间,也禁用反向查找。

 // disabling the reverse lookup which will solve 'you talk so soon problem'
 this.options.disableReverseLookup = true;