Home > Back-end >  Is there a way one can send different emails to different people using nodemailer?
Is there a way one can send different emails to different people using nodemailer?

Time:12-09

I want to send an email to the user saying

hey u have successfully registered please refer the code below 234234

and an email to the owner saying

user 234234 has registered 
            let transporter = nodemailer.createTransport({
                service: 'gmail',
                port: 587,
                secure: false,
                requireTLS: true,
                auth: {
                    user: MYEMAIL,
                    pass: MYPASSWORD,
                }
            })

            const randomGenerator = Math.floor(100000   Math.random() * 900000)
        
            let mailOptions = {
                to: [
                            { name: "Receiver Name 1", address: "[email protected]" },
                            { name: "Receiver Name 2", address: "[email protected]" },
                    ], 
                subject: 'You have successfully registered, Please refer the code given below',
                text: `Your code is ${randomGenerator}`
            }
        
            transporter.sendMail(mailOptions, (err, info) => {
                if(err) console.log(err)
                else{
                    console.log('email sent'   info.response)
                }
            })

so far i've tried making an array of subject and text so that the email sends them orderwise, but instead, it sent the same email to the user and owner twice

CodePudding user response:

Just call .sendMail() more than once and feed it different addressees and different email content each time you call it. One call to .sendMail() has one set of addressees and one piece of email content. So, to send different content, use separate calls to .sendMail() and vary the inputs.

  • Related