nodemailer / smtp-server

Create custom SMTP servers on the fly
Other
846 stars 145 forks source link

stream.sizeExceeded documentation update #125

Closed niftylettuce closed 5 years ago

niftylettuce commented 5 years ago

I think that you should update the docs so that the example at https://nodemailer.com/extras/smtp-server/#using-size-extension is something like this instead:

onData(stream, session, fn) {

  stream.on('data', () => {
    if (stream.sizeExceeded) {
      const err = new Error('Message size exceeds maximum of XYZ');
      err.responseCode = 450;
      stream.destroy(err);
    }
  });

  stream.once('end', () => {
    fn();
  });

  stream.once('error', fn);
}

The reason being is because the stream should stop as soon as the size is detected to be over the limit, otherwise it will parse the entire file. Correct me if I'm wrong?

Many thanks as always @andris9.

andris9 commented 5 years ago

sizeExceeded is informational and actual behavior is up to the actual app to decide. You have to read the entire input stream as per SMTP protocol you can only respond with an error once the client terminates the message. Instead of destroying the stream, keep reading but do not process the chunks. Once message is finished, return the error.