简体   繁体   English

如何替换Sendgrid模板变量? (在C#中)

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

I defined a variable <%datetime%> in my SendGrid template. 我在SendGrid模板中定义了一个变量<%datetime%> I decided by this naming convention to follow the already placed <%subject%> of the subject line. 我根据此命名约定决定遵循已放置的主题行的<%subject%> 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% . 我在示例中看到了不同的变量命名约定: https : //github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L41使用-name--city- ,而https:/ /github.com/sendgrid/sendgrid-csharp/blob/master/SendGrid/Example/Example.cs#L157使用%name%%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? 我究竟做错了什么?

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. 在阅读了如何通过API C#和模板问题和答案将自定义变量添加到SendGrid电子邮件后 ,我意识到使用<%foobar%>类型表示法是一个错误的决定。

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?" 基本上,这是SendGrid自己的符号, <%subject%>表示它们将替换您分配给Mail subject ,在我的情况下是"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. 在模板主体中,我使用了{{foobar}}符号表示变量。 Although the last answer for the question linked above states that you must insert <%body%> into the template body, but that's not required. 尽管以上链接的问题的最后一个答案指出您必须在模板主体中插入<%body%> ,但这不是必需的。 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%> . 我假设我可以在主题行中使用自己的{{foobar}}变量进行适当的替换,而不是使用<%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. 基本上,模板的默认状态是<%subject%><%body%> ,如果您不希望进行任何替换并通过主题提供主题和正文,则可以实现无缝的电子邮件传递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. TL; DR:不要对您自己的变量使用<%foobar%>表示法,而应从其他数十种样式中选择一种。 None of the examples or docs I read mentioned this. 我阅读的所有示例或文档都没有提到这一点。

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

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