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?