'Nodemailer: My frontend website can't send emails through node mailer from a different IP address

My frontend portfolio has an emailing option which sends a post req to nodemailer in the backend. My portfolio is already deployed in firebase and I'm currently running the backend on the localhost server.

I face no issues when I send emails on my laptop, same IP address, while I receive an error whenever I try sending emails on a different device.

I have searched around for a day now and I can't find an available solution. Any idea on how I can solve this issue?

My nodemailer configuration:


const transporter = nodemailer.createTransport(smtpTransport({
    name: 'domainname',
    host: 'domainname.com',
    port: 443,
    secure: true,
    service: 'gmail',
    auth: {
        user: '*****@gmail.com',
        pass: '******' //Google's less secure apps option enabled.
    },
    tls: {
        // do not fail on invalid certs
        rejectUnauthorized: false,
    }
}))  

The following error is logged into the console when sending email fails:

POST http://localhost:4000/api/email/ net::ERR_CONNECTION_REFUSED

TypeError: Failed to fetch
    at main.1ec56b92.js:2:347746
    at c (main.1ec56b92.js:2:168378)
    at Generator._invoke (main.1ec56b92.js:2:168166)
    at Generator.next (main.1ec56b92.js:2:168807)
    at Ft (main.1ec56b92.js:2:332714)
    at i (main.1ec56b92.js:2:347506)
    at main.1ec56b92.js:2:347567
    at new Promise (<anonymous>)
    at main.1ec56b92.js:2:347446
    at main.1ec56b92.js:2:348157

Thank you



Solution 1:[1]

As Derpirscher pointed out, the host of the backend was hardcoded as localhost:4000 hence why it worked on my laptop and failed on my phone. The solution for me was to deploy the code to a server -like Heroku- and use its address to connect to the nodemailer API, eg: https://project.herokuapp.com/api/email instead of https://localhost:4000/api/email

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ismail