zone-eu / zone-mta

📤 Modern outbound MTA cross platform and extendable server application
European Union Public License 1.2
599 stars 96 forks source link

fix error handling on smtp:data hook + add sessionId to all logs #290

Closed matteomattei closed 2 years ago

matteomattei commented 2 years ago

If I throw an error inside the smtp:data hook, the client stuck and after a long time (~10 minutes) exits for timeout instead of the actual message I want to return:

    app.addHook('smtp:data', (envelope, session, next) => {
        console.log('================================');
        console.log('SMTP:DATA', envelope, session);
        console.log('================================');
        let error = new Error('Generic error');
        error.responseCode = 535;
        return next(error);
    });

This is the client (using nodemailer):

Error: Timeout
    at SMTPConnection._onTimeout (/home/matteo/src/test/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:847:30)
    at Socket.SMTPConnection._onSocketTimeout (/home/matteo/src/test/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:193:44)
    at Socket.emit (events.js:400:28)
    at Socket._onTimeout (net.js:495:8)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7) {
  code: 'ETIMEDOUT',
  command: 'CONN'
}

It is necessary to close (destroy) the stream to unlock the process and forward the error message correctly to the client.

UPDATE: 2022-02-17

I also added the sessionId (id) to all logs for legal compliance.

andris9 commented 2 years ago

Thanks!