javascript - How to use template engines to send text as email in nodejs - Stack Overflow

MsSql - 2014I recently started programming on nodeJs.I have email template in data base tabel. For eg:

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.

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 edited Jun 5, 2016 at 6:27 Evan Carroll 1 asked Jun 5, 2016 at 6:14 Sawan KumarSawan Kumar 3271 gold badge6 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Well, you can do this a bunch of ways.

ES6 Template Literals

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.

Fancy full featured template engine

Other template languages include Pugjs (previously Jade), Handlebars.js, and _.template. Typically these more professional third party templates pile 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.pileFile('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.

To load template from DB

If you have the template in your database then rather than using pug.pileFile you would simply use pug.pile(resultFromDb.pugTemplate). Check the documentation on pug for more information.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745048396a4608235.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信