How to include html email template in node js application

I have an html template that we use to send to new website registrations. This is a simple html file that I would like to load into a variable so that I can replace some parts before sending using nodemailer (for example, [FIRST_NAME]). I try not to embed most of the html in my export function. Any ideas on how I can do this?

For a clearer idea, I need to know how to do this:

var first_name = 'Bob';  
var html = loadfile('abc.html').replace('[FIRST_NAME]', first_name);
+4
source share
2 answers

Here is an example of how to do this with ejs, but you can use any template 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);

}

, fs. , , utf-8:

var fs = require('fs');

var template = fs.readFileSync('abc.html',{encoding:'utf-8'});
+4

, -, .

, , :)

(PS: , )

Js nodemailer:

var nodemailer = require('nodemailer')
var jade = require('jade');
var config = {
  // config for sending emails like username, password, ...
}
var emailFrom = 'this@email.com';
var emailTo = 'this@email.com';
var templateDir = 'path/to/templates/';
var transporter = nodemailer.createTransport(config);

var username = 'thisUsername'
// rendering html template (same way can be done for subject, text)
var html = jade.renderFile(templateDir+'/html.jade', {username: 'testUsername'});

//build options
var options = {
   from: emailFrom,
    to: emailTo,
    subject: 'subject',
    html: html,
    text:'text'
};

transporter.sendMail(options, function(error, info) {
  if(error) {
    console.log('Message not sent');
    console.log(info);
    return false;
  }
  else{
    console.log('Message sent: ' + info.response);
    console.log(info);
    return true;
  };
});

html.jade

p test email html jade
p
| Username:
| !{username}

nodemailer.

js :

var path = require('path');
var EmailTemplate = require('email-templates').EmailTemplate;
var transporter = nodemailer.createTransport(config);

var templateDir = path.join(__dirname, '/yourPath/emailTemplates', 'subdir');
var template = new EmailTemplate(templateDir)
var username = 'testUsername';


var transport = nodemailer.createTransport(config)
template.render(locals, function (err, results) {
  if (err) {
    return console.error(err)
  }
  // replace values in html template
    console.log('template render')
    console.log(err);

    // default is results.html in this case
    // read template and replace desired values
    var res1 = results.html.toString();
    var str = res1.replace('__username__', username);
    console.log(str);
    console.log('end template render')

  transport.sendMail({
    from: emailFrom,
    to: emailTo,
    subject: 'subject',
    html: str,
    text: results.text
  }, function (err, responseStatus) {
    if (err) {
      return console.error(err)
    }
    console.log(responseStatus)
  })
})

html.html

test email html
username:

<div>
__username__
</div>
+1

All Articles