2015-10-07 24 views
0

我想要插入HTML文件模板以用于与节点一起发送的电子邮件中。如何在节点中为电子邮件插入HTML文件模板

当我使用下面的代码,我得到这个错误...

(function (exports, require, module, __filename, __dirname) { <!doctype html>

有什么建议?

var template = require('../../views/email-template.js'); 
var htmlAll = _.template(template)(contactinfo[0]); 
var data = { 
    from: [email protected], 
    to: email, 
    subject: 'Your Order Confirmation', 
    html: htmlAll 

电子邮件template.js

<!doctype html> 
<html> 
    <head> 
    <meta name="viewport" content="width=device-width"> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Email</title> 
    <style media="all" type="text/css"> 

// . . . 

    <table> 
    // . . . 

      <td><%= firstName %> <%= lastName %> 
      <br><%= company %> 
      <br><%= address %> 
      <br><%= city %>, <%= state.code %> <%= zip %> 
      <br><%= email %> 
      <br><%= phone %> 
      </td> 
     </tr> 

    </table> 
    // . . . 

回答

1

你不能要求通过的NodeJS require

非JavaScript文件使用fs模块

var fs = require('fs') 
    var template = fs.readFileSync('../../views/email-template.js').toString(); 
    var htmlAll = _.template(template)(contactinfo[0]); 

最好是缓存fs.readFileSync结果持续使用