sendgrid / sendgrid-csharp

The Official Twilio SendGrid C#, .NetStandard, .NetCore API Library
https://sendgrid.com
MIT License
1.08k stars 587 forks source link

Using Templates #157

Closed leonelpc closed 8 years ago

leonelpc commented 8 years ago

There seems to be a problem when specifying template engines via EnableTemplateEngine("");. When the template id is provided it looks like we still need to provide the message subject and body, even if the template is already taking care of that. For example this will throw an invalid request error...

        var credentials = new NetworkCredential("user", "password");
        var tranport = new SendGrid.Web(credentials);

        var msg = new SendGrid.SendGridMessage();            

        msg.EnableTemplateEngine("de639fc1-1b82-469f-929e-507691aa1855");
        msg.DisableUnsubscribe();

        msg.AddSubstitution(":name", new List<string>() { "John", "Smith" });
        msg.AddSubstitution(":suburb", new List<string>() { "Fremantle" });
        msg.AddSubstitution(":role", new List<string>() { "Agent" });

        //msg.Subject = "dsfsd";
        //msg.Html = "dfgfdshf";
        msg.From = new System.Net.Mail.MailAddress("test@server.com", "Test");
        msg.To = new System.Net.Mail.MailAddress[]
            {
                new System.Net.Mail.MailAddress("user@domain.com", "Albert Einstein")
            };

        await tranport.DeliverAsync(msg);

Notice that I commented out the lines that assign the subject and the body of the email. In my understanding there shouldn't be a need to specify this information if I'm already using a template to populate those fields.

Even worse, if I assign a value to the message subject and body (I'm forced to do so) the template's subject and body gets "appended" to the provided message subject and body.

penihel commented 8 years ago

My workaround

myMessage.Html = "&nbsp;";

But it is really a bug

wiggisser commented 8 years ago

This seems to be connected, to the feature/bug that you have to include the <%subject%> and <%body%> tags in the template, even if you don't use them. (Why would someone define an email template and then provide the email's body via code???). They will be replaced with the values from msg.Subject and msg.Text/msg.Html respectively. As they must not be empty I had to put these tags at the very end and set

 msg.Text = " ";
 msg.Html = " ";
 msg.Subject = " ";

Ugly workaround ...

andrewflierman commented 8 years ago

:+1: and still an issue

hankonon commented 8 years ago

..and still an issue. (6.3.4.0) Took a while to find out why emails were not getting sent (Grrr..). (And nothing in activity log) I was searching why SendGrid's DeliverAsync was not working.. How is this an "official" library if no one is fixing stuff? I refactored our email sending to sendgrid library to get rid of bunch of one-man github projects. Well now i seem to at least have only one of them.

I'll add the code here for working example (as template body/replacement tags were unclear to me, prob because of the concept of having to have body tag in template was confusing coming from razorengine templates)

            var emailMessage = new SendGridMessage();
            emailMessage.From = new MailAddress(EmailFrom, "Support Team");
            emailMessage.AddTo(toEmailAddress);
            emailMessage.Subject = "Complete registration";
            emailMessage.EnableTemplateEngine(completeRegistrationTemplateId);
            emailMessage.AddSubstitution("%%registration_uri%%", new List<String>() { completeRegistrationUri });
            //Without setting something to these will not work.
            emailMessage.Text = " ";
            emailMessage.Html = " ";

            try
            {
                var transportWeb = new Web(sendGridEmailApiKey);
                transportWeb.DeliverAsync(emailMessage);
            }
            catch (Exception e)
            {
                Logger.Exception(e, "Exception sending email message: ");
            }

and template:

Hello,

Click this link to register:
%%registration_uri%%

Br, 
Administrator

<%body%>
thinkingserious commented 8 years ago

Apologies for my delayed response. This ticket is still on our backlog.

Quick update:

I'm currently working on getting the new versions of all our libraries updated across the 7 languages we support to provide access to the new v3 API. I can't comment on a specific time, but we are very close and I'm very excited to share it with you.

One of the things we are doing with the new libraries is creating specific helper code for common scenarios and "creating a template" will definitely be one of them, which I hope, will make sending with them a breeze.

tiago-costa-farfetch commented 8 years ago

" This ticket is still on our backlog.".

This is sad...I would need to do a fast decision upon a service like yours and by the documentation, this seems to be the perfect one....

Isn't there any alternative to this problem? For example, is it possible to specify wich argument is null in the header? Probably overriding it would be a quick win to wait for the final fix.

tiago-costa-farfetch commented 8 years ago

Discovered the problem and found the solution.

For my case, I have examples where one or more of my substitution values may be null and this seemed to be the problem because I just validate if it is null and if so I just pass a string.empty.

thinkingserious commented 8 years ago

Hello Everyone,

The workaround described above is not needed anymore with a recent fix to the API.

Also, you can now find the new v3 /mail/send endpoint here: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html and the new version of the library here: https://github.com/sendgrid/sendgrid-csharp/blob/v3beta We should be out of beta in a few weeks.

@tiago-costa-farfetch thank you for sharing your solution!

Thanks for your support everyone!

If you continue to experience issues with the templates, please feel free to reopen this issue.

With Best Regards,

Elmer