Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way one can send different emails to different people using nodemailer?

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: "receiver1@example.com" },
                            { name: "Receiver Name 2", address: "receiver2@example.com" },
                    ], 
                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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading