19

I am trying to create an application that will send out style-heavy emails and have required clients working except Google's Gmail. I researched the issue and it looks like Gmail strips out CSS from external files or CSS nested in the 'style' tag. Does an easy way exist of moving style from an external file to being inline?

Something that will take:

<style>
  .wide { width: 100px; }
  .cell { display: block; }
</style>
<span class="wide cell">Sample</span>

And convert it to:

<div class="wide cell" style="width: 100px; display: block;">Sample</div>

Thanks!

3
  • 1
    There's no source available, but here's a link anyway: beaker.mailchimp.com/inline-css Commented May 24, 2011 at 16:38
  • @thirtydot This is exactly what I'm looking for! Do you know if they offer an API? Commented May 24, 2011 at 17:08
  • 1
    I don't know. There's probably a similar tool available that has source code available. I'd Google for "style inliner", and see if there's anything available with source. If it's Ruby, great for you, otherwise you'll have to port it. I don't know how often you'd want to use the inlining feature (or how "mission critical" it is), but it might be worth considering the quick and easy approach of simply sending a direct POST request (like this) to here: beaker.mailchimp.com/inline-css Commented May 24, 2011 at 17:42

3 Answers 3

42

Here are couple of gems you can check out:

I have no winner at the time writing this answer but premailer seems to be most up-to-date.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I already went with premailer (see answer). Cheers!
There's now also Roadie - a fork of mail_style.
If you are on Rails, I recommend Roadie. Couldn't be much easier to implement.
There also exists a premailer-rails gem which I was able to quickly put in place without problems. (I have no idea how it compares to Roadie.)
Thanks @JasonSwett I've added it to the list.
|
6

Added premailer:

def premailer(message)
  message.text_part.body = Premailer.new(message.text_part.body.to_s, with_html_string: true).to_plain_text
  message.html_part.body = Premailer.new(message.html_part.body.to_s, with_html_string: true).to_inline_css

  return message
end

def welcome(user)
  @user = user

  message = mail ...
end

Comments

4

There's just one problem with your reasoning.....many styles, even inline, aren't supported.

Here's a reference for what is supported in email

In your example, you use the display: tag, which isn't supported by Outlook 07+

My company sends out thousands of emails a day and I often have a hand in building them. In practice, inline styles work, but it's not as simple as just in-lining everything and it will work. You have to be extremely careful about what you use and how you use it. I've resorted to my beginnings, doing nearly everything in pure HTML with tables for layouts. It's basically the only way I've found to get things to work nearly 100% of the time.

If you're building this functionality into an app that's going to get a lot of use, I'd also strongly recommend building in Email on Acid via their API. While you can code a very good quality output, Microsoft will no doubt find some way to make your valid code not work. Email on Acid will render using whatever madness Microsoft is using at the time to show you if your email works. It's pure genius and required use for those who are serious about sending a ton of emails. And no, I don't work for the company....

1 Comment

Thanks. I have no problem modifying a stylesheet to only use supported files, however I don't want to move all the styles inline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.