1

We are using Sendgrid C# library created a stored dynamic template. It is working fine except we cannot figure out how to access the "to" object to display the user's name dynamically in the template. Here is the JSON being sent:

{
   "from":{
      "email":"fromemail@mydomain.com"
   },
   "personalizations":[
      {
         "to":[
            {
               "name":"test name",
               "email":"test@testingmail.com"
            }
         ],
         "dynamic_template_data":{
            "MailBody":"this is a \u003cstrong\u003eTEST!\u003c/strong\u003e",
            "MailSalutation":"Best Regards,",
            "MailGreeting":"Dear"
         }
     
      
      },{
         "to":[
            {
               "name":"another name",
               "email":"anothertest@testingmail.com"
            }
         ],
         "dynamic_template_data":{
            "MailBody":"this is a \u003cstrong\u003eTEST!\u003c/strong\u003e",
            "MailSalutation":"Best Regards,",
            "MailGreeting":"Dear"
         }
     
      
      }
   ],
   "template_id":"xxxxxxxxxx-ede3cd3f9b"
}

The template looks something like this:

<p> {{MailGreeting}} {{to.name}} or {{to[0].name}}???,</p>
        
        <p>
        {{{MailBody}}}
        </p>
        <p>
        {{MailSalutation}}

</p>

The name property of the "to" object is what we want to display here {{to.name}}

We are using the recommended method from the sample code with our data being fed in.

Our class object:

public class EmailMergeSendDetails
        {
            public List<EmailAddress> MailToList { get; set; }
            public string MailFrom  { get; set; }
            public string MailFromName { get; set; }
            public string MailSubject { get; set; }
            public string MailBody { get; set; }
            
    }

Using Sendgrid C# to send

var from = new EmailAddress(d.MailFrom, d.MailFromName);
            var tos = d.MailToList;

                var dynamicTemplateData = new EmailMergeSendDetails
                {
                    Subject = d.MailSubject,
                    MailSalutation = d.MailSalutation,
                    MailGreeting = d.MailGreeting,
                    MailBody = d.MailBody,
                   
            };

                var msg = MailHelper.CreateSingleTemplateEmailToMultipleRecipients(from,
                                                                       tos,
                                                                       "d-xxxx-template-id-here",
                                                                       dynamicTemplateData
                                                                       );

var response = await client.SendEmailAsync(msg);

Why isn't this working for the {{to.name}}? I'm guessing that the only data available for the {{}} is the dynamic_template_data? If so, how do we display the to.name. It feels like such a basic thing to be able to do but we've literally spend hours on it :).

For the sake of clarity, we have a template for a newsletter which stores dynamic template data in our database. We want to query that data along with multiple name/email addresses to send it to (25k+ of them), fill in the template with the newsletter data and send it to each one of the recipients adding a "Dear _____" at the top. Seems super basic. How can we do this using the dynamic templates if CreateSingleTemplateEmailToMultipleRecipients is not the correct approach?

brianc
  • 455
  • 2
  • 21

1 Answers1

3

I don't think you can. You can add additional values to your dynamic_template_data though; e.g. "RecipientName", which you would then set to "test name", and then reference it in your template as {{RecipientName}}.

Or... the way you have done it, you could just add it to the end of MailGreeting; e.g. give it a value of "Dear test name" instead of just "Dear".

Edit (multiple emails):

From looking at the package code, I think CreateMultipleTemplateEmailsToMultipleRecipients() is what you want instead, and adding a different RecipientName (or similar) to each dynamicTemplateData, and ensuring you have the same number of items in tos as dynamicTemplateData.

SendGrid has a limit of 1000 personalizations per email, so you'd need to break up your 25k into batches.

sellotape
  • 8,034
  • 2
  • 26
  • 30
  • We thought about adding it to the dynamic_template_data but with a single template sent to multiple email addresses, that would add the entire "to" array to the dynamic data for each email sent, essentially sending the "to" data twice. From looking at JSON sent (provided above), to and dynamic_template_data both fall under "personalizations" and a separate personalization is sent for each email. It should be accessible in this way. We must be going at it wrong. Maybe CreateSingleTemplateEmailToMultipleRecipients is not the correct function. – – brianc Nov 02 '21 at 16:29
  • I'm going to edit my answer a little, as I'd forgotten that personalizations is a collection with addresses in each. – sellotape Nov 02 '21 at 21:05
  • 2
    Twilio SendGrid developer evangelist here. This answer is right, the data you are sending to the template is entirely contained in the `dynamic_template_data`, so even if you are sending the name as part of the `to` email, you will need to send it as part of the template data as well in order to read it in the template. – philnash Nov 03 '21 at 01:31
  • @philnash. Thanks. We tried that with the existing code and it seems the entire "to" array would be grouped with each personalization object in dynamic_template_data essentially increase the json size quite a bit. Something seems off. There must be a better way to do this. Given the code above , what would. your solution be using the Sendgrid C# library? Is it the CreateMultipleTemplateEmails function as suggested? Much aooreciated – brianc Nov 03 '21 at 06:57
  • Ah! I see it now. Yes, what the library was doing with the CreateSingleTemplateEmailToMultipleRecipients we will basically do in our code prior to the submission and submit the dynamicTemplateData as a list to the last argument. Got it. – brianc Nov 03 '21 at 07:04