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":"[email protected]"
},
"personalizations":[
{
"to":[
{
"name":"test name",
"email":"[email protected]"
}
],
"dynamic_template_data":{
"MailBody":"this is a \u003cstrong\u003eTEST!\u003c/strong\u003e",
"MailSalutation":"Best Regards,",
"MailGreeting":"Dear"
}
},{
"to":[
{
"name":"another name",
"email":"[email protected]"
}
],
"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?
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.