0

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 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?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Ricardo Marimon
  • 10,339
  • 9
  • 52
  • 59

4 Answers4

1

This is what you're looking for:

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

Hope this helps!

mattbasta
  • 13,492
  • 9
  • 47
  • 68
0

Drop the style tag, use inline styles.

bevacqua
  • 47,502
  • 56
  • 171
  • 285
0

I have the same issue - I have a php app that sends out a confirmation email once a customer has placed an order. In various email clients it's fine, but web based clients tend to strip out the HEAD tag, which includes the STYLE tag - so any style is lost.

While it's still a good idea, as @Zack mentions, to include a plain text version of what you wanted to say, nobody likes to read plain text. I doubt that Zack is reading Stack Overflow on Lynx, for example.

A quick Google search for 'CSS inliner php' brings up: http://classes.verkoyen.eu/css_to_inline_styles

Also it seems that this question has been asked before on stackoverflow (at least once), at least for php, and there was a Ruby answer given in php class to inline css styles?

Community
  • 1
  • 1
Arunas
  • 1,282
  • 1
  • 11
  • 17
-1

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.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 3
    unrealistic and reactionary. You do realize that many companies/mailing lists regularly send out html formatted email? – jball Oct 28 '10 at 02:56
  • 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*. – zwol Oct 28 '10 at 03:14