Inline CSS & Premailer
HTML e-mails is one of the most common situations where one needs inline CSS and there’s a ton of powerful ruby gems you can use to tackle this problem. Although it isn’t the only situation you may need inline css. For example, you want to pass your HTML files to another tool/service and it doesn’t support stylesheets. That was me a month ago, I already had my views and stylesheets. The implementation was going great, I just need it a simple way of inlining the stylesheets to be able to send the HTML to an external tool we are using on the project.
My first thought went to roadie, a powerful gem I had use before. But I decided to research another options since on this project we weren’t using any library with this purpose and I had liberty to choose. Among the options I found, I liked premailer because the gem it’s pretty simple but actually really flexible. For example, you can pass your HTML file or the HTML as a string. The same with the stylesheets which is perfect if you want to be able to write your stylesheet on SASS. And the best part is that the setup took me what it lasted to download the gem.
In this particular problem premailer was a really good fit, an easy
to use and flexible library. I didn’t need any hook with
ActionMailer or anything similar. Definetely,
the aspect that convinced me to choose this gem was the community. As good as premailer is, I quickly
realized I need one more option to make it exactly what I needed. I made the changes on the source code, added
proper documentation/tests, submitted a PR and the PR was quickly merged (Yay ).
To sum up premailer rocks #fangirling, hahaha just kidding. Being serious, I learned from this experience that when you find yourself on a not common situation it’s best to just not go with the popular tool and try a couple of different libraries and evaluate things like capabilities, documentation, source code (in case you want to contribute) and community. If you have any comment or question, feel free to leave it on the comment section.