2015-05-31 84 views
1

我有一個簡單的聯繫表格使用Node Sendgrid helper library使用電子郵件模板聯繫表格使用Express/Sendgrid

我想使用編譯爲HTML並添加正確上下文的模板email/contact.jade。我知道它需要在payload.html的價值,但我堅持如何發送電子郵件的模板。

routes.js

app.route('/contact') 
    .post(function(req, res) { 
    var template = 'email/contact.jade'; 

    var payload = { 
     to: req.body.email, 
     from: '[email protected]', 
     subject: req.body.subject, 
     html: req.body.message 
    }; 

    var email = new sendgridClient.Email(payload); 

    sendgridClient.send(email, function(err, json) { 
     if (err) { 
     return console.error(err); 
     } else { 
     res.redirect('/thanks'); 
     } 
    }); 
    }); 

電子郵件/ contact.jade

p Thanks 
p Name {{ name }} 
p Email {{ email }} 
p Subject {{ subject }} 
p Message {{ message }} 

回答

1

首先我不知道你的玉語法是正確的。你可以代替試試這個:

電子郵件/ contact.jade

p Thanks 
p Name #{name} 
p Email #{email} 
p Subject #{subject} 
p Message #{message} 

而且渲染成HTML這樣的:

var jade = require('jade'); 
var templatePath = __dirname + '/contact.jade'; 

app.route('/contact') 
    .post(function(req, res) {  
    var payload = { 
     to: req.body.email, 
     from: '[email protected]', 
     subject: req.body.subject, 
     html: jade.renderFile(templatePath, req.body) 
    }; 
    //... 
    }); 
+0

你說得對玉模板。然而,我必須這樣做才能使它工作:var template = fs.readFileSync(__ dirname +'/contact.jade')',因爲我一直收到'ENOENT,沒有這樣的文件或目錄' – cusejuice

+1

也只是'html:jade.renderFile(__ dirname +'/contact.jade',req.body)''。使用'renderFile'方法http://jade-lang.com/api/ – cusejuice

+0

是的,'renderFIle'也可以工作,可能會更好,因爲它可以讓玉更容易緩存模板。我已經編輯了一些答案,將其考慮在內。 –