Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars as an email template

I've just inherited a codebase, and it's using handlebars as an email templating language.

I've googled around to try and get more information, but I can't find anyone else doing this.

I was just wondering if anyone could supply me some documentation or search terms to look for. I had no idea you could even use handlebars like this!

Thanks,

Ollie


Email sender

// Send new account email 
function sendNewAccountEmail(expert) {   
  ...
  return handlebars.render('views/emails/newAccountEmail.handlebars', {
    name: `${expert.firstName} ${expert.lastName}`,
    layout: false,
    expert,
    url: `${LIVE_URL}/expert/reset/${expert.resetPasswordToken}`,   
}).then(email => new Promise((resolve, reject) => {
      sendmail({
        from: SEND_EMAIL,
        to: recipient,
        subject: '',
        text: email,
      }, (err, reply) => {
        ...
      });
    })); }

newAccountEmail.handlebars

Hi {{name}},

You now have access to RARA Survey tool!
You can now access your dashboard and assigned campaigns by going to the following link and creating a password:

Login URL: {{url}}

Thanks!

Influencer Team
like image 428
Ollie Avatar asked Nov 06 '17 16:11

Ollie


People also ask

How do I create a custom email template?

Hover over the template you want to start with and click Select. In the Create New Template pop-up, enter a name and click Save. Add or edit content blocks and styles to design your template as needed. To learn more, read Design an Email With the New Builder.

What are the types of email templates?

In Salesforce Classic, you can create four different types of email templates: text, HTML with Classic Letterhead, custom, and Visualforce. All of these email templates can include text, merge fields, and attached files.


2 Answers

To do the email sent based on files .hbs as templates, it's necessary the instalation using NPM of packages:

  1. nodemailer
  2. nodemailer-express-handlebars

It will be to set the host informations:

    var transport = nodemailer.createTransport({
        host: 'YOUR HOST',
        port: 'YOUR PORT',
        auth: {
            user: 'YOUR USER',
            pass: 'YOUR PASSWORD'
        },
        tls: {
            rejectUnauthorized: false
        }
    });

Now, we need configure the transport to it be able to use the template:

    transport.use('compile', hbs({    
        viewPath: 'YOUR PATH where the files are, for example /app/view/email',
        extName: '.hbs'
    }));



    exports.sendEmail = function (from, to, subject, callback) {

        var email = {
            from: 'YOUR FROM FOR EXAMPLE [email protected]',
            to: 'RECIPIENT',
            subject: 'SUBJECT',
            template: 'TEMPLATE NAME, DO NOT NEED TO PLACE  .HBS',
            context: {
                name: 'YOUR NAME',
                url: 'YOUR URL'
            }
        };

        transport.sendMail(email, function (err) {
            if (err) {
                return callback({ 'status': 'error', 'erro': err });
            }
            else {
                return callback({ 'status': 'success' });
            }
        })
    };
like image 101
Marcio.Torquato Avatar answered Sep 20 '22 03:09

Marcio.Torquato


Remember handlebars is just a template language. What your code is doing is it's taking a .handlebars template, passing some variables which will be populated in your template and compiling it down to html, which is your email variable. You then take that email html and use your sendmail function to actually send the email. You can see the full documentation here

like image 44
dzm Avatar answered Sep 19 '22 03:09

dzm