mehdime / RazorEngineEmails

http://mehdi.me/generating-html-emails-with-razorengine-introduction/
MIT License
40 stars 21 forks source link

Question - How to use premailer.net with RazorEngine #2

Open Martin-Andersen opened 6 years ago

Martin-Andersen commented 6 years ago

You are singing the praise of Premailer.Net but not using it. Where in the rendering flow should I use PreMailer?

Martin-Andersen commented 6 years ago

I can see that RazorEngine are a moving target. Don't know if you have been working with version 3.1 I have started to write some code but I am not sure on how to cache the template.

private static void ConfigureRazorEngineEmailSettings(IAppBuilder app, Container container, AppSettingsRepository settings)
{
    var templateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates");
    var config = new TemplateServiceConfiguration();
    config.Namespaces.Add("Goo.Web.Api.Models");
    var service = RazorEngineService.Create(config);

    var html = File.ReadAllText(Path.Combine(templateFolderPath, "SuggestNewWord.cshtml"));
    var htmlSuggestNewWord = PreMailer.Net.PreMailer.MoveCssInline(html);

    // do we need this
    service.AddTemplate("SuggestNewWord", htmlSuggestNewWord.Html);
    var emailBody = service.RunCompile("SuggestNewWord", typeof(AboutPosFormModel));

    // run this code in a API controller
    emailBody = service.Run("SuggestNewWord", typeof(AboutPosFormModel), new AboutPosFormModel());
}

There are RunCompile and Run. I guess that I use RunCompile in the setup and Run in my controllers. Then there are service.AddTemplate this is a little confusing because RunCompile with a key should cache the template.

Thanks for the articles