0xdeafcafe / sendgrid-dotnet

Other
15 stars 1 forks source link

Template support #4

Closed fa10 closed 7 years ago

fa10 commented 7 years ago

Hi i am using your implementation, and i wanted to ask if template is working? I tried to use a template, but did get the mail with the template. I was thinking that maybe we need to set the template enabled 1 with the request?

I think that you have some jsonproperty names that dont match with the SendGrid API v3.

0xdeafcafe commented 7 years ago

I didn't see a template_enabled or similar option in the docs for the v3 api. Could you link me to it?

fa10 commented 7 years ago

Hi @0xdeafcafe https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/smtpapi.html

0xdeafcafe commented 7 years ago

That is for the old X-SMTP-API. If you look below it says

If you are using the Web API v3 mail send endpoint, you can specify which transactional template you would like to use simply by setting the template ID in the template_id parameter of your JSON payload.

So I'm not sure what's going on. Could be a bug on the SendGrid end. The JsonProperty is set up correctly for it.

fa10 commented 7 years ago

Okay that sounds good! I will try again and let you know about the result. Do you have a good example on how you use a template with substitutions/sections? And what is the difference on them (substitutions/sections)?

0xdeafcafe commented 7 years ago

I have no idea sorry, never used either - and I don't know what the difference is. @thinkingserious will be about to help you out with that.

gldraphael commented 7 years ago

@fa10 I think this should help you

gldraphael commented 7 years ago

I was able to use SendGrid's transactional API with a code like this:

var key = new ApiKeyConnection("SG.xxxxxxxxxx--xx_xxxxxxx.xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx-xxxx");
var client = new SendGridClient(key);

// Read on substitutions here: https://sendgrid.com/docs/API_Reference/SMTP_API/substitution_tags.html
var substitutions = new Dictionary<string, string>();
substitutions.Add("replace_this", "with this"); // the substitution key (replace_this) can't have spaces
substitutions.Add("-name-", "Galdin Raphael");

client.MailClient.SendAsync(new Email
{
    Personalizations = new List<Personalization>
    {
        new Personalization
        {
            To = new List<EmailDetail>
            {
                new EmailDetail
                {
                    Email = "name@domain.com",
                    Name = "Galdin Raphael"
                }
            },
            Substitutions = substitutions
        }
    },
    // Sections = substitutions,
    From = new EmailDetail
    {
        Email = "email@domain.com",
        Name = "Galdin Raphael"
    },
    Subject = "Hi -name-",
    TemplateId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}).Wait();
fa10 commented 7 years ago

@gldraphael okay thank you what is the reason that the Substitutions and Sections are set with the same values?

gldraphael commented 7 years ago

@fa10 setting substitutions will be enough for the above example

I can send one request to SendGrid to send 100 emails. In that case I'll have to send 100 substitutions (in my example, I'll send 100 names as substitutions). Things like the date will however be common for all the emails. So I can add those as sections just once, and the same substitution will be done in all the 100 emails. So my example shouldn't be setting the Sections tag. (I've commented it out now)

Substitution is for personalised information. Section is for common information.

This page documents it.

0xdeafcafe commented 7 years ago

Thanks @gldraphael. Let me know if this works for you @fa10 and I'll go ahead and close this.

fa10 commented 7 years ago

@gldraphael thanks for you answer and information. @0xdeafcafe i will test it and let you know.

0xdeafcafe commented 7 years ago

Assuming this is fixed so I'm going to go ahead and close this. Let me know if it isn't.