0

I am using nodemailer to send emails for users on registration. The function is as following:

const setAndSendMsg = (recipients, users, subject, template) => {
    const htmlFile = checkTemplate(template)
    const message = {
        from: mailServer.mailerObj.auth.user,
        bcc: recipients,
        subject: subject,
        // html: `<div><b>Hello</b></div>`,
        html: htmlFile,       
        context: {
            user: 'test'
        }
    }

    transport.sendMail(message, function(err, data) {
        if(err) {
            console.log(err);
            return err
        } else {
            console.log(data);
            return data
        }
    })
}

checkTemplate return the required template:

const checkTemplate = (template) => {
    return fs.readFileSync(path.resolve(__dirname, `../email-templates/${template}.html`), 'utf-8').toString()
}

The template contains:

<div>
    Dear {{user}},
    <br/>
    <div>
        Welcome to our platform, please click on the link below to activate your account:
        
    </div>
</div>

The email is well received to all recipients as follows:

Dear {{user}}, ...

without test being displayed in place of {{user}}

I used the script from this question on stackoverflow but it didn't work.

alim1990
  • 4,656
  • 12
  • 67
  • 130

1 Answers1

0

The problem is in your config, you're defining your template as static html, and therefore no variables are being evaluated there, if you go to the templating documentation of node-mailer here, you'll find that is not supported anymore, and instead they send you here. My suggestion would be to follow the official documentation, they offer good examples there.

Other way would be to manually render the template into a static html and set it to the mailer, that's if you don't want to install another package, but email-templates does this for you :)

Hope this helps.

Cheers :)

bretanac93
  • 627
  • 8
  • 20