Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

376
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda