0

I need to remove CSS of a string from the bodies of E-Mails, such that I get only the real message someone would read from that mail.

Many e-mails contain lots of HTML, which I already was able to strip using striptags. However, the CSS remains. As an example mailbody:

table.footer-table { width: 100%; font-size: inherit; line-height: 18px; text-align: center; }

Dear Sir,

Hello. Kind regards, John

As this is an example, in reality there are lots more CSS classes defined above the actual message.

I do not know what classes etc. will be in such mail (e.g. if it starts with table or whatever).

Question: How can the CSS be efficiently removed from the string?

I have not found a library yet which is able to do that.

11
  • 1
    First put the complete string without stripping off html Commented Oct 9, 2017 at 9:49
  • You can remove the styling from the html by using simple jquery script since most of the styling in emails are done inline. It might be something like $("table").removeAttr("style") Commented Oct 9, 2017 at 9:51
  • @Paudel But this is not a very nice solution if I don't know the classes in advance. Commented Oct 9, 2017 at 9:56
  • @binariedMe I do not know how the mail will look in advance, I am looking for an allrounder solution. Commented Oct 9, 2017 at 9:58
  • you can strip off everything except for the table that you need and then you can go on to strip class attribute like the style one. Commented Oct 9, 2017 at 9:58

1 Answer 1

1

Use a regexp to strip out the <style> block and its content.

Assuming the original content is stored in a variable emailContent, it could be like this:

var striptags = require('striptags');

stripedEmailContent = striptags(emailContent.replace(/<style.*<\/style>/g, ''));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.