Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

MsSql - 2014

I recently started programming on nodeJs.

I have email template in data base tabel.

For eg:

   message1 : Hello {friendName},
                  Happy Birthday {friendName}.
                  Thank you,
                  {Name}

    message2 : Hello All,
                  The total expense as;
                    {ListOfExpense}
                  Thank you,
                  {Name}

For message 1 -- I have UI input as name and friendName on input obj. For message 2 -- I have UI input of List of expense on input.expense obj.

I am able to read from DB let say in var dbMessage = message 1 or message 2;

I want to use template engines to change {} values according to template and store on the text so I can set on the body.

I am using nodemailer to send mail;

var mailOptions={
        to : req.query.to,
        subject : req.query.subject,
        text : ? // I want use text as email body.
    }

    smtpTransport.sendMail(mailOptions, function(error, response){
        if(error){
           res.end("error");
        }else{
           res.end("sent");
        }
    });

Kindly help.

share|improve this question

Well, you can do this a bunch of ways. You're only doing string interpolation so you can very easily use a function with ES6 template literals..

function myTemplate(env) {
    const template = `Hello ${env.friendName},
                  Happy Birthday ${env.friendName}.
                  Thank you,
                  ${env.Name}`;
    return template.replace(/\n/, '<br>');
}

var mailOptions={
        to : req.query.to,
        subject : req.query.subject,
        text : myTemplate({friendName: "foo", Name: "Bar"});
    };

You may want to trim it up a bit, but that'll get you started.

Other template languages include Pugjs (previously Jade), Handlebars.js, and _.template. Typically these more professional third party templates compile down to a JavaScript function, and they're called the same way as our function above. All of the above engines have to be set up in their own fashion...

const pug = require('pug');
const myTemplate = pug.compileFile('myPugPath.pug');
myTemplate({friendName: "foo", Name: "Bar"});

Also, that you're sending email has nothing to do with this question. Just write an HTML string somehow (ex., using ES6 Template literals, or pug) and then send the email with the mime type properly set (text/html), and the body set to the html.

share|improve this answer
    
Thanks, For answering. I have one question, If I use Pug. I do not have file myPugPath.pug. I have dbresult message. So how do I use in that case. – Sawan Kumar 9 mins ago
    
@SawanKumar you would simply use pug.compile. You'll have to have the DB store pug templates. – Evan Carroll 5 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.