Unable to send mail with template and substitutions
Issue Summary
When I set a template from code to a template I just created with a single variable, and try to pass substitutions with the call I always get the following error: "BadRequest" - "{"errors":[{"message":"Substitutions may not be used with dynamic templating","field":"personalizations.0.substitutions","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions"}]}"
Steps to Reproduce
- Create a SendGridMessage()
- Call functions SetFrom, SetTemplateId, AddTo and AddSubstitutions on the object
- Send the mail using SendEmailAsync
var substitions = new System.Collections.Generic.Dictionary<string, string>();
substitions.Add("name", name);
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage();
msg.SetFrom("[email protected]");
msg.SetTemplateId("d-ad7946cf53634a16925d551e15ed6a7c");
msg.AddTo("[email protected]");
msg.AddSubstitutions(substitions);
var response = await client.SendEmailAsync(msg);
I tried it with the 'AddSubstitutions' and the 'AddTo' on the message, and also using the Personalizations.Add()
var substitions = new System.Collections.Generic.Dictionary<string, string>();
substitions.Add("name", name);
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage();
msg.SetFrom("[email protected]");
msg.SetTemplateId("d-ad7946cf53634a16925d551e15ed6a7c");
var personalization = new Personalization();
personalization.Tos = new System.Collections.Generic.List<EmailAddress>();
personalization.Tos.Add(new EmailAddress("[email protected]"));
personalization.Substitutions = substitions;
msg.Personalizations = new System.Collections.Generic.List<Personalization>();
msg.Personalizations.Add(personalization);
var response = await client.SendEmailAsync(msg);
Technical details:
- sendgrid-csharp Version: latest (9.9.0)
- Platform Console Application (Windows)
- .NET Version: 4.6.1
Edit Removed email adres from code
I second this, getting the same behavior no matter what I try. Has this library not been updated to use the new transactional templates? If not, is there a timeline for when support will be added? Hate the idea of creating new "legacy" templates ...
Same error here. As far as I understand, to use v3 templates with placeholders we need to provide "personalization.dynamic_template_data" section to the request JSON. Though c# library does not implement a way to do this.
I am seeing the same behavior on my end.
@ChrisASearles, @p-selivanov, @SlyCaptainFlint,
I will get some helpers added to support the dynamic templates soon; however, I don't have a firm timeline.
In the meantime, you can handcraft the request body like this.
With Best Regards,
Elmer
Only thing you have to do is adding
[JsonProperty(PropertyName = "dynamic_template_data", IsReference = false)]
public object DynamicTemplateData { get; set; }
in the Personalizations class, and after that you can use it like this:
sendGridMessage.Personalizations[0].DynamicTemplateData = new
{
RegionHouseNumber = 28,
EmployeeFirstName = "Chef"
}