How to substitute Sendgrid template variables? (in C#)

4.7k views Asked by At

I defined a variable <%datetime%> in my SendGrid template. I decided by this naming convention to follow the already placed <%subject%> of the subject line. I see different variable naming conventions in the examples: https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41 uses -name- and -city-, while https://github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157 uses %name% and %city%.

I just assume, that the variable substitution is based on simple pattern matching, so the counterpart templates of those examples contain the same exact strings. That doesn't work for me so far though for whatever reason.

string sendGridApiKey = ConfigurationManager.AppSettings["SendGridApiKey"].ToString();
var sendGrid = new SendGridAPIClient(sendGridApiKey);

string emailFrom = ConfigurationManager.AppSettings["EmailFrom"].ToString();
Email from = new Email(emailFrom);
string subject = "Supposed to be replaced. Can I get rid of this somehow then?";
string emaiTo = ConfigurationManager.AppSettings["EmailTo"].ToString();
Email to = new Email(emaiTo);
Content content = new Content("text/html", "Supposed to be replaced by the template. Can I get rid of this somehow then?");
Mail mail = new Mail(from, subject, to, content);
mail.TemplateId = "AC6A01BB-CFDF-45A7-BA53-8ECC54FD89DD";
mail.Personalization[0].AddSubstitution("<%subject%>", $"Your Report on {shortDateTimeStr}");
mail.Personalization[0].AddSubstitution("<%datetime%>", longDateTimeStr);
// Some code adds several attachments here

var response = await sendGrid.client.mail.send.post(requestBody: mail.Get());

The request is accepted and processed, but email I get still have the subject line

"Supposed to be replaced. Can I get rid of this somehow then?"

The body is replaced by the raw template content but variable is not substituted either. What am I doing wrong?

1

There are 1 answers

0
Csaba Toth On BEST ANSWER

After reading How to Add Custom variables to SendGrid email via API C# and Template question and answers I came to the realization that it was a wrong decision to use <%foobar%> type notation.

Basically it's SendGrid's own notation, and <%subject%> means that they gonna replace what you assign to the Mail subject, in my case that was "Supposed to be replaced. Can I get rid of this somehow then?". Now I assemble a proper subject there.

In the template body itself I switched to {{foobar}} notation for variables. Although the last answer for the question linked above states that you must insert <%body%> into the template body, but that's not required. It works without it for me. I assume I could use my own {{foobar}} variables in the subject line also with proper substitution instead of the <%subject%>.

Basically the default state of the template is <%subject%> for the subject and <%body%> for the body, which would result in a seamless email delivery if you don't want any substitution and supply the subject and the body through the API.

Correct me if I'm wrong please.

string subject = $"Report on ${shortDateTimeStr}";
string emaiTo = ConfigurationManager.AppSettings["EmailTo"].ToString();
Email to = new Email(emaiTo);
Content content = new Content("text/html", "Placeholder");
Mail mail = new Mail(from, subject, to, content);
mail.TemplateId = "AC6A01BB-CFDF-45A7-BA53-8ECC54FD89DD";
mail.Personalization[0].AddSubstitution("{{datetime}}", longDateTimeStr);

TL;DR: don't use <%foobar%> notation for your own variable, but choose one from the dozen of other styles. None of the examples or docs I read mentioned this.