sendgrid-csharp icon indicating copy to clipboard operation
sendgrid-csharp copied to clipboard

Unable to send mail with template and substitutions

Open Joerio opened this issue 8 years ago • 6 comments

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

  1. Create a SendGridMessage()
  2. Call functions SetFrom, SetTemplateId, AddTo and AddSubstitutions on the object
  3. 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

Joerio avatar Jul 25 '18 10:07 Joerio

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

ChrisASearles avatar Jul 25 '18 13:07 ChrisASearles

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.

p-selivanov avatar Jul 25 '18 13:07 p-selivanov

I am seeing the same behavior on my end.

SlyCaptainFlint avatar Jul 25 '18 17:07 SlyCaptainFlint

@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

thinkingserious avatar Jul 25 '18 20:07 thinkingserious

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" }

RoelVerhees avatar Jul 26 '18 12:07 RoelVerhees

Please track support for this issue here moving forward. Thanks!

Thanks for the help @RoelVerhees!

thinkingserious avatar Jul 26 '18 21:07 thinkingserious