ETIMEDOUT connect error using nodemailer in Nodejs Openshift application
Asked Answered
S

5

12

I'm facing some problems using the nodemailer module in my node.js single gear non-scalable application in Openshift. As the documentation suggested, I initialized the transporter object and used the sendMail function. A simplified version of my code is

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: '[email protected]',
        pass: 'mypassword'
    }
});

var mail = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Test mail',
    html: 'Test mail'
};

transporter.sendMail(mail, function(error, info) {
    if(error){
        console.log(error);
    }else{
        console.log(info);
    }
});

This code works correctly when I run it on my local machine, but when I try to execute it on the server I got an ETIMEDOUT error, as if the application couln't connect to the smtp server.

{ [Error: connect ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect' }

I've tried to increase the timeout connection parameter, but I got the same results.

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: '[email protected]',
        pass: 'mypassword'
    },
    connectionTimeout: 5 * 60 * 1000, // 5 min
});

Is there any firewall or default Openshift settings or enviroment variable I'm missing?

Sommer answered 17/7, 2015 at 10:4 Comment(0)
S
11

I've to run a few more tests, but I think I figured out what the problem was. The authentication via username and password is a not-secure authentication method for Gmail. As written in the Nodemailer documentation

Even though Gmail is the fastest way to get started with sending emails, it is by no means a preferable solution unless you are using OAuth2 authentication. Gmail expects the user to be an actual user not a robot so it runs a lot of heuristics for every login attempt and blocks anything that looks suspicious to defend the user from account hijacking attempts. For example you might run into trouble if your server is in another geographical location – everything works in your dev machine but messages are blocked in production.

I upgraded my authorization method using XOAuth2, as well explained in this guide and now the mails are correctly sent.

Sommer answered 17/7, 2015 at 14:21 Comment(0)
B
5

In my case the problem is with the port I have changed port from 25 to 2525 and it worked.

try changing port

Blackmun answered 18/6, 2021 at 5:19 Comment(1)
Thanks! In my case, I change the port from 25 to 587Sibella
S
2
const transporter = await nodemailer.createTransport(smtpTransport({
    service: "Outlook365",
    host: "smtp.office365.com",
    port: "587",
    tls: {
        ciphers: "SSLv3",
        rejectUnauthorized: false,
    },
    auth: {
        user: '***',
        pass: '***'
    }
}));

It works for outlook

Stepfather answered 3/2, 2022 at 11:2 Comment(0)
H
1

If you're using Office 365 (Outlook), make sure to enable SMTP authentication. Please refer to the link below for instructions on how to do this.

SMTP Authentication Enable

https://hk.godaddy.com/en/help/enable-smtp-authentication-40981

Hertel answered 4/11, 2023 at 10:57 Comment(0)
A
0

If you are trying to run this from the localhost.

Add the following line of code after the auth option

 tls:{
        rejectUnauthorized:false
    }

It will work but you might consider upgrading to XOAuth2 as suggested by @matt.kiwi

Allison answered 29/9, 2020 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.