Home > Software engineering >  Nodemailer is not sending e-mail, but no errors occur
Nodemailer is not sending e-mail, but no errors occur

Time:05-29

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: `<[email protected]`,
        subject: `some subject`,
        text: `some text`
    }, (err, data) => {
    
        if (err)
            console.log(err)
        else
            console.log(`success`)
    
    })

    

    while (success == undefined) {}

    return success

}

CodePudding user response:

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: `<[email protected]`,
        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)
           }
    }}
})

CodePudding user response:

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

CodePudding user response:

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.

  • Related