是否可以在Node.js中使用lodash模板发送电子邮件的HTML版本?

时间:2015-10-04 08:27:43

标签: node.js express lodash

当用户提交订单时,我使用Node.js发送确认电子邮件。我想要包含的电子邮件包括用户提交的所有项目。每个提交的项目数量会有所不同。我希望电子邮件正文包含一个表格。可以使用lodash模板吗?或者应该采用不同的方式处理?

当我使用以下代码时,生成的电子邮件包含我认为是未编译代码的内容。

var tpl = _.template('<% _.forEach(items, function(item) { %><td><%- item %></td><% }); %>');
tpl({ 'items': ['Guitar', 'Harmonica'] });

var data = {
  from: 'support@example.com',
  to: email,
  subject: 'Your Order Confirmation',
  html: '<p>Thank you for submitting your order.</p>
      <table>
        <tr>
         <thead>
           <tr>
            <th><strong>Items</strong></th>'
               + tpl +

             // The template should insert each item here
             // <td>Guitar</td>   
             // <td>Harmonica</td> 

          '</tr>
         </thead>
        </tr>
       </table>' 
  };

发送的实际电子邮件中的输出:

function (obj) { obj || (obj = {}); var __t, __p = '', __e = _.escape,
__j = Array.prototype.join; function print() { __p +=
__j.call(arguments,    '') } with (obj) { _.forEach(items,
function(item) { ; __p += ' ' + __e(     item ) + ' '; }); ; } return
__p }

1 个答案:

答案 0 :(得分:1)

变化:

tpl({ 'items': ['Guitar', 'Harmonica'] });

var html = tpl({ 'items': ['Guitar', 'Harmonica'] });

ng>Items</strong></th>'
               + tpl +

ng>Items</strong></th>'


  + html +

如果您查看以下文档:https://lodash.com/docs#template 您将看到编译的函数返回您需要使用的输出。 您不使用输出,而是使用实际的函数本身。