How to receive and display HTML emails with stylesheets? - css

When developing a web-based email system, is there a generally accepted correct way to handle style sheets on incoming emails? I am referring to the <style tag. Not referring to #import or <link notation.
These stylesheets have to be restricted to only the element containing the foreign email, or else they will also adjust the content of your own system, which is a big no-no.
I am aware that I should sanitize scripts and stylesheets, which will allow me to prevent url background images, or fixed position elements that would cover parts of my system.
I would not want to drop the stylesheet completely, so I see these options
Convert style sheets to inline styles
Put the entire foreign email in its own div with an ID, and change the style rules to only affect that div (for example a,b{color:purple} would become #foreign a,#foreign b{color:purple})
Are either of these good ideas? Is there an obvious/better way that I am missing? What is common/accepted/robust?

Email doesn't support external style sheets, so all CSS on a html email is in the page style tag, or inline. As it is standard for clients like gmail and yahoo to strip the style tag, and everything else outside the body tags, you could simply do the same. Limiting incoming emails to inline styles will keep all CSS local to those elements. It is pretty much email design 101 to inline the css, so you don't have to worry about your email system being less than what is standard.
Web clients also strip many CSS elements in addition to this. Don't want someone adding position:fixed; for example as it would break out of the viewing pane. The best reference for what CSS is allowed in each client is Campaign Monitor's CSS guide.

I think the best idea is use inline styles. I have made may web sites that send styled html email and inline styles is the only thing tha work in most email sistemas.

Related

Hide content in Outlook with external CSS

I inherited a WordPress plugin that sends an RSS feed of content to Mailchimp to generate an email. This code (which I cannot find the source) is adding an extra logo image which is throwing off the formatting. I know I should add something like
<!--[if !mso 9]><!-->
to the code if I want to hide it in Outlook, but I cannot find the source to add this. I can only add external CSS. The usual display: none works in the other email platforms. Any advice on how to remove this extra image instance in Outlook via external CSS?
The code you provided is an HTML conditional comment. It can only work embedded in the HTML code. If you want to hide an element from an external stylesheet in the Outlooks on Windows (from 2007 and above), you can use the mso-hide:all property. It’s basically a display:none but for Word’s rendering engine. Although, contrary to display:none, this doesn’t always inherit to <table> children elements. In that case you could use a double selector like the following.
.your-element,
.your-element table {
mso-hide: all;
}

Remote Content - Blocking CSS Overrides

I am working on a system that will allow users to embed content into a web page that they have access to.
Conceptually the user would create a div with a specific id (let's call it "myId") on their page, and include a JavaScript file that I control which would basically inject markup into "myId".
The markup returned would include divs with inline styles to them. I can't allow the users who are embedding this content into their page override the CSS styles.
I've read hundreds of articles and threads about CSS Specificity, !Important, etc. It seems the only true way to block users from overriding your styles is to use iFrames.
Below are three articles a few co-workers and I have been bouncing around.
http://weblog.bocoup.com/3pjs-css-defense/
https://speakerdeck.com/antonkovalyov/achieving-harmony-with-third-party-javascript
http://weblog.bocoup.com/3pjs-css-delivery/
One of the concerns is that iOS will stop supporting iFrames, and honestly we've all been told for years not to use iFrames. While it seems like the perfect and only solution it really does sound "hacky".
Does anyone have any insight into any other solutions? I looked at how linkedIn embeds their "connect with linkedIn button" and they are simply wrapping every property value with an !important (which a) seem super hacky and b) doesn't account for undeclared properties and values).
Your main concern with !important is that they can still override it with a later declaration using the !important keyword.
You are right about iFrames.
So you are not left with a lot of options:
one option is to prepend your own CSS declarations with a custom non-generic package-like name so that the chances the user will override them will be very slim:
.content h1
will become
.my-custom-css .content h1
the other option is to modify their css markdown instead of yours. Prepend each of their declarations with a specific css class and use that class for their div container so it applies to all of its contents.

Outlook 2007/2010 & GMail Paragraph Spacing Issues in Newsletters

I'm getting double spacing in Outlook 2007/2010/2013 and GMail but it looks fine elsewhere. I'm creating a newsletter and I've tried all-sorts: border-collapses, margins, paddings, spans, and still can't get my head around this. Can anyone see where I'm going wrong? Am I missing something?
Link below - source code too long to paste here. Sorry.
http://pastebin.com/FW3RgYuv
For one, all styles must be inline. Most mail clients do not support stylesheets.
You are not building a web page, you're building an email, therefore everything in your HEAD section will be removed, including your embedded stylesheet.
In you code you have </br>. This is not a valid tag. Use <br />.
NEVER NEVER use !important. Inline styles already override other styles, plus you can dig yourself into a dead end.
Put your text in a DIV or a P and style that, not the TD. Not all styles are supported by every mail client. Here's a good list to compare to.

Safe markup for HTML email

Email clients are limited in their HTML display capabilities.
What HTML markup and CSS styles is it safe to use in HTML-formatted email?
In general you want to stick to 10+ years old HTML.
Avoid trying to link to external stylesheets and avoid styles in the HEAD.
Use inline styles.
Use HTML tables for layout.
Industry standard is to stick to width of 600px or less for your email content.
This is a good guide: http://kb.mailchimp.com/article/how-to-code-html-emails
Some useful links on this subject detailing which clients support what markup and style:
http://www.campaignmonitor.com/css/
http://www.email-standards.org/
Places you can get help building cross-client HTML e-mail:
https://www.mailrox.com/
https://www.getfractal.com/

Email clients ignoring internal style sheet

Best practices in normal web development call for putting your styles between style tags or loading a style sheet; however, I've found out that several email clients will ignore any style tags and only execute inline styles (www.campaignmonitor.com). I can deal with that, but I'm not sure if CSS supports inline media queries. I would like my email to display a little different on the desktop. Is something similar to this supported?:
<div id="myDiv" style="#media screen and (max-width:480px;){ color:black; }"></div>
HTML emails are an entirely different beast. You need to code them like it is 1999. Use a very limited set of tags and make sure all or your styles are inline. Use tables for your layouts.
To make use of media queries you need to do both.
What I recommend doing is to first create your email with all inline styles. Then when you are happy with it you can add support for mobile.
To add support for mobile add the media queries to the head tag and use !important to override any inline styles.
Here is an example:
Optimizing for mobile email
Here is a helpful chart that details which css support for email clients.
http://www.campaignmonitor.com/css/
I don't think they work inline like that, you would probably have to embed the stylesheet in the HTML email template itself (e.g. <style>#media {...}</style>).
Even that seems like a VERY dodgy thing to rely on. HTML emails are a huge pain in the ass because standards and CSS support are about a decade behind and there is massive variation among popular email clients in terms of which properties and types of styling are supported.
In general keeping it simple and using old school table based layouts with all inline CSS is the way to go. Campaign Monitor has a great chart of support for various CSS properties in major email clients and devices.
A common workaround to the problem of HTML not rendering correctly in email clients is to do the best you can and have a prominent link at the top that says: "Email not displaying correctly? View it in your browser."
Ray Kurzweil's weekly newsletter blast does this, and it's very well formed, and well done, and I always read it. (It's a brilliant blend of content and presentation).

Resources