views:

37

answers:

3

I want to write an application that sends html formatted email. I have the css and html files as I want them. I'm trying to send the email with the embedded css using the style element like so:

<style type="text/css">
h1 {border-width: 1; border: solid; text-align: center}
</style>

<h1>Title</h1>
<p>Content of the email</p>

It works in some clients (e.g. it works on Mac OSX mail app) and not others (e.g. it doesn't work when reading the email in gmail). When I translate the above to:

<h1 style="border-width: 1; border: solid; text-align: center">Title</h1>
<p>Content of the email</p>

Then it works everywhere. What I'm looking for is a way to place the css as style properties on their corresponding dom elements according the the css rules I defined. So for a given file.css and file.html I want to create a new file result.html which displays correctly but in which all the css is embedded as style properties in the dom elements. Any ideas?

A: 

I want to write an application that sends html formatted email

Never do this. Email MUST be plain text. You cannot even rely on attachments.

Zack
@Zack - unrealistic and reactionary. You do realize that many companies/mailing lists regularly send out html formatted email?
jball
Yes, and people regularly don't get those mails because they are assumed to be spam. I'm not saying mail must be plain text because I'm an old fart, I'm saying mail must be plain text because *nothing else can be relied on*.
Zack
A: 

Drop the style tag, use inline styles.

Nico
@Nico, the OP wants to use inline styles and is asking how to convert/apply the styles from the style tag inline.
Jason McCreary
+1  A: 

This is what you're looking for:

http://www.mailchimp.com/labs/inlinecss.php

Hope this helps!

mattbasta
This is great !!! Would you know of anything that does it programmatically on one of my servers?
rmarimon