Sendgrid 总是发送文本电子邮件

Dan*_*nce 5 html email node.js sendgrid

我有一个 HTML sendgrid 模板,正在使用 npm 包“sendgrid”通过 Node.js 发送它。问题是我总是以文本格式而不是 HTML 接收电子邮件,即使模板具有 HTML。

代码:

var email = new sendgrid.Email({
    to      : 'me@here.com',
    from    : 'you@there.com',
    subject : 'Saying Hi with HTML Template',
    text    : 'Body'    //This is required
});

email.addFilter('templates', 'enable', 1);
email.addFilter('templates', 'template_id', '12131331.....');
email.addSubstitution('{{TOKEN1}}', 'value');

sendgrid.send(email, function(err, json) {
    if (err) { console.error(err); }
    console.log(json);
});
Run Code Online (Sandbox Code Playgroud)

模板

<html>
<head><title></title></head>
<body>
  <h1>This is a test</h1>
  <p>{{TOKEN1}}</p>
  <p><a href="http://www.there.com">There</a></p>

  <div>&lt;%body%&gt;</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

是否有我应该设置的代码参数?还是模板本身的设置以允许 HTML?

JD *_*vis 7

根据文档。如果您发送纯文本,则仅使用 text 属性。相反,使用 html 属性来创建 HTML 消息。您可以使用内置setHtml方法,如下所示:

var email     = new sendgrid.Email(); 
email.setHtml('<h1>Some html</h1>');
sendgrid.send(email, function(err, json) { });
Run Code Online (Sandbox Code Playgroud)