【问题标题】:Sendgrid Always Sends Text EmailSendgrid 始终发送文本电子邮件
【发布时间】:2015-11-25 00:38:44
【问题描述】:

我有一个 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);
});

模板

<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>

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

【问题讨论】:

  • 检查documentation。您需要设置电子邮件对象的 HTML,而不是文本。
  • 我明白了,谢谢。 即使不需要也是必需的。
  • 我相信 'text' 是电子邮件客户端不支持 HTML 电子邮件的后备方案。
  • 是的。我在想我可以把我所有的 html 放在模板中并且只使用替换,但我看到我必须至少放一个“。”在 html 字段中。

标签: html node.js email sendgrid


【解决方案1】:

根据documentation。只有在发送纯文本时才使用 text 属性。而是使用 html 属性来创建 HTML 消息。您可以使用内置的setHtml 方法,如下所示:

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

【讨论】:

    【解决方案2】:

    如果您不使用 SendGrid API,但 System.Net.Mail 类与 sendgrid 下面的设置将做到这一点:

    EmailMessage.IsBodyHtml = true;
    

    【讨论】:

    • 那么纯文本选项呢,许多垃圾邮件检查器会尝试查找是否有,如果没有找到纯文本/文本版本,则可能会将其标记为垃圾邮件。
    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多