Iterating through array in SendGrid email template

19,909

Solution 1

Update August 2018:

Sendgrid now offers iterators from the transactional email using handlebars, here's to the docs for more info:

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

Solution 2

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

Solution 3

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

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

Solution 4

Update

SendGrid now has support for dynamic templates!

You can read about it on their blog: https://sendgrid.com/blog/how-to-use-sendgrids-dynamic-templates-for-your-transactional-emails/

Old answer:

Searching for this resulted the following GitHub issue. So it's not possible with SendGrid (yet?).

However there are other ways to do this. Using sendwithus you get access to a more powerful template editor that supports looping and iterating.

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.

Share:
19,909
Sergio Tapia
Author by

Sergio Tapia

All things Elixir and Ruby. All opinions my own.

Updated on June 23, 2022

Comments

  • Sergio Tapia
    Sergio Tapia almost 2 years

    I'm trying to iterate through a collection and display information in a SendGrid template using Ruby on Rails.

    recipient = SendGrid::Recipient.new("[email protected]")
    recipient.add_substitution("username", user.github_id)
    recipient.add_substitution("numbers", [1,2,3,4])
    

    In gmail, this template arrives as:

    sergiotapia
    ARRAY(0x85b9d90)
    

    The actual code for the template, copied from SendGrid's editor:

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

    I'm just trying things out with a simple number array to see how it SendGrid works.

  • Sergio Tapia
    Sergio Tapia over 8 years
    youtube.com/watch?v=09s-c2JVI40 - Is it possible for me to send over a large string and replace that in the template? I can loop and render in Rails, and send that chunk to be replaced in the template. Is that kosher?
  • bwest
    bwest over 8 years
    Yep, that's a good way to go, here's a similar example
  • Maria Ines Parnisari
    Maria Ines Parnisari over 7 years
    Is this answer still valid or has SenGrid implemented loops?
  • Dima Kozhevin
    Dima Kozhevin about 5 years
    While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.