Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
How to send response from Nodemailer

I have an app written with React as the frontend, and Express as the backend. I'm using Nodemailer to email out the results of a form. It hits the backend and sends out just fine, but I'm looking for the proper way to send a response back to the front end so that I can alert the user if there's an error, or redirect to a success page if it goes through fine. I've tried both using a callback function and omitting it in my transporter.sendMail() code and neither have worked. I don't really know what I'm doing, and could use some help.

Transporter Code (Wrapped inside my sendMail function)

transporter.sendMail(mailOptions, (err, info) => {
            if (err) {
                console.log(err)
                res.sendStatus(500)
            } else {
                console.log(info)
                res.sendStatus(201)
            }
        })

Server Code

app.post('/sendApplication', cpUpload, async (req, res) => {
    return sendMail(req.files['essay'][0], req.files['recLetter1'][0], req.files['recLetter2'][0], req.body)
})

Form Submit Code

handleSubmit = async (e) => {
    e.preventDefault()
    let formData = new FormData()
    for (let i in this.state.formValues) {
        formData.append(i, this.state.formValues[i])
    }
    formData.append('essay', document.getElementById('essay').files[0])
    formData.append('recLetter1', document.getElementById('recLetter1').files[0])
    formData.append('recLetter2', document.getElementById('recLetter2').files[0])
    await fetch('/sendApplication', {
        method: 'POST',
        body: formData
    }).then((res) => {
        if (res.ok) {
            window.localStorage.removeItem('applicationData')
            this.props.history.push('/success')
        } else {
            alert('Error Message')
        }
    })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First, you need to add (req,res,next) in you controller function to use res function.

After this, you will be able to return res to the client.

Try this :

const sendMail = (res,req,next) => {
     
      /// 
transporter.sendMail(mailOptions, (err, info) => {
            if (err) {
                console.log(err)
                res.status(500).send({err : err})
            } else {
                console.log(info)
                res.status(200).send({success : info})
            }
        })
      ///
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda