【问题标题】:How to insert dynamic blocks of html inside SendGrid email?如何在 SendGrid 电子邮件中插入动态 html 块?
【发布时间】:2016-09-02 15:47:59
【问题描述】:

我正在使用 SendGrid Web API v3。尝试生成包含许多收件人的个性化对象。每个收件人都有 1 封电子邮件,其中包含与当前收件人相关的数据(通过替换传递)

示例:

personalizations: [{
       to: [{email: 'example@mail.com'}],
       subject: 'Hello, :name!',
       substitutions: {':name': 'John', ':info_section_html':'<p>Some useful block1</p><p>Some useful block2</p>'}
    },
    {
       to: [{email: 'example@mail.com'}],
       subject: 'Hello, :name!',
       substitutions: {':name': 'John', ':info_section_html':'<p>Some useful block1</p><p>Some useful block2</p><p>Some useful block3</p><p>Some useful block4</p><p>Some useful block5</p><p>Some useful block6</p>'}
    }
],
    from: {email: 'send@example.com'},
    content: [{type: 'text/html', value: 'Hello, :name! </br> Here are your very usefull info</br> :info_section_html'}]

替换时:info_section_html 有很多这样的块,它超过了 10000 字节的限制。我的块有比示例中更多的 HTML。每个收件人可以有不同数量的块,这就是为什么我不能将它们包含在内容属性中。

我还在考虑可以在其中传递我的info_section_html 的 HTML 的部分属性。但不能传递数据数组替换。

有人可以建议如何克服这个问题吗?

【问题讨论】:

    标签: html email sendmail sendgrid


    【解决方案1】:

    我知道这来得太晚了,但我遇到了同样的问题并且找不到答案,所以我想也许仍然在这里发布它会很有用:

    可以在这里找到:SendGrid API v3

    遵循模式“substitution_tag”:“要替换的值”的键/值对集合。全部假定为字符串。除了主题和回复参数之外,这些替换将适用于您的电子邮件正文的文本和 html 内容。每个个性化对象的替换总大小不得超过 10,000 字节。

    所以这里没有什么可做的。

    对于这种用法,Sendgrid 有另一个称为 Sections 的功能(也可以查看上面的链接)。它们的工作方式几乎与Substitutions 一样,只是它们链接到整个邮件而不是Substitution

    它们可以与Substitutions 一起使用以实现上述行为。

    正如 mbernier 所说:Github issue related to the topic

    你可以像这样使用它们:

        "personalizations: [
    {
        "to: [{"email":"bob@example.com"}],
        "substitutions": {
            "[%product_info%]": "[%has_product%]",
        },
    },
    {
        "to":[{"email":"bob@example.com"}],
        "substitutions": {
            "[%product_info%]": "[%no_product%]",
        },
    }],
    "content": [
    {
        "type": "text/plain",
        "value": "We just wanted to tell you that we appreciate you being a long time customer! [%product info%]"
    }],
    "sections": {
        "[%has_product%]": "Also, thanks for ordering:<br />[%product_section%].<br /> Are you ready to order again!?",
        "[%no_product%]": "You haven't ordered in a while, but we'd love it if you came back and saw our new products!"
    }
    

    这里最重要的是应该通过替换标记添加这些部分。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-04
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多