nodemailer / smtp-server

Create custom SMTP servers on the fly
Other
862 stars 146 forks source link

Stream end event never triggered leads to duplicates #187

Closed krm35 closed 1 year ago

krm35 commented 1 year ago

EDIT: the simpleParser triggers it that's why it's never triggered again.

Hello,

It seems that the stream doesn't trigger the end event , so the 250 OK isn't sent and services like protonmail try to resend the mail multiple times.

I call the callback after the parsing and it works but it shouldn't be done that way.

const {SMTPServer} = require("smtp-server");
const {simpleParser} = require("mailparser");

const server = new SMTPServer({
    onData(stream, session, callback) {
        simpleParser(stream, {}, (err, parsed) => {
            if (err) return callback(err);
            console(parsed);
            /*
            triggered in the simpleParser
            stream.on('end', () => {
                callback(null, 'OK')
            });
            */
            callback(null, 'OK')
        })
    },
    disabledCommands: ['AUTH']
});

server.on("error", () => {
});

server.listen(2525, "127.0.0.1");
reisenbauer commented 4 months ago

Hi there,

even without any parsing, the "end" event is not triggered. my code:

const SMTPServer = require("smtp-server").SMTPServer;
const server = new SMTPServer({
  secure: false,
  authMethods: [],
  disabledCommands: [ "AUTH" ],
  authOptional: true,
  maxClients: 10,
  hideSTARTTLS: true,
  onData(stream, session, callback) {
   stream.on('end', () => {
    console.log(" --- ENDE --- ");
    callback(null, 'OK')
   });
  },
});
server.listen(25);

an test Telnet Session:

220 ns03 ESMTP
ehlo blubb.at
250-ns03 Nice to meet you, xxxxxx
250-PIPELINING
250-8BITMIME
250 SMTPUTF8
MAIL FROM: <eee@blubb.at>
250 Accepted
RCPT TO: <data@blubb.at>
250 Accepted
DATA
354 End data with <CR><LF>.<CR><LF>
d
asd
as
das
d
.

-- hangs forever --

any hints?

thx, stefan

krm35 commented 4 months ago

try this lib to send an email instead of telnet: https://github.com/guileen/node-sendmail

reisenbauer commented 4 months ago

try this lib to send an email instead of telnet: https://github.com/guileen/node-sendmail

thats not the point - i even sent an E-Mail from Gmail to the script above, also raised a timeout, as the "end" event did not get triggered.

krm35 commented 4 months ago

You need to pipe the stream and disable hideSTARTTLS

const {SMTPServer} = require("smtp-server");

const server = new SMTPServer({
    secure: false,
    authMethods: [],
    disabledCommands: ["AUTH"],
    authOptional: true,
    maxClients: 10,
    // hideSTARTTLS: true, // with this at true it wont work idk why
    onData(stream, session, callback) {
        stream.pipe(process.stdout);
        stream.on('end', () => {
            console.log(" --- ENDE --- ");
            callback(null, 'OK')
        });
    },
});
server.listen(2525);
reisenbauer commented 4 months ago

confirmed. that did the trick. thanks!