0

I'm working on a testing tool in asp mvc. One of the features is to preview html emails.

However, these emails are going to have things like doctypes and css tags. What are my options to properly display these emails without screwing up the html on my page?

There is no need to keep the formatting and css, just the text and links that comes with it. Any ideas?

1
  • any <html> or <head> tags can usually be stripped since most conforming HTML emails don't use stylesheets--they use inline style attributes (and things like <meta>, <title>, <link> or <scripts> are absent from emails). From there, reset the CSS starting at your email's wrapper element and let the email format the rest. Jul 29, 2013 at 18:23

2 Answers 2

5

You could load the whole content into an iFrame.

<div id="preview">
    <!-- YOUR ACTUAL PREVIEW SITE -->
    <iframe src="/path/to/newsletter"></iframe>
    <!-- YOUR ACTUAL PREVIEW SITE -->
</div>
4
  • I went with this solution. I'll give best answer once I confirm it works well :)
    – elite5472
    Jul 29, 2013 at 19:55
  • I'm having trouble with your solution. I'm getting the raw html within the iframe.
    – elite5472
    Jul 30, 2013 at 13:43
  • solved, I had to render my html string as follows: @(new HtmlString(Model.Content));
    – elite5472
    Jul 30, 2013 at 13:57
  • Or the shorthand helper: @Html.Raw(Model.Content) Jul 30, 2013 at 14:13
1

Assuming this are your own HTML email so you don't need to worry about security loading in IFrame will give you the most close rendering.

Note that IFrame will not strip out external links as most e-mail viewers do, so while view will be very close to your HTML it may not reflect what users will see.

Other options:

  • if you just care of text/links - parse HTML with Html Agility Pack and extract text/links, than show as you feel necessary
  • if you care about security issues and look of the mail - try to search for libraries that filter out "unsafe" HTML (like external links, script,...) or use Html Agility Pack and filter out everything but content that you consider absolutely safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.