OrchardCoreContrib / OrchardCoreContrib.Modules

Unofficial Orchard Core modules that driven by the community who love Orchard Core CMS
BSD 3-Clause "New" or "Revised" License
35 stars 10 forks source link

Improvement proposal for `OrchardCoreContrib.Email.SendGrid`: add support for SendGrid `Dynamic Templates` via SendGrid Client Library #135

Open MarGraz opened 1 week ago

MarGraz commented 1 week ago

Hi,

I think it would be great if the OrchardCoreContrib.Email.SendGrid module could implement the SendGrid client library to send emails using pre-created Dynamic Templates.

SendGrid allows you to create email templates with placeholders in their backoffice, using a WYSIWYG editor (official guide here). You can then use the client library to select the template and pass dynamic text to the APIs. Here is an example of using Dynamic Templates.

Below is the code snippet from this example:

using SendGrid;
using SendGrid.Helpers.Mail;

var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("{ Your verified email address }", "{ Sender display name }");
var to = new EmailAddress("{ Recipient email address }", "{ Recipient display name }");

var templateId = "{ Your dynamic template id }";
var dynamicTemplateData = new
{
    subject = $"To-Do List for {DateTime.UtcNow:MMMM}",
    recipientName = "Demo User", 
    todoItemList = new[]
    {
        new { title = "Organize invoices", dueDate = "11 June 2022", status = "Completed" },
        new { title = "Prepare taxes", dueDate = "12 June 2022", status = "In progress" },
        new { title = "Submit taxes", dueDate = "25 June 2022", status = "Pending" },
    }
};
var msg = MailHelper.CreateSingleTemplateEmail(from, to, templateId, dynamicTemplateData);

var response = await client.SendEmailAsync(msg);
if (response.IsSuccessStatusCode)
{
    Console.WriteLine("Email has been sent successfully");
}

I think that should be sufficient to add another method in the SendGridService.cs class, that allows the use of a Dynamic Template.

What do you think about it? 😊

Thank you

hishamco commented 6 days ago

This could be a useful feature for email in general, do you want to send a PR for it? The template could be customizable: Liquid, Text .. etc

MarGraz commented 6 days ago

@hishamco I can implement the SendGrid one, adding a method in the SendGridService.cs class. But I need a new interface to use in DI, because ISmtpService doesn't provide any method to be used with the SendGrid template 🤔

Of course, in this case, the template is provided by SendGrid. But yes, I agree, having the possibility to create a template and manage a list of them could be a nice feature for the Email module in general.

Thank you

hishamco commented 5 days ago

Let's start on https://github.com/OrchardCoreContrib/OrchardCoreContrib repo and add templating infrastructure to the OCC.Email