2

I have been looking during hours for an answer to my question. I think this question (Send email to localhost smtp server in Node.js) is quite the same but the answer didn't help me.

So I have two VM's which run on Centos7. And I want my VM 1 to send an email to my VM 2. Here is what I have done :

VM 1 : Postfix is installed and configured with it : relayhost = [ip of vm 2]:25

VM 2 : The port 25 is opened. There is the code which listen in port 25 :

'use strict'

const fs = require('fs-extra') 
const SMTPServer = require('smtp-server').SMTPServer
const simpleParser = require('mailparser').simpleParser
const date = require('date-and-time')
const builder = require('xmlbuilder') //9.0.4

const say = msg => {
            console.log(msg)
}


const server = new SMTPServer({
  logger: true,
  //secure: true,
  //authOptional: true,
  //disabledCommands: ['AUTH'],
  disabledCommands: ['AUTH', 'STARTTLS'],

  // By default only PLAIN and LOGIN are enabled
  authMethods: ['PLAIN', 'LOGIN', 'CRAM-MD5'],

  onConnect: function(session, callback){
    say('hello')
    callback()
  },
  onData: function(stream, session, callback) {
    say('received');
    callback()
  },

 })


server.on('error', err => {
  say('Error %s', err.message)
})

server.listen(25)

From VM 2 : When I send a mail to locahost with sendmail :

sendmail ldecaudin@localhost < email.txt

, I can see the "hello" and the "received"

From VM 1 : When I send a mail with sendmail (which is automatically relayed by postfix to my VM2) using it :

sendmail ldecaudin@[192.168.56.101] < email.txt

I can only see the "hello", so the connection is running, but I am not able to pass in the "onData" function to get the stream I need.

Also from my VM1, I have a node code which send mails using nodemailer, this code works. When I use this code, I have the same result ("hello" but not "received"), but there are 2 connections instead of one.

So I am totally lost, I tried to add the "onAuth" function, to try many options of "new SMTPServer".

I wonder if this is a problem with a port (but port 25 is open!), or maybe I forgot to put something on Postfix. I don't know.

Can you help me please ?

Thank you a lot by advance !

1 Answers1

1

I might be late to answer but I faced a similar issue a couple of hours ago writing this answer. Answering in a hope that somebody will find it helpful in the future...

As per the SMTP standards, it sends mailform, rcptto commands before actually sending the body.. and hence, here in this nodeJs lib, the handler methods equivalent to those commands mailfrom -> onMailFrom(...) & rcptto -> onRcptTo(...) is getting called and ARE EXPECTED TO CALL callback() in order to send success signal on the connected SMTP socket.

In my case, I wrote an empty handler and was not calling the callback on onMailFrom & onRcptTo. This was creating the problem.

Calling the callbacks from the above handlers fixed it.

Code sample when I faced the issue:

.
.
.
onMailFrom: (address, session, callback) => {},
onRcptTo: (address, session, callback) => {},
.
.

Code sample when I called callback:

.
.
.
onMailFrom: (address, session, callback) => {
    return callback();
},
onRcptTo: (address, session, callback) => {
    return callback();
},
.
.
.

Adding a note: Do check on other handler functions as well, if this solution doesn't work. Debugging helped me here to identify the problem in my case.

miiiii
  • 1,580
  • 1
  • 16
  • 29