22
votes

Sending transactional apis through SendGrid. My template (ported over from Mailchimp) has conditionals (e.g.

*|IF:SHOWTHISSECTION|*

in Mailchimp syntax). This includes or excludes sections of the template based on a variable.

I can't find the analog in SendGrid, does it simply not have this capability? I'd like to suppress certain sections depending on the presence/absence of a substitution variable.

7

7 Answers

21
votes

SendGrid supports this natively now:

{{#if user.profile.male}}
  <p>Dear Sir</p>
{{else if user.profile.female}}
  <p>Dear Madame</p>
{{else}}
  <p> Dear Customer</p>
{{/if}}

Reference: https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements

6
votes

It's a horrible hack, but by introducing new variables and using CSS, you can hide the relevant portions of mails using display. So where before in Mandrill/MailChimp I'd have something like:

    *|IF:FAKEVAR|* 
    <p>Show some text here</p>
    *|END:IF|*

Instead, introduce a new variable IF_FAKEVAR, whose value is either "none" or "inherit" depending on whether FAKEVAR has a value, then do this:

<p style="display: *|IF_FAKEVAR|*">Show some text here</p>

While it's a hack, for very complex email templates, it avoids sending 70k bytes to the server for every single email, which when you have thousands or tens of thousands of mails, is prohibitive.

2
votes

SendGrid templating does not support this, but you can use a templating API like sendwithus to accomplish this on top of your SendGrid account. I believe sendwithus supports jinja conditionals, so you could do the following:

{% if variable %}
    <h1>{{ variable }}</h1>
{% endif %}
2
votes

SendGrid doesn't have true conditionals, but it does have Section Tags. With those, you can define a block of text at the message level (as opposed to the distinct recipient level of a Substitution Tag), and then call the appropriate section for the recipient as needed.

2
votes

I Know this is old, but I had the same problem and I found a solution compatible with several email managers that maybe it's helpful for someone.

You can use substitution tags with the html comment symbols value in case you want to hide a section.

{%OPEN_COMMENT}
<h1>Whatever section you want to hide</h1>
{%CLOSE_COMMENT}

Replace tags with "" respectively if you want to hide the section. Replace them with empty strings in the other case.

2
votes

Sendgrid supports conditional using Handlebar

{{#if user.profile.male}}
  <p>Dear Sir</p>
{{else if user.profile.female}}
  <p>Dear Madame</p>
{{else}}
  <p> Dear Customer</p>
{{/if}}

from their documentation here https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements

1
votes

Below handlebars can be used in Sendgrid dynamic templates:

Conditional statements:
{{#if variable}}
{{#unless variable}}
{{#greaterThan variable value}}
{{#lessThan variable value}}
{{#equals variable value}}
{{#notEquals variable value}}
{{#and variable1 variable2}}
{{#or variable1 variable2}}

Looping statements:
{{#each hash}}

Refer https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/ for detailed information