0

When I try to send an e-mail using nodemailer I got an connection timeout error with code 'ETIMEDOUT', but when I try on my notebook there's no error, both using the same e-mail account and password.

This is the file 'mail.js':

const nodemailer = require('nodemailer');

const user = process.env.EMAIL;
const pass = process.env.EMAIL_PASS;


const transporter = nodemailer.createTransport({
    service: 'kinghost',
    host: 'smtp.kinghost.net',
    port: 587,
    secure: false,
    pool: true,
    auth: { user, pass }
});

module.exports = {
    sendMail(to, subject, html) {
        const mailOptions = {
            from: user,
            to,
            subject,
            html
        };

        console.log(user)

        transporter.sendMail(mailOptions, (err, info) => {
            if (err) {
                console.log(err)
            };

            console.log(`Mail to ${mail} has been sended`);
        });
    }
};
  • There is a similar problem posted elsewhere, which might be of help: https://stackoverflow.com/questions/31473292/etimedout-connect-error-using-nodemailer-in-nodejs-openshift-application – Hero Qu Nov 28 '19 at 12:37
  • I saw this problem, but in that problem was the gmail protection – Assis Duarte Nov 28 '19 at 12:56

2 Answers2

0

It might be because on a production website, you are on a server that has a different IP to your own, and perhaps your email service is running some filtering because it thinks it might be unintended behaviour.

You should maybe look into other secure ways of authenticating your email client, or perhaps see if you can register the IP of your production server (less effective).

aspirant_sensei
  • 1,568
  • 1
  • 16
  • 36
0

Hey programmer friend.

I went through the same situation using kinghost. Make sure you check your email provider's dashboard for an option called "SMTP INTERNATIONAL".

Quick fix: change your config

nodemailer.createTransport({
                host: **"smtpi.kinghost.net"**, // smtp international
                secure: true, // force port 465
                port: 465, // default port
                auth: {
                    user: process.env.EMAIL, //email
                    pass: process.env.EMAIL_PASS, //pass
                }
            });

Your ECS or EC2 container must run on a different source, for this reason, activate the international email in your provider and use the same to solve this problem.

Your ECS or EC2 container must run on a different source, for this reason, activate the international email in your provider and use the same to solve this problem.

For my application this worked.

Dharman
  • 30,962
  • 25
  • 85
  • 135