简体   繁体   English

在 SendGrid 电子邮件模板中遍历数组

[英]Iterating through array in SendGrid email template

I'm trying to iterate through a collection and display information in a SendGrid template using Ruby on Rails.我正在尝试使用 Ruby on Rails 遍历集合并在 SendGrid 模板中显示信息。

recipient = SendGrid::Recipient.new("sergio@gmail.com")
recipient.add_substitution("username", user.github_id)
recipient.add_substitution("numbers", [1,2,3,4])

In gmail, this template arrives as:在 gmail 中,此模板到达:

sergiotapia
ARRAY(0x85b9d90)

The actual code for the template, copied from SendGrid's editor:模板的实际代码,从 SendGrid 的编辑器中复制:

<html>
  <head>
    <title></title>
  </head>
  <body>
    <div>&lt;%body%&gt;</div>

    <div>username</div>

    <div>numbers</div>

    <p>This is a small example email.</p>
  </body>
</html>

How can I iterate through a generic array or object in a SendGrid template?如何遍历 SendGrid 模板中的通用数组或对象? For this particular example, a user has many posts and I just want to show the title of the user's posts in a <li> element.对于这个特定示例,用户有很多posts ,我只想在<li>元素中显示用户帖子的标题。

I'm just trying things out with a simple number array to see how it SendGrid works.我只是尝试使用一个简单的数字数组来查看 SendGrid 是如何工作的。

Update August 2018: 2018 年 8 月更新:

Sendgrid now offers iterators from the transactional email using handlebars, here's to the docs for more info: Sendgrid 现在使用 handlebars 提供来自交易电子邮件的迭代器,这里是文档以获取更多信息:

https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#basic-iterator https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#basic-iterator

Iterate example for the data:迭代数据示例:

{
  "people":[{"name":"Bob"},{"name":"Sally"}]
}

Code:代码:

{{#if people}}
  <p>People:</p>
  {{#each people}}
    <p>{{this.name}}</p>
  {{/each}}
{{/if}}

Result:结果:

People:人们:

Bob鲍勃

Sally莎莉

{{#each data.products}}
    {{name}}: {{price}} <br/>
{{/each}}

{"data":{"products": [{"name": "Tomato", "price": "5"}, {"name": "Banana", "price": "8"}]}}

Update更新

SendGrid now has support for dynamic templates! SendGrid 现在支持动态模板!

You can read about it on their blog: https://sendgrid.com/blog/how-to-use-sendgrids-dynamic-templates-for-your-transactional-emails/您可以在他们的博客上阅读: https ://sendgrid.com/blog/how-to-use-sendgrids-dynamic-templates-for-your-transactional-emails/

Old answer:旧答案:

Searching for this resulted the following GitHub issue .对此进行搜索会导致以下GitHub 问题 So it's not possible with SendGrid (yet?).所以 SendGrid 不可能(还?)。

However there are other ways to do this.但是,还有其他方法可以做到这一点。 Using sendwithus you get access to a more powerful template editor that supports looping and iterating.使用sendwithus ,您可以访问功能更强大的模板编辑器,该编辑器支持循环和迭代。

Simply set it up using your own SendGrid API key and you will be able to use the arrays in the sendwithus template which will send the mail using SendGrid.只需使用您自己的 SendGrid API 密钥进行设置,您就可以使用 sendwithus 模板中的数组,该模板将使用 SendGrid 发送邮件。

Unfortunately the templates SendGrid provides are pretty minimal at this time.不幸的是,此时 SendGrid 提供的模板非常少。 The templates don't support arrays as values and there are no conditional or looping controls, so you'd need to predetermine everything prior to building the template and template content.模板不支持数组作为值,并且没有条件或循环控件,因此您需要在构建模板和模板内容之前预先确定所有内容。 A more robust templating system is coming soon.一个更强大的模板系统即将推出。

Here is a workaround Sendgrid is yet to update their template engine for this这是一个解决方法 Sendgrid 尚未为此更新其模板引擎

Hello guys i need to do some iteration in my sendgrid mail and ran into this issue for now i had a temporal workaround which solved the problem.大家好,我需要在我的 sendgrid 邮件中进行一些迭代并遇到了这个问题,现在我有一个解决问题的临时解决方法。 here is how i worked around it这是我解决它的方法

  • created a txt file and loaded my html template there创建了一个 txt 文件并在那里加载了我的 html 模板
  • Note areas i wanted to iterate on i replaced with a sendgrid variable eg %list of items%注意我想迭代的区域我用 sendgrid 变量替换,例如 %list of items%
  • read the txt file into a string create a string builder and pass all iterated object into the %list of items% variable将 txt 文件读入字符串创建一个字符串生成器并将所有迭代对象传递到 %list of items% 变量中

then send the whole string content as message through sendgrid然后通过 sendgrid 将整个字符串内容作为消息发送

public void sendSimpleMessage(String message,
                String subject, 
                String toEmail,
                String fromEmail){
                 Email from = new Email(fromEmail);
                 Email to = new Email(toEmail);
                 Content content = new Content("text/html", message);
                 Mail mail = new Mail(from, subject, to, content);


                 SendGrid sg = new SendGrid(sendgridApiKey);
                 Request request = new Request();
                 try {
                      request.method = Method.POST;
                      request.endpoint = "mail/send";
                      request.body = mail.build();
                      sg.api(request);
                 } catch (IOException ex) {
                      ex.printStackTrace();
                }
        }

hope it helps someone https://github.com/sendgrid/sendgrid-nodejs/issues/221#issuecomment-361489007希望它能帮助别人https://github.com/sendgrid/sendgrid-nodejs/issues/221#issuecomment-361489007

        "businessContributors" : [
                    {
                        "total" : {
                            "amount" : 11340,
                            "title" : "Dhama Ji Total"
                        },
                        "list" : {
                            "Dhama Ji Paid" : -296310,
                            "HDFC Account" : 0,
                            "Dhama Ji Received" : 307650
                        },
                        "title" : "Dhama Ji Owner Account"
                    },
                    {
                        "total" : {
                            "amount" : -1270,
                            "title" : "Rahul Total"
                        },
                        "list" : {
                            "Rahul Paid" : 243838,
                            "Rahul Received" : 242568
                        },
                        "title" : "Rahul Account"
                    },
        ]
    
    
   in email template  :-

 
     <h4>Business Contributors </h4>
            <ul>
                {{#each businessContributors}}
                <li> {{this.title}} <br>
                    {{#each this.list}}
                     {{@key}} = {{this}} <br>
                {{/each}}  </li>
                

      
         
                <hr style="height: 2px; background-color: black;"><br>
                <h2>{{this.total.title}}</h2><br>
                <h2>{{this.total.amount}}</h2>
                <br><br>
            {{/each}}
            </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM