【问题标题】:Email doesn't get sent with Gmail smtp using Nodejs使用 Nodejs 无法通过 Gmail smtp 发送电子邮件
【发布时间】:2020-05-30 16:31:48
【问题描述】:

我正在尝试使用 nodejs 发送电子邮件 (Gmail) smtp,但出现错误:

我的电子邮件和密码正确我正在使用 nodemailer 发送邮件。这是我的代码:

    //send email to confirme gain
    var smtpTransport = nodemailer.createTransport({
        service : 'Gmail',
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        auth : {
            user : config.supportemail,
            pass : config.gmailPSW
        }
    });
    var mailOptions = {
        to : gain.emailAccount,
        from : config.supportemail,
        subject : "FATBOAR : Vous avez gagné ! ",
        text : "Félicitations ! \n \n Votre compte : "+gain.emailAccount+" a bien gagné : "+gain.libelleGain+" grace à votre ticket de caisse N°: "+gain.numTicket+". \n \n Présentez-vous à notre équipe Fatboar avec ce mail ou avec l'application Fatboar pour récupérer votre gain. \n \n Nous vous remercions pour votre visite et espérons vous revoir très prochainement pour plus de cadeau. \n \n P.S : Vous avez participé automatiquement au tirage au sort pour gagner une voiture Range Rover Evoque. \n \n Bonne chance et à bientôt ! \n \n L'équipe FATBOAR"
    }
    smtpTransport.sendMail(mailOptions,function(err){
        if(err){
            res.status(400);
            resultats = {
                "success": false,
                "message": 'Error :5 '+ err,
                "result": ''
            }
            res.json(resultats);
        }
        else{
            res.status(200);
            resultats = {
                "success": true,
                "message": "SUCCESS",
                "result": gain
            }
            res.json(resultats);
        }
    });

这是我的 .env 文件:

#GMAIL
SECRETKEY="xxxx......="
GMAILPSW="XXXXX"
SUPPORTEMAIL="xXX.XxXXXxXXxxXx@gmail.com"

我允许访问安全性较低的应用,并激活了两步验证。 尽管它没有提供任何东西。

【问题讨论】:

    标签: node.js smtp nodemailer


    【解决方案1】:

    首先,您需要在 Gmail 中生成 App 密码。 进入email->security 并生成它。它应该看起来像:abjsdwqrjdsdxwfx

    如果需要,请在环境变量文件.env 中使用 gmailPWD“Gmail 密码”设置它并使用 require('dotenv').config(); 加载它们

    在这个例子中,我使用Mailgen来生成响应HTML模板,你应该安装并导入上面的包:const Mailgen = require('mailgen');

    let transporter = nodemailer.createTransport({
            service:"Gmail",
            secure:true,
            auth:{
                user: process.env.supportemail,
                pass: process.env.gmailPSW
            }
        });
    
    const emailSend= async(userEmail,user) => {
        try{
            let mailGenerator =  new Mailgen({
                theme:"default",
                product:{
                    name:"Name",
                    link:`${process.env.YOUR LINK CLIENT }`
                }
            });
    
    const email = {
                body:{
                    name:userEmail,
                    intro: 'Text',
                    action:{
                        instructions: 'Text',
                        button:{
                            color:'#1a73e8',
                            text: 'Valideaza-ti contul.',
                            link: `${process.env.SITE_DOMAIN}` //link for redirect 
                        }
                        
                    },
                    outro: 'if u need help....'
                }
            }
            let emailBody = mailGenerator.generate(email);
            let message = {
                from: process.env.EMAIL,
                to:userEmail,
                subject:"welcome",
                html: emailBody
            };
    
            // sending the email
            await transporter.sendMail(message);
            return true;
        } catch(error){
            throw error;
        }
    } // export the module
    module.exports = {
        emailSend,
    }
    

    【讨论】:

      【解决方案2】:

      如果您已激活双因素身份验证 请查看本指南:

      https://galleryserverpro.com/use-gmail-as-your-smtp-server-even-when-using-2-factor-authentication-2-step-verification/

      也许您需要改用“谷歌应用密码”。

      更多官方信息在这里: https://support.google.com/mail/answer/185833?hl=en

      也许这有帮助:)

      【讨论】:

        猜你喜欢
        • 2021-07-06
        • 2020-02-21
        • 2017-05-12
        • 2016-01-16
        • 2011-08-11
        • 2011-02-08
        • 2012-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多