简体   繁体   English

Nodemailer 未发送电子邮件,但未发生错误

[英]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.我目前正在为我的网站进行注册确认 email function,我以前做过类似的事情。 Only this time I am running into a problem;只有这一次我遇到了问题; The emails aren't sending, while the package returns no error.电子邮件未发送,而 package 未返回任何错误。

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.运行此代码时,控制台中会记录success

Before replying, keep in mind that I already am:在回复之前,请记住我已经是:

  • using the google 16-character password.使用 google 16 个字符的密码。
  • using real values, just replaced them for privacy reasons.使用真实值,只是出于隐私原因替换它们。

EDIT: Code works when put at the end of my main file (index.js):编辑:代码放在我的主文件(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):但是,代码在我的模块中不起作用(记录了“set”参数,但没有进一步发生,然后我的进程似乎也停止运行,express 停止回复请求):

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

}

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:我理解你的问题,我看到一个永无止境的 while 循环可能会导致问题,所以如果你需要成功发生一些事情,比如res.sendStatus(200) (因为你提到了 express )只需将“回调”function 传递给你的功能是这样的:

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)
           }
    }}
})

You can also try this method to create Transport for Gmail您也可以尝试使用此方法为 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.我认为您的 email 已根据 Google 规则进入垃圾邮件文件夹。

If the messages are not in the spam folder, you may need to lower your Gmail security settings.如果邮件不在垃圾邮件文件夹中,您可能需要降低 Gmail 安全设置。

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

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

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.当 email 在导出的 function ( module.exports = () => {} ) 中时,它似乎根本无法工作,当我将它直接放在文件中而不是要求并调用它作为外部 function 时,它起作用了.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM