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

367
Visualizações
Nodemailer is not sending e-mail, but no errors occur

I am currently making a sign-up confirmation email function for my website, and I have done something like it before. Only this time I am running into a problem; The emails aren't sending, while the package returns no error.

I took the code out of context and let it e-mail me as soon as it launches, and even then it doesn't work (while saying it does)

My code (out of context one):

const nodemailer = require('nodemailer')

const mailTransporter = nodemailer.createTransport({
    host: `smtp.gmail.com`,
    port: 465,
    secure: true,
    auth: {
        "user": "<theemail>@gmail.com",
        "pass": "<thekey>"
    }
})

mailTransporter.sendMail({
    from: `<the-email>@gmail.com`,
    to: `<myemail>@gmail.com`,
    subject: `some subject`,
    text: `some text`
}, (err, data) => {

    if (err)
        console.log(err)
    else
        console.log(`success`)

})

When this code is run, success is logged in the console.

Before replying, keep in mind that I already am:

  • using the google 16-character password.
  • using real values, just replaced them for privacy reasons.

EDIT: Code works when put at the end of my main file (index.js):

const nodemailer = require('nodemailer')

const mailTransporter = nodemailer.createTransport({
    host: `smtp.gmail.com`,
    port: 465,
    secure: true,
    auth: {
        user: "<email>@gmail.com",
        pass: "<pass>"
    }
})

mailTransporter.sendMail({
    from: `<sendemail>@gmail.com`,
    to: `<myemail>@gmail.com`,
    subject: `some subject`,
    text: `some text`
}, (err, data) => {

    if (err)
        console.log(err)
    else
        console.log(`success`)

})

However, the code does not work inside my module (the 'set' parameter is logged, but nothing further happens, my process then also seems to stop running, express stops replying to requests):

module.exports = (set) => {

    let success = undefined

    console.log(set)

    const mt = nodemailer.createTransport({
        host: `smtp.gmail.com`,
        port: 465,
        secure: true,
        auth: {
            user: "<email>@gmail.com",
            pass: "<pass>"
        }
    })
    
    mt.sendMail({
        from: `<sendemail>@gmail.com`,
        to: `<myemail@gmail.com`,
        subject: `some subject`,
        text: `some text`
    }, (err, data) => {
    
        if (err)
            console.log(err)
        else
            console.log(`success`)
    
    })

    

    while (success == undefined) {}

    return success

}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Ad i understand your problem ,I see a never ending while loop there maybe that causes the problem so if you need something to happen in success like res.sendStatus(200) (since you have mentioned express ) just pass a 'callback' function to your funtion like this:

module.exports = (set,callback) => {

    let success = undefined

    console.log(set)

    const mt = nodemailer.createTransport({
        host: `smtp.gmail.com`,
        port: 465,
        secure: true,
        auth: {
            user: "<email>@gmail.com",
            pass: "<pass>"
        }
    })
    
    mt.sendMail({
        from: `<sendemail>@gmail.com`,
        to: `<myemail@gmail.com`,
        subject: `some subject`,
        text: `some text`
    }, callback)

}

And in your router:

router.post("sendEmail",(req,res)=>{
send(yourset,(err, data) => {
        if (err)
            console.log(err)
            res.sendStatus(500)
        else{
            console.log(`success`)
            res.sendStatus(200)
           }
    }}
})
about 4 years ago · Juan Pablo Isaza Relatório

0

You can also try this method to create Transport for Gmail

transport = nodemailer.createTransport({
                    service: 'Gmail',
                    auth: {
                        user: setting.mailService.user,
                        pass: setting.mailService.pass,
                    }
                })

And I think your email has gone to the spam folder according to Google rules.

If the messages are not in the spam folder, you may need to lower your Gmail security settings.

https://nodemailer.com/usage/using-gmail/

https://www.google.com/settings/security/lesssecureapps

about 4 years ago · Juan Pablo Isaza Relatório

0

It appears that the email was simply not working when it was inside of an exported function (module.exports = () => {}), when I put it directly in the file instead of requiring and calling it as external function, it worked.

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