Skip to content

Css Style is being rendered in the mail #55

Description

@070bex424pankaj

If a <style> tag is included in the HTML, the style tag gets removed, and the CSS is rendered directly in the view. I believe we can remove the <style> tag from the email entirely.

Screenshot

Image

Suggested code

module RailsMail
  module EmailsHelper
    def prepare_email_html(raw_html)
      doc = Nokogiri::HTML::DocumentFragment.parse(raw_html)

      # Remove all style tags and their content
      doc.css('style').remove

      doc.css("a[href]").each do |a|
        a.set_attribute("target", "_blank")
        a.set_attribute("rel", "noopener noreferrer")
        a.set_attribute("data-turbo", "false")
      end
      sanitize(doc.to_html,
        tags: ActionView::Base.sanitized_allowed_tags + [ "table", "tbody", "tr", "td" ],
        attributes: ActionView::Base.sanitized_allowed_attributes + [ "style", "target", "data-turbo" ])
    end
  end
end

I think we can remove the embedded <style> tag for now, since in most cases only inline styles are supported.

Let me know what you think @synth

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions