The problem with emails is that CSS has to be inline (f.ex. <span style="font-width: ...>) and not in separate CSS files that are referred to through classes – and I don’t think Phoenix does that conversion automatically.
And the CSS class is in another file – which is not linked inside the email message itself – then how would an email client know what to render?
I mean, the OP can make their app use the classic techniques (CSS classes in the HTML elements + separate CSS files) but I don’t think they will achieve 100% parity with how email clients actually display content.
An alternative to inline the css in each line is to use <head><style>...</style></head> in the email document.
So using the <style> tag with css classes prefixed with something the website doesn’t use will allow to keep “parity” in the rendering between the email client and the web app. Bear in mind that email clients don’t keep parity in the rendering between them, thus it may be a good idea to also include the css reset rules inside the <style> tag.
Yes, but that still means that the CSS must be inside the same HTML file that is being sent as email message. There are ways to just refer to external resources – that’s how email marketing campaigns work; the companies know you actually opened the message (if the invisible pixel gets loaded) – but many email clients will refuse to load external CSS so the safest bet is to use one of the few possible approaches of inlining the CSS inside the HTML itself.
I actually do get it and said as much. I simply added that whether you (a) have all CSS in a <head> tag or (b) have all styles inside the HTML elements themselves (via the style="..." attribute) it makes no difference – you still can’t use an external CSS file.
I think I might have asked my original question poorly. So please allow me another shot at it.
So, what I’ve done is created a new, stock Phoenix application that has an emails resource and a string attribute. In that string attribute I’ve stored the complete HTML content of an HTML email that I received. When I render the show page for the email it looks like this:
The CSS that come with the LinkedIn email gets applied to the rest of the site.
What I would like to know is if anyone has any idea on how to render BOTH the site as is with its CSS and then also render the HTML along with its inline CSS without the site or the email overriding each other’s design.
Hope that’s a little more clear and I also hope someone might have an idea on how to do this.
and this is how you leak the CSS into your entire page. To avoid that, you should use an iframe. Iframe helps you embedding a site in your site in complete isolation. To do that, you will need to do a couple of things.
Add new route, for example get "/email/iframe/, MyController, :email_iframe
Add email_iframe/2 to the MyController file.
Add whatever code you need to retrieve the email record and make assignments.
Put a layout with Phoenix’s put_layout/2, for example conn = put_layout(conn, "email_iframe.html").
Create the new layout with following content:
<%= @inner_content %>
And lastly update your initial template to be something like this: