Alex Rivera | Logout

Can Express with EJS render HTML to a variable (so I can send as e-mail)?

Asked 2012-04-02T01:53:17.013
8

I am writing a nodejs application that will be sending html e-mail using emailjs. Basically I provide the html to send as a variable that I attach to the message.

Rather than build this variable using lots of string concatenation, I'd like to just render a view using express/ejs and save the contents to the variable.

So instead of doing:

messageHtml = '<html>'+ ....
message.attach({data: messageHtml, alternative: true});

I'd like to do something like:

messageHtml = render('emailTemplate.ejs', viewArgs);
message.attach({data: messageHtml, alternative: true});

Can this be done, and if so, how?!

Edit
Report

1 Answer

15

Just require ejs directly and use as per the example, e.g simplified usage (without caching):

var ejs = require('ejs')
  , fs = require('fs')
  , str = fs.readFileSync(__dirname + '/emailTemplate.ejs', 'utf8'); 

var messageHtml = ejs.render(str, viewArgs);

message.attach({data: messageHtml, alternative: true});
answered 2012-04-02T02:53:33.040

Your Answer