I'm using the c# NuGet library. I'm trying to receive custom args on the SendGrid callbacks.
For some reason the custom args are being returned just for the first email on the list provided on the MailHelper.CreateSingleEmailToMultipleRecipient function. I don't know if this is the normal behavior and I didn't find any information on the documentation about it.
Here is my SendEmail function
private static void SendMail(RequestSendMail requestSendMail, [CallerMemberName] string callerFunction = "")
{
try
{
TemplateEmail templateEmail = TemplateEmailBLL.Get(new TemplateEmail() { Code = requestSendMail.TemplateCode });
if (templateEmail != null)
{
string apiKey = Environment.GetEnvironmentVariable("FOOBAR");
SendGridClient client = new SendGridClient("FOOBAR");
EmailAddress from = new EmailAddress("mycompany.co", "MyCompany");
if (requestSendMail.Metatags != null && requestSendMail.Metatags.Count > 0)
{
for (int i = 0; i < requestSendMail.Metatags.Count; i++)
{
Metatag metatag = requestSendMail.Metatags[i];
templateEmail.HTML = templateEmail.HTML.Replace("[#" + metatag.Tag + "#]", metatag.Value);
templateEmail.Subject = templateEmail.Subject.Replace("[#" + metatag.Tag + "#]", metatag.Value);
}
}
List<EmailAddress> mailAddressRecipients = new List<EmailAddress>();
// mailAddressRecipients[0].Email = "[email protected]" -> this receives the custom args.
// mailAddressRecipients[1].Email = "[email protected]" -> this don't receive the custom args.
for (int i = 0; i < requestSendMail.Recipients.Count; i++)
{
mailAddressRecipients.Add(new EmailAddress(requestSendMail.Recipients[i].Email));
}
SendGridMessage msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, mailAddressRecipients, templateEmail.Subject, string.Empty, templateEmail.HTML);
// Adding custom args here!
msg.AddCustomArg("CallerFunction", callerFunction);
msg.AddCustomArg("TemplateCode", requestSendMail.TemplateCode);
if (requestSendMail.CustomArgs.Count > 0)
msg.AddCustomArgs(requestSendMail.CustomArgs);
try
{
client.SendEmailAsync(msg);
}
catch {}
}
}
catch (Exception ex)
{
throw ex;
}
}
Here is the callbacks
[
{
"CallerFunction": "ChargeSubscription",
"IdentifierKey": "TransactionId",
"IdentifierValue": "0",
"TemplateCode": "155",
"email": "[email protected]",
"event": "processed",
"send_at": 0,
"sg_event_id": "cHJvY2Vzc2VkLTk4MTQxMTktMG4wdWdnYUxSM3V6T3ZMUHZmb0VpUS0w",
"sg_message_id": "0n0uggaLR3uzOvLPvfoEiQ.filterdrecv-p3iad2-cbc8f7585-bxkw8-17-5E4ADA88-68.0",
"smtp-id": "<[email protected]>",
"timestamp": 1581963913
},
{
"CallerFunction": "ChargeSubscription",
"IdentifierKey": "TransactionId",
"IdentifierValue": "0",
"TemplateCode": "155",
"email": "[email protected]",
"event": "delivered",
"ip": "149.72.209.135",
"response": "250 2.6.0 <[email protected]> [InternalId=14267881409505, Hostname=DB8EUR05HT127.eop-eur05.prod.protection.outlook.com] 24860 bytes in 0.652, 37.220 KB/sec Queued mail for delivery -> 250 2.1.5",
"sg_event_id": "ZGVsaXZlcmVkLTAtOTgxNDExOS0wbjB1Z2dhTFIzdXpPdkxQdmZvRWlRLTA",
"sg_message_id": "0n0uggaLR3uzOvLPvfoEiQ.filterdrecv-p3iad2-cbc8f7585-bxkw8-17-5E4ADA88-68.0",
"smtp-id": "<[email protected]>",
"timestamp": 1581963916,
"tls": 1
}
]
[
{
"email": "[email protected]",
"event": "processed",
"send_at": 0,
"sg_event_id": "cHJvY2Vzc2VkLTk4MTQxMTktMG4wdWdnYUxSM3V6T3ZMUHZmb0VpUS0x",
"sg_message_id": "0n0uggaLR3uzOvLPvfoEiQ.filterdrecv-p3iad2-cbc8f7585-bxkw8-17-5E4ADA88-68.1",
"smtp-id": "<[email protected]>",
"timestamp": 1581963913
}
]
[
{
"email": "[email protected]",
"event": "delivered",
"ip": "149.72.209.135",
"response": "250 2.0.0 OK 1581963915 q13si650662ejz.259 - gsmtp",
"sg_event_id": "ZGVsaXZlcmVkLTAtOTgxNDExOS0wbjB1Z2dhTFIzdXpPdkxQdmZvRWlRLTE",
"sg_message_id": "0n0uggaLR3uzOvLPvfoEiQ.filterdrecv-p3iad2-cbc8f7585-bxkw8-17-5E4ADA88-68.1",
"smtp-id": "<[email protected]>",
"timestamp": 1581963916,
"tls": 1
}
]
Hello @nicholasks,
Thanks for reporting this! I have verified this is a bug.
Here is the code I used and the resulting JSON:
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Example
{
internal class Example
{
private static void Main()
{
Execute().Wait();
}
static async Task Execute()
{
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("[email protected]", "Example User");
var tos = new List<EmailAddress>
{
new EmailAddress("[email protected]", "Example User1"),
new EmailAddress("[email protected]", "Example User2"),
new EmailAddress("[email protected]", "Example User3")
};
var subject = "Sending with Twilio SendGrid is Fun";
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var showAllRecipients = false; // Set to true if you want the recipients to see each others email addresses
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from,
tos,
subject,
plainTextContent,
htmlContent,
showAllRecipients
);
msg.AddCustomArg("CallerFunction", "test1");
msg.AddCustomArg("TemplateCode", "test2");
Console.WriteLine(msg.Serialize());
// var response = await client.SendEmailAsync(msg);
}
}
}
Output:
{
"content": [
{
"type": "text/plain",
"value": "and easy to do anywhere, even with C#"
},
{
"type": "text/html",
"value": "<strong>and easy to do anywhere, even with C#</strong>"
}
],
"from": {
"email": "[email protected]",
"name": "Example User"
},
"personalizations": [
{
"custom_args": {
"CallerFunction": "test1",
"TemplateCode": "test2"
},
"to": [
{
"email": "[email protected]",
"name": "Example User1"
}
]
},
{
"to": [
{
"email": "[email protected]",
"name": "Example User2"
}
]
},
{
"to": [
{
"email": "[email protected]",
"name": "Example User3"
}
]
}
],
"subject": "Sending with Twilio SendGrid is Fun"
}
This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.
Thank you!
With best regards,
Elmer
Best I can tell this is by design. AddCustomArgs (and other similar adders) was designed to update a specific personalization or the first one (or in the case of PHP, the last one).
I think what you're wanting is AddGlobalCustomArg which applies to all personalizations in the message.
Values that are specific to the entire send that will be carried along with the email and its activity data. Substitutions will not be made on custom arguments, so any string that is entered into this parameter will be assumed to be the custom argument that you would like to be used. This parameter is overridden by any conflicting聽personalizations[x].custom_args聽if that parameter has been defined. If聽personalizations[x].custom_args聽has been defined but does not conflict with the values defined within this parameter, the two will be merged. The combined total size of these custom arguments may not exceed 10,000 bytes.
Most helpful comment
Best I can tell this is by design.
AddCustomArgs(and other similar adders) was designed to update a specific personalization or the first one (or in the case of PHP, the last one).I think what you're wanting is
AddGlobalCustomArgwhich applies to all personalizations in the message.