【问题标题】:Sendgrid: render an ejs template and then send as an email.Sendgrid:渲染一个 ejs 模板,然后作为电子邮件发送。
【发布时间】:2015-07-23 15:39:47
【问题描述】:

所以我有 index.ejs,当我启动我的 nodejs 服务器时,它可以完美呈现:

<!DOCTYPE html>
<html>
    <head>
        <title<%= title %></title>
    </head>
    <body>
        <h1><%= title %></h1>
        <h3><%= yesterday %></h3>
        <h1> Number of Spins: <%= count %></h1>
        <h1> Active User Count: <%= userCount %></h1>
        <h1> Users that did not validate: </h1>
        <ul>

            <% for(var i=0; i<unvalid.length; i++) {%>

                <li><%= unvalid[i] %></li>
            <% } %>
        </ul>
    </body>
</html>

问题是,我想使用 Sendgrid 通过电子邮件发送。到目前为止,我一直在做的是使用 .setHTML 方法来“蛮力”它:

email.setHtml('<h1> Spotluck Daily Report </h1><h3>'+ yesterday + '</h3><h1> Number of Spins: '+cuenta+'</h1><h1> Active User Count: '+userCount+'</h1>' +'<h1> Users that did not validate: </h1>');

但这永远不会起作用,因为它将无法呈现 ejs for 循环。所以我的问题是:我如何告诉 Sendgrid 电子邮件呈现我的 ejs 并将其作为电子邮件发送而不必求助于 .setHTML?

【问题讨论】:

    标签: javascript node.js email sendgrid


    【解决方案1】:

    您可以使用setHtml 内部的ejs.render(str, subs) function 实现此目的。

    email.setHtml(ejs.render(yourTemplate, {foo: 'bar'}));
    

    但我建议使用 SendGrid 的 Template Engine,因为我们的节点库 supports it

    【讨论】:

    • 所以我正在尝试做 email.setHtml(ejs.render('../path/to/index.ejs', { //在这里设置模板变量 })),但是当我发送的电子邮件只是在电子邮件上打印出“../path/to/index.ejs”。建议?
    • 您需要使用类似readFileSync 的方式来读取文件。甚至可以打电话给toString()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多