sendgrid / sendgrid-csharp

The Official Twilio SendGrid C#, .NetStandard, .NetCore API Library
https://sendgrid.com
MIT License
1.09k stars 586 forks source link

Unable to send mail with template and substitutions #712

Closed Joerio closed 6 years ago

Joerio commented 6 years ago

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("from@example.com");
            msg.SetTemplateId("d-ad7946cf53634a16925d551e15ed6a7c");
            msg.AddTo("to@example.com");
            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("from@example.com");
            msg.SetTemplateId("d-ad7946cf53634a16925d551e15ed6a7c");

            var personalization = new Personalization();
            personalization.Tos = new System.Collections.Generic.List<EmailAddress>();
            personalization.Tos.Add(new EmailAddress("test@example.com"));
            personalization.Substitutions = substitions;

            msg.Personalizations = new System.Collections.Generic.List<Personalization>();
            msg.Personalizations.Add(personalization);

            var response = await client.SendEmailAsync(msg);

Technical details:

Edit Removed email adres from code

ChrisASearles commented 6 years ago

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

p-selivanov commented 6 years ago

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.

SlyCaptainFlint commented 6 years ago

I am seeing the same behavior on my end.

thinkingserious commented 6 years ago

@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

RoelVerhees commented 6 years ago

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

thinkingserious commented 6 years ago

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

Thanks for the help @RoelVerhees!