简体   繁体   English

Sendgrid 动态数据未发送到模板

[英]Sendgrid dynamic data not being sent to Template

This is how I send my email:这就是我发送 email 的方式:

public async System.Threading.Tasks.Task<string> SendInternalEmail(BasicEmailStructureViewModel Structure, List<string> AdditionalVariables, TenantEmailTemplate tenantEmailTemplate, TenantCommunication tenantCommunication, string ReceiverId, string ReceiverEmail, string ReceiverName, string CampaignName)
{
    try
    {

        var client = new SendGridClient(tenantCommunication.SendgridApiKey);
        var message = new SendGridMessage();
        message.SetFrom(new EmailAddress(tenantCommunication.SendgridPrimarySender, tenantCommunication.SendgridPrimarySenderTag));
        message.AddTo(new EmailAddress(ReceiverEmail, $"{ReceiverName}"));
        message.Subject = tenantEmailTemplate.Subject;
        message.SetTemplateId(tenantEmailTemplate.TemplateId);
        List<string> jsonVars = new List<string>();

        var subjectString = @$"""subject"":""{tenantEmailTemplate.Subject}""";

        jsonVars.Add(subjectString);

        foreach (PropertyInfo prop in Structure.GetType().GetProperties())
        {
            var variableString = @$"""{prop.Name}"":""{prop.GetValue(Structure, null)}""";
        }

        for (var i = 0; i < AdditionalVariables.Count; i++)
        {
            jsonVars.Add(AdditionalVariables[i]);
        }
        var flattenList = "{" + string.Join(",", jsonVars) + "}";
        var emailData = Newtonsoft.Json.JsonConvert.DeserializeObject<object>(flattenList);
        message.SetTemplateData(emailData);
        if (CampaignName != null && CampaignName != "")
        {
            message.AddCustomArg("CampaignName", CampaignName);
        }
        var response = await client.SendEmailAsync(message);
        if (response.IsSuccessStatusCode == true)
        {
            return Guid.NewGuid().ToString();
        }
        else
        {
            var errorMessage = response.Body.ReadAsStringAsync().Result;
            return errorMessage;
        }
    }
    catch(Exception e)
    {
        if (e != null)
        {
            return e.Message;
        }
    }
    return "Invalid Email";
}

A typical input to this function will be like this:这个 function 的典型输入是这样的:

var variableString = @$"""verification_link"":""www.website.com?Key={Input.Key}"""; var variableString = @$"""verification_link"":""www.website.com?Key={Input.Key}""";

My email sends normally, however, none of the variables that I have set have been sent through.我的email发送正常,但是我设置的变量都没有发送通过。 This is based roughly off the template sample on github: https://github.com/sendgrid/sendgrid-csharp/blob/main/examples/templates/templates.cs这大致基于 github 上的模板示例: https://github.com/sendgrid/sendgrid-csharp/blob/main/examples/templates/templates.cs

Is there another sample I can use or what is the correct way to send variables dynamically?有没有我可以使用的另一个示例,或者动态发送变量的正确方法是什么?

I don't think that is the best way to construct the JSON for your dynamic template variables.我认为这不是为动态模板变量构造 JSON 的最佳方法。

Would it be possible for you to build the variables as an object and then serialize them.您是否可以将变量构建为 object,然后将它们序列化。 Like:喜欢:

var templateData = new {
  subject = tenantEmailTemplate.Subjectm
  otherVariableName = otherVariable
};

string emailData = JsonConvert.SerializeObject(templateData);
message.SetTemplateData(emailData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有模板的情况下发送 Sendgrid 电子邮件 - Sendgrid emails being sent without the template Ruby on Rails 和 SendGrid; 动态模板数据未填充 - Ruby on Rails and SendGrid; dynamic template data is not populating SendGrid API - 无法将动态模板数据与旧模板一起使用 - SendGrid API - Cannot use dynamic template data with a legacy template SendGrid 动态模板中的收件人详细信息 - Recipient detail in SendGrid Dynamic Template 如何使用动态模板将 Sendgrid 中的 email 发送给单个收件人并获取已发送的 HTML 以供保留 - How to send an email in Sendgrid to a single recipient using dynamic template and obtain the sent HTML for retention purposes SendGrid 动态模板中的基本替换 - Basic Substitution in SendGrid Dynamic Template Sendgrid - 动态模板中的车把错误消息 - Sendgrid - handlebar error msg in dynamic template 如何在 SendGrid 代码编辑器中使用手柄在动态 email 模板中呈现所有 json 数据? - How to render all json data in dynamic email template with handlebars in SendGrid code editor? 在 sendgrid 中发送多个动态模板电子邮件时出现个性化字段错误 - Personalizations field error when sending multiple dynamic template emails in sendgrid 通过 SendGrid 发送来自 Okta 的 MFA email 时 DMARC 失败 - DMARC failing when MFA email from Okta being sent through SendGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM