Search code examples
node.jssmtpnodemailer

nodemailer connect ECONNREFUSED


I'm working for my portfolio and I use nodemailer express and nodemon and I need to work for nodemailer. I don't know what is the problem here

// server scripts

const contactForm = document.querySelector('.cnt-form');
let message = document.getElementById('message');
let name = document.getElementById('name');
let email = document.getElementById('email');
let subject = document.getElementById('subject');
let phone = document.getElementById('phone');
contactForm.addEventListener('submit', (e)=>{
    e.preventDefault();
    let formData = {
        message: message.value,
        name: name.value,
        email: email.value,
        subject: subject.value,
        phone: phone.value
    }
    let xhr = new XMLHttpRequest();
    xhr.open('POST', '/');
    xhr.setRequestHeader('content-type', 'application/json');
    xhr.onload = function(){
        console.log(xhr.responseText);
        if(xhr.responseText == 'success'){
            alert('Email Sent Successfully');
            message.value = '';
            name.value = '';
            email.value = '';
            subject.value = '';
            phone.value = '';
        }else{
            alert('Something went wrong!');
        }
    }
    xhr.send(JSON.stringify(formData));
});

here is my code for client side

const express = require('express');
const nodemailer = require('nodemailer');
const app = express();


const PORT = process.env.PORT || 5000;

app.use(express.static('public'));
app.use(express.json())
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/public/index.html');
})
app.post('/', (req, res) => {
    console.log(req.body);
    const transporter = nodemailer.createTransport({
        service: 'smpt.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'xxxxxxxx',
            pass: 'xxxxxxxx'
            
        }
    })
    const mailOptions = {
        from: req.body.email,
        to: 'xxxx@mail.com',
        subject: `Message from ${req.body.email}:  ${req.body.subject}`,
        text: req.body.message,
        contact: req.body.phone
    }
    transporter.sendMail(mailOptions, (error, info)=>{
        if(error){
            console.log(error);
            res.send('error');
        }else{
            console.log('Email sent: ' + info.response);
            res.send('success');
        }
    })
});
app.listen(PORT, ()=>{
    console.log(`server running on port ${PORT}`);
});

and here is for my server side

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

and this code above is the error

I tried to change the port the secure and still not working, what should I do? thank you


Solution

  • I don't see service option in Nodemailer documentation.

    Change

     const transporter = nodemailer.createTransport({
            service: 'smpt.gmail.com',
            port: 465,
            secure: true,
            auth: {
                user: 'xxxxxxxx',
                pass: 'xxxxxxxx'
                
            }
    

    to

     const transporter = nodemailer.createTransport({
            host: 'smpt.gmail.com',
            port: 465,
            secure: true,
            auth: {
                user: 'xxxxxxxx',
                pass: 'xxxxxxxx' 
            }