Problem in production with nodemailer

Hello,

I have encountered a problem with my app after deployed it, I’m using nodemailer package to send an email from a website. Everything works fine in development and I’m able to receive mail from the site, but in a production environment, i got a 404 error status on my request

the netlify site name is https://fanciful-cendol-d532d8.netlify.app

Front:

const contact = {
    initContact:function(){
        contact.listener();
    },

    dataForm:{
        contactForm : document.querySelector('.contact-form'),
        email : document.querySelector(".contact-email"),
        message : document.querySelector(".contact-text"),
        emailState : document.getElementById("input-hidden"),
        server_url : "/.netlify/functions/sendmail"

    },

    listener: function(){
         contact.dataForm.contactForm.addEventListener('submit', contact.actionListener.handleSubmit)
    },
    

    actionListener: {

        handleSubmit : (e) => {
            e.preventDefault();
            fetch(contact.dataForm.server_url,{
                method:"POST",
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(
                    {
                    datas: {
                        email: contact.dataForm.email.value,
                        message: contact.dataForm.message.value                  
                    }
                    }
                )
            }
            )

            .then(res => {

                if(res.ok){
                    res.json()
                    .then(data => {
                        console.log(data)
                        console.log(res)                
                        window.alert("Votre email a bien été envoyé")
                        contact.dataForm.email.value = '';
                        contact.dataForm.message.value = '';
                    })
                }
                else{
                    console.log(res)
                    console.log("There is a problem with nodemailer")
                    contact.errorResponse()
                }
            })
            .catch(function(error) {
                console.log('There is a problem with the fetch operation: ' + error.message);
                contact.errorResponse()
            });
        }   
    },

    errorResponse: function(){
        window.alert("Une erreur est survenue, merci d'utiliser le lien Linkedin")
        contact.dataForm.email.value = '';
        contact.dataForm.message.value = '';
    }
}
document.addEventListener('DOMContentLoaded', contact.initContact);

i have just changed the server_url from my localhost to

server_url : "/.netlify/functions/sendmail"

i found informations on this post, but not really sure its appropriate to my code, problem coub be from here
https://answers.netlify.com/t/netlify-function-for-sending-mail-with-nodemailer-doesnt-work-in-production/41148

back:

app.post('/',(req,res) => { 
    try{
        
        const transporter = nodemailer.createTransport({
            host: "smtp.ethereal.email",
            port: 465,
            secure: true,
            service:'gmail',
            auth:{
                user: process.env.GMAIL_USER,
                pass: process.env.PASSWORD,
            }
        })

        console.log(req.body.datas.email)
        const mailOptions = {
            from: req.body.datas.email,
            to: process.env.GMAIL_USER,
            subject: "email from my Portfolio",
            html: `You got a message from 
            Email : ${req.body.datas.email} <br>
            Message: ${req.body.datas.message}`,
        }
        
        transporter.sendMail(mailOptions, (error, info) => {
            res.status(200)
            res.json({email:"email sent"})
        });

    }catch(error){
        res.status(500)
        res.json({message: error})
    }
})

As i said, in developement i got no probleme, i recieve the email with all the informations, after some researchs and differents trials, with no success, i am asking some advices,
Please let me know if any other information needs in order to help solve my problem.
Thank you.

Hey @matthieu.dev.degny

It appears the sendmail function does not actually exist. Does the deploy log show the function was build and deployed alongside the rest of your site?

E.G.

What is the server.js file? It is not possible to run a server on Netlify. You can only use serverless functions

Can you share the repository you are deploying?

The server.js file is, as evidenced by the following line from the package.json

"start": "node server.js"

won’t (as mentioned previously) work as is on Netlify. Running a node server instance does not work.

You can use express in a serverless function as shown in How to run Express.js apps with Netlify Functions.

You don’t need to use Express JS. A less-than-perfect example written by myself is available here if you wish to use/reference it.

The server.js is this site is doing nothing. If you recall the discussion from here where you set the publish directory to public, this means that anything at the root of the repository is not deployed. Only the static assets inside the public directory are deployed and served.

The same with this new portfolio site, if you set the public directory as the publish directory in the build settings, the server.js file is ignored.

For this site, if you wish to have a function to send an email via nodemailer, create a netlify/functions directory at the root of the repository, and inside this place the function. Set the publish directory to public and Netlify will deploy this and automatically build and deploy the function(s).

As mentioned previously, you can use express js inside a serverless function if you desire.