Sendgrid-csharp: Unique arguments with substitution tags

Created on 2 Jan 2017  Â·  6Comments  Â·  Source: sendgrid/sendgrid-csharp

Issue Summary

inconsistency when using unique arguments with substitution tags

Steps to Reproduce

this is the code i use:
personalization.AddSubstitution("%cid%", "cid example");
personalization.AddSubstitution("%cstate%", "cstate example");
personalization.AddSubstitution("%cfname%", "cfname example");
personalization.AddSubstitution("%clname%", "clname example");
personalization.AddCustomArgs("cid", "%cid%");
personalization.AddCustomArgs("cstate", "%cstate%");
personalization.AddCustomArgs("cfname", "%cfname%");
personalization.AddCustomArgs("clname", "%clname%");

this is the serialization of the personalization json:
{"Tos":[{"Name":null,"Address":"[email protected]"}],"Ccs":null,"Bccs":null,"Subject":null,"Headers":null,"Substitutions":{"%cid%":"cid example","%cstate%":"cstate example","%cfname%":"cfname example","%clname%":"clname example"},"CustomArgs":{"cid":"%cid%","cstate":"%cstate%","cfname":"%cfname%","clname":"%clname%"},"SendAt":null}

i would expect all the args to be replaced but the result in this case is
UNIQUE ARGUMENTS:
CFNAME : %cfname%
CID : cid example
CLNAME : %clname%
CSTATE : %cstate%
(this data is from sendgrid activity feed)

i tried different values for the substitution and it results in inconsistency - sometimes i get two proper replacements but i don't understand when a replacement is working and when it doesn't

Technical details:

  • sendgrid-c# Version: 8.0.5
  • target framework: .NET 4.5.2
help wanted question

Most helpful comment

Hello @lebamb2,

Here is some sample code:

var from = new EmailAddress("[email protected]", "Name0");
var emails = new List<EmailAddress>
{
    new EmailAddress("[email protected]"),
    new EmailAddress("[email protected]"),
    new EmailAddress("[email protected]")
};
var subjects = new List<string> {"Test Subject1", "Test Subject2", "Test Subject3"};
var plainTextContent = "Hello -name-";
var htmlContent = "Goodbye -name-";
var substitutions = new List<Dictionary<string, string>>
{
    new Dictionary<string, string>() {{"-name-", "Name1"}},
    new Dictionary<string, string>() {{"-name-", "Name2"}},
    new Dictionary<string, string>() {{"-name-", "Name3"}}
};
var msg = MailHelper.CreateMultipleEmailsToMultipleRecipients(from,
                                                              emails,
                                                              subjects,
                                                              plainTextContent,
                                                              htmlContent,
                                                              substitutions
                                                              );

All 6 comments

Hello @lebamb2,

Looking through this code, I don't see any obvious bug. I'm going to add this to our backlog for further investigation.

Thanks!

Hello @lebamb2,

Now that v9 is out of beta, have you had a chance to give it a spin?

hi,
can you give an example of how substitution should work on v9?
when i try to add substitutions, for example
msg.AddSubstitution("%contact_id%", "1");
msg.AddSubstitution("%contact_id%", "2");
i get an exception: "An item with the same key has already been added."

thanks

Hi @lebamb2,

You are using it correctly. The problem is that you are trying to add a value to the same key. For example, this should work:

msg.AddSubstitution("%contact_id1%", "1");
msg.AddSubstitution("%contact_id2%", "2");

Thanks!

thanks for the reply @thinkingserious,
isn't the idea of substitution to replace the same placeholder with
different values for different recipients?
if i have an email with the text "hello %contact_name%",
i want to do:
msg.AddSubstitution("%contact_name%", "Elmer");
msg.AddSubstitution("%contact_name%", "Amit");
in v1.2.2 there was a recipients list and a substitution list containing a
keyword and a list of values. what is the equivalent in v9?
can you refer me to an example with substitution in v9?

On Tue, Mar 21, 2017 at 5:55 PM, Elmer Thomas notifications@github.com
wrote:

Hi @lebamb2 https://github.com/lebamb2,

You are using it correctly. The problem is that you are trying to add a
value to the same key. For example, this should work:

msg.AddSubstitution("%contact_id1%", "1");msg.AddSubstitution("%contact_id2%", "2");

Thanks!

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-csharp/issues/381#issuecomment-288124896,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAzlEbQDi2psoYlX8ZQmlv27U9fbWBSZks5rn_MCgaJpZM4LZPVN
.

Hello @lebamb2,

Here is some sample code:

var from = new EmailAddress("[email protected]", "Name0");
var emails = new List<EmailAddress>
{
    new EmailAddress("[email protected]"),
    new EmailAddress("[email protected]"),
    new EmailAddress("[email protected]")
};
var subjects = new List<string> {"Test Subject1", "Test Subject2", "Test Subject3"};
var plainTextContent = "Hello -name-";
var htmlContent = "Goodbye -name-";
var substitutions = new List<Dictionary<string, string>>
{
    new Dictionary<string, string>() {{"-name-", "Name1"}},
    new Dictionary<string, string>() {{"-name-", "Name2"}},
    new Dictionary<string, string>() {{"-name-", "Name3"}}
};
var msg = MailHelper.CreateMultipleEmailsToMultipleRecipients(from,
                                                              emails,
                                                              subjects,
                                                              plainTextContent,
                                                              htmlContent,
                                                              substitutions
                                                              );
Was this page helpful?
0 / 5 - 0 ratings