OrchardCoreContrib.Modules icon indicating copy to clipboard operation
OrchardCoreContrib.Modules copied to clipboard

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

Open MarGraz opened this issue 8 months ago • 12 comments

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

MarGraz avatar Jun 27 '24 16:06 MarGraz