Nodemailer SMTP Server receives e-mail but doesn't respond anything

Viewed 441

I have got a simple script for receiving e-mails, even though it receives e-mails and prints ok, unfortunately, doesn't respond to sending server, (no 250OK) as a result sending server keeps sending the same e-mail (retrying)

What is required to respond or what might be wrong?

In this setup, this code running in my local network (OsX), my router's port 25 forwarded to my machine.

const SMTPServer = require("smtp-server").SMTPServer;
const parser = require("mailparser").simpleParser

const server = new SMTPServer({

  onData(stream, session, callback) {
    parser(stream, {}, (err, parsed) => {

      if (err){
        console.log("Error:" , err)
      }
      
      eMailParse(parsed)
      stream.on("end", callback)

    })
    
  },
  disabledCommands: ['AUTH']

});

server.listen(25, "192.168.50.20")

console.log("Mail Server Running");


function eMailParse(parsed){
    const {headers, from, to, cc, date, html, text, messageId} = parsed;

    console.log("\n//############# New Mail #############\n")
    console.log(messageId)//Unique Mail ID;
    console.log("\n//$$$$$")
    console.log(from)
    console.log(to)
    console.log(cc)
    console.log(date)
    console.log(text)
    console.log("//====")
    console.log(html)
}

Gmail's response

The recipient server did not accept our requests to connect. Learn more at https://support.google.com/mail/answer/7720 [dc-133a13304d09.*******.fun. nn.nnn.nn.nn: timed out]

Telnet response

421 Timeout - closing connection Connection closed by foreign host.

2 Answers

After a while, I tried a while more and I've found the solution.

This part is not working

eMailParse(parsed)
stream.on("end", callback) //on("end") is never triggered

Instead;

eMailParse(parsed)
callback()

Works like a charm.

I had the same problem recently. I found this thread and the answer worked for me too, but later I found the real problem - the listener for the 'end' event should be attached immediately in onData callback, not in the parser callback. Here's the solution:

const SMTPServer = require("smtp-server").SMTPServer;
const parser = require("mailparser").simpleParser

const server = new SMTPServer({

  onData(stream, session, callback) {
    stream.on("end", callback)
    parser(stream, {}, (err, parsed) => {
      if (err){
        console.log("Error:" , err)
      }
      
      eMailParse(parsed)
    })
  },
  disabledCommands: ['AUTH']
});

server.listen(25, "192.168.50.20")

console.log("Mail Server Running");


function eMailParse(parsed){
    const {headers, from, to, cc, date, html, text, messageId} = parsed;

    console.log("\n//############# New Mail #############\n")
    console.log(messageId)//Unique Mail ID;
    console.log("\n//$$$$$")
    console.log(from)
    console.log(to)
    console.log(cc)
    console.log(date)
    console.log(text)
    console.log("//====")
    console.log(html)
}
Related