Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ESOCKET error mean when I'm trying to send an email?

I'm trying to send an email using nodemailer.

In my LAN there is a SMTP server listening on port 25. If I use telnet, it works fine.

My js script is:

this.transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(JSON.stringify(error));
            return callback(error, null);
        }

        console.log(JSON.stringify(info));

        return callback(null, true);
});

It only prints: {"code":"ESOCKET","command":"CONN"}. What does it mean?

Thanks in advance

like image 991
Domenico Ventura Avatar asked Dec 18 '22 17:12

Domenico Ventura


1 Answers

Per https://github.com/nodemailer/nodemailer/issues/889#issuecomment-488267379 (and it's follow-ups):

same problem here. resolved it by using IP address as host, see https://nodemailer.com/smtp/#general-options

For us it seems to be related to throttling as circa 140 messages go through in a batch while the remainder get this error (and all are being sent to the same email address, so no issue re: bad email addresses). Changing to an IP didn't solve the issue (maybe because the SMTP is on AWS?).

What did eventually work for us was this - https://stackoverflow.com/a/55187729/235704

The below code change fixed the issue. Added this to the createTransport()

tls: {rejectUnauthorized: false}

Code:-

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    host: 'host',
    port: 25,
    secure : false, // true for 465, false for other ports
    auth: {
        user: 'user',
        pass: 'password'
    },
    tls: {
        // do not fail on invalid certs
        rejectUnauthorized: false
    },
});

It seems that, in our providers case, their certificates do not cover all of the IPs they are being served from on AWS.

like image 161
Campbeln Avatar answered Mar 26 '23 22:03

Campbeln