0

I'm using RazorEngine v3.3 to create emails using template files (emails are sent using SendGrid Web API). I implemented a base template so I can use my own html helpers by overriding the WriteTo() method as shown here .

My problem is that my emails are part Html and part Text. For the Html templates, I use razor's default implementation that html-encodes the @Model values. This is because some of the data comes from user input. However, I can't use the same implementation for the text part as Html will not be interpreted when being read.

So the way I see it I have 3 options:

  • use @Raw(Model) in all my text based templates to ignore Html encoding
  • create an other base template for my text templates which doesn't encode to Html
  • modify my Html base template so the WriteTo() method doesn't encode anything

The 1st solution seems the safest, but I have about 50 text based templates to go through, and it reduces readability.

The 2nd solution seems the cleanest to me, however this would prevent the use of cache, as I would be constantly doing Razor.SetTemplateService() to reassign the right base template ?

What would you recommend doing ? Thanks

Community
  • 1
  • 1
ThunderDev
  • 1,118
  • 2
  • 19
  • 38
  • Why not just use the Postal nuget package? You can then inject the various user elements into the output from properties on the email model (it support having both HTML and text templates per email) – iCollect.it Ltd Mar 24 '14 at 11:07
  • 1
    Postal uses the native SmtpClient, if I'm not mistaken. My company uses a external web client so I don't think it would work out for me. Also, I'm in way to deep to change the mailing system to something other than RazorEngine ^^ Thanks for the suggestion – ThunderDev Mar 24 '14 at 11:39
  • FYI We use an external SMTP provider with `Postal` (Azure SendGrid). Postal is configurable in web.config. – iCollect.it Ltd Mar 24 '14 at 11:59

0 Answers0