0

I writed this code in node.js - visual studio code, the database is mongoDB atlas (the code sends mail with token details after login):

function sendMail(email, name, tkn) {
    let transporter = nodemailer.createTransport({
        service: 'workers',
        auth: {
            user: process.env.EMAIL_ADDRESS,
            pass: process.env.EMAIL_PASSWORD
        }
    });

    let mailOptions = {
        from: process.env.MAIL,
        to: email,
        subject: `welcome ${name}`,
        text: `your token is ${tkn} `
    };
transporter.sendMail(mailOptions, function (err, info) {
    if (err) {
        console.log(err);
    } else {
        console.log('email sent:' + info.response);
    }
});
}

const checkPermission = async (req, res) => {
    try {
        /*let employed = */await Employed.findOne({
            email: req.body.email,
            password: req.body.password
        }).then((employed) => {
                const token = jwt.sign({ id: employed._id }, process.env.ACCESS_TOKEN_SECRET)
                sendMail(employed.email, employed.full_name, token);
                res.status(200).send(`successfull login ${employed}`)
            })
    } catch (err) {
        res.status(400).send(`error: ${err.message}`);
    }
}

this is the postman:

localhost:4000/checkPermission

it runs but the mail is not sended and i get this error in the terminal:

Error: connect ECONNREFUSED 127.0.0.1:587
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
  errno: -4078,
  code: 'ESOCKET',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 587,
  command: 'CONN'
}

do you know what the problem is? thanks ahead!

yael h
  • 3
  • 4
  • 1
    ECONNREFUSED typically means there is no process listening on that machine on that port or the firewall does not allow to connect to that port. Is there a mailserver up and running on your local machine? – derpirscher Jun 13 '21 at 14:40
  • i installed npm nodemailer – yael h Jun 13 '21 at 14:45
  • and write app.listen(4000, () => { console.log("listening on port 4000!!") }) and itws listening – yael h Jun 13 '21 at 14:46
  • 1
    `nodemailer` is not a mailserver. It's a library that connects to an SMTP server you specify and sends mails via that server. And as you seemlingly didn't provide any server address it tries to connect to a SMTP server on localhost – derpirscher Jun 13 '21 at 14:47
  • Does this answer your question? [Error: connect ECONNREFUSED 127.0.0.1:465 nodemailer](https://stackoverflow.com/questions/38024428/error-connect-econnrefused-127-0-0-1465-nodemailer) – derpirscher Jun 13 '21 at 14:50
  • it listens on port 4000, this is the code: const connectionParams = { newUrlParser: true, useCreateIndex: true, useUnifiedTopology: true/*, useFindAndModify:false*/ } mongoose.connect(process.env.DB_CONNECT, connectionParams) .then(() => { console.log("connected successfuly!!") }).catch((err) => { console.log("error connection") }) app.use(express.json()); app.use('/', router); app.listen(4000, () => { console.log("listening on port 4000!!") }) – yael h Jun 13 '21 at 14:51
  • ^ i check it now – yael h Jun 13 '21 at 14:52
  • Yes you have some express HTTP server listening on port 4000. But what does this have to do with your nodemailer trying to connect to an SMTP server at '127.0.0.1:587` For sending emails, you have to use an SMTP server. It's quite unlikely, that you have one your installed on your local machine ... – derpirscher Jun 13 '21 at 14:52

0 Answers0