yads / nodemailer-express-handlebars

A plugin for nodemailer that uses express-handlebars view engine to generate emails
87 stars 30 forks source link

Template seems to not fill up correctly #18

Closed Prechan closed 6 years ago

Prechan commented 6 years ago

Hey !

I experienced an ankward problem here, and after hours trying things to fix it I ask some help ! Some received emails are incompleted as strings and/or variables are missing. I think that the email is send before context variable is entirely build up. I tried to wrap in async/await, but same result.

To help you understand my problem, here is how it works in code:

    // Retrieve locales from JSON local file, based on language parameter.
    var locales = loadLocales(lg);

    // Build context with locales and parameters.
    var context = {
        TITLE: locales.ACCOUNT_GRANTED.TITLE,
        YOUR_ACCOUNT_IS_CREATED: locales.ACCOUNT_GRANTED.YOUR_ACCOUNT_IS_CREATED,
        LOGIN: locales.ACCOUNT_GRANTED.LOGIN,
        userLogin: userLogin,
        TO_ACCESS_YOUR_ACCOUNT: locales.ACCOUNT_GRANTED.TO_ACCESS_YOUR_ACCOUNT,
        accountLink: config.link,
        ACCESS_MY_ACCOUNT: locales.ACCOUNT_GRANTED.ACCESS_MY_ACCOUNT,
        OUR_TEAM_IS_UP: locales.ACCOUNT_GRANTED.OUR_TEAM_IS_UP,
        teamPhone: teamPhone,
        OR_BY_EMAIL_AT: locales.ACCOUNT_GRANTED.OR_BY_EMAIL_AT,
        teamEmail: teamMail,
        TEAM: locales.SHARED.TEAM
    };

    // Call sendMail function (classic Nodemailer SMTP Transporter)
    mailer.sendMail(options, 'account_granted', receivers, locales.ACCOUNT_GRANTED.TITLE, context);

``

In the received email, TITLE, OR_BY_EMAIL_AT and TEAM keys are displayed, others are not. When I do a console.log(context), just before mailer.sendMail, the email send is correct.

Do you have an idea about this ? Thank you !

yads commented 6 years ago

As long as the other values are not promises it should just work. I suspect your other values are not resolved at the time of your call to mailer.sendMail. Ensure all the values are resolved before you make that call. Additionally double check that the context keys match the template.

Prechan commented 6 years ago

After tried multiples things and triple check context keys, I wasn't able to fix it !

So I tried to change templating engine, I switched to EJS, without changing the way to get values (locales etc...), and it works fine... I'm sorry because your middleware was perfect otherwise.

That kind of bug...

yads commented 6 years ago

Yeah not sure, what the problem may have been without seeing your template. This is likely an issue either with the template or with https://github.com/ericf/express-handlebars. Feel free to open an issue there as well.