简体   繁体   中英

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

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?!

Just require ejs directly and use as per the example , eg 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});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM