NodeJS Why is not SMTP server work with secure option turned on?

Viewed 141
const fs = require('fs');
const {SMTPServer} = require('smtp-server');

const smtp = new SMTPServer({
    secure: true,
    key: fs.readFileSync('certs/privkey.pem'),
    cert: fs.readFileSync('certs/cert.pem'),
    onData,
    onRcptTo,
    authOptional: true,
});

function onRcptTo(address, session, callback) {
    console.log("IS HERE!!!");
    callback();
}

function onData(stream, session, callback) {
    stream.pipe(process.stdout);
}

smtp.listen(465);

Without secure option and post 25 accordingly all works, but not with it.

The certificate I got using Let's Encrypt through the Certbot. And also I tried to remain default certificate, but it doesn't work that way either. I tested it using mxtoolbox's tool and got the "Connection error" result

So, why is this happening?

1 Answers

You must try Error Handling to encounter what actually went wrong. Include and Rearrange the following code snippet:

const {SMTPServer} = require('smtp-server');
const fs = require('fs'); // must include after smtp-server 

const smtp = new SMTPServer({
    secure: true,
    key: fs.readFileSync('certs/privkey.pem'),
    cert: fs.readFileSync('certs/cert.pem'),
    onRcptTo, // call Rcpt callback first
    onData,
    authOptional: false // leave it optional
});

function onRcptTo(address, session, callback) {
    console.log("IS HERE!!!");
    callback();
}

function onData(stream, session, callback) {
    stream.pipe(process.stdout);
stream.on("end", callback); // must include a callback function
}
smtp.on("error", err => {
      console.log("Error %s", err.message);
    });

smtp.listen(465);
Related