Saturday, September 8, 2007

Stylesheets in ActionMailer

I love ActionMailer. When sending html emails you might want to reuse your existing CSS stylesheet(s). There are potential problems with doing this (see CampaignMonitor for a definitive run down on styling for email clients), but you might be clever enough to have a page layout that is directly usable for html emails, too.

Most/all email clients won't import a linked stylesheet like a browser does, instead you have to embed it directly in the email as an inline style.

To embed your styles in the email insert something like this in the <head> of your email template:

<style type="text/css">
<%= render :file=>"../../../public/stylesheets/admin.css", :use_full_path=>true %>

Then you can go ahead and use the similar layout as your Web pages in your email. As the Campaign Monitor guys will tell you, you want to avoid floated elements and any information in rendered images. This usually means you'll make a table-based layout for the email but you can reuse styling from the Web pages.

No comments: