Problem with Netlify function after moving domain to netlify

Hello,
I have a problem with netlify function which sends an email.
It has been working before but after I moved the domain from my hosting to a netlify it gives me this kind of error:

ERROR  Invoke Error 	{"errorType":"Error","errorMessage":"connect ECONNREFUSED 127.0.0.1:465","code":"ESOCKET","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":465,"command":"CONN","stack":["Error: connect ECONNREFUSED 127.0.0.1:465","    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)"]}

I added MX records to a DNS configuration, I know it might take some time to update them, but basically, it is already passed 24hours.
I’m not sure if this error is related to DNS configuration or maybe I’m just missing something else.

which site is this regarding, @reginatrade ? we can check your DNS settings if we know where to look, thanks!

Thank, it’s regina.lv

Hi @reginatrade

I wonder how you are trying to send the email. Are you trying to send it from localhost / 127.0.0.1?

Can you share your code?

No, I deployed it to Netlify, email worked perfectly before moving the domain to Netlify.

exports.handler = async (event, context, callback) => {

    const payload = JSON.parse(event.body);

    const transporter = nodemailer.createTransport({
        host: process.env.MAIL_HOST,
        port: 465,
        secureConnection: true,
        auth: {
            user: process.env.MAIL_USERNAME,
            pass: process.env.MAIL_PASSWORD
        }

    });

    const message = {
        to: process.env.MAIL_USERNAME,
        from: payload.email,
        subject: `Regina.lv - New message ${payload.name} | ${payload.email}`,
        text: payload.message
    };

    const info = await transporter.sendMail(message);

    if (info.err) {
        return {
            statusCode: 400,
            body: info.err.message
        };
    }

    return {
        statusCode: 200,
        body: 'Message sent'
    };
};

So you are using Netlify DNS for regina.lv, that I can see. I also see you have mail.regina.lv set up (which I might assume is the process.env.MAIL_HOST value), however it appears to point to nowhere.

Which email provider do you use (Zoho, Google, Microsoft, etc)?

Your function looks almost identical to the one I use so cannot see how it would error otherwise.

Hello, thank you for checking,. i"m using my domain provider, email server.

I tried to run it locally and got this error message:

Error during invocation:  Error: getaddrinfo ENOTFOUND mail.regina.lv
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)

As well I forgot to mention, that I changed Name servers on my Hosting to Netlify provided ones.

I suggest your mail domain is not configured correctly as there are no records for it.

Can you take a screenshot of the records you have in Netlify DNS?

Ok, so that is technically correct. But, mail.regina.lv is a non-existent. Here are some of my MX records

Keep in mind too, Netlify does not provide mail. This Support Guide explains more (ie. you need to use an external service such as Zoho for email.)

I checked my email configurations and saw this

so I guess instead of mail.regina.lv in VALUE i should just put regina.lv

regina.lv is in Netlify DNS pointing to a Netlify website, and as Netlify does not provide email service (as per previously linked Support Guide, your MX destination needs to point to a third-party service (such as Zoho) in order for you to send and receive email using your domain. The service you use will dictate what DNS records you need to configure and what settings you will use to send/receive email.

Thanks for the suggestions, i changed my mail provider to Zoho, manage to set-up everything and it works fine in development, but in production still this weird error:

ERROR Invoke Error {"errorType":"Error","errorMessage":"connect ECONNREFUSED 127.0.0.1:465","code":"ESOCKET","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":465,"command":"CONN","stack":["Error: connect ECONNREFUSED 127.0.0.1:465"," at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)"]}

What is the value of your environment variable MAIL_HOST?

MAIL_HOST='smtp.zoho.eu'

Ok, looking at your script again, I suspect the issue is perhaps

secureConnection: true,

It would seem that this option was used in 0.x releases (see here) however since 1.x it was changed to

secure: true,

This is evident in the example on Nodemailer website as well as on the Ethereal Email website.

What version of Nodemailer is in your package.json?

2 Likes

Wow, thanks, it worked, but I’m confused about why did it work in development mode?

Thank you soo much for helping out!

1 Like

:thinking: You could possibly dig a very big tunnel trying to find out! :thinking: