I have an html template that we use to send to new website registrations. It is a simple html file that I would like to load into a variable so that I can replace certain parts before sending out using nodemailer (eg [FIRST_NAME]). I am trying to avoid having to paste a large chunk of html into my exports function. Any ideas on how I could go about doing that?
For a clearer idea, what I need to know is how to actually do this:
var first_name = 'Bob';
var html = loadfile('abc.html').replace('[FIRST_NAME]', first_name);
Here's an example of how to do it using ejs, but you can use any templating engine:
var nodemailer = require("nodemailer");
var ejs = require('ejs');
var transport = nodemailer.createTransport("SMTP", {
service: <your mail service>,
auth: {
user: <user>,
pass: <password>
}
});
function sendMail(cb) {
var user = {firstName : 'John', lastName: 'Doe'};
var subject = ejs.render('Hello <%= firstName %>', user);
var text = ejs.render('Hello, <%= firstName %> <%= lastName %>!', user);
var options = {
from: <from>,
replyTo: <replyto>,
to: <to>,
subject: subject,
text: text
};
transport.sendMail(options, cb);
}
To load the template file just us the fs module. Here's how to do it synchronously when the file is encoded in utf-8:
var fs = require('fs');
var template = fs.readFileSync('abc.html',{encoding:'utf-8'});