imedo Development Blog

there is no charge for awesomeness

Archive for the ‘actionmailer’ tag

Rails ActionMailer with HTML – Layouts, inline CSS and entity substitution

without comments

For the impatient

Check out the demo application:
http://opensource.imedo.de/htmlmail

Install the plugin:
script/plugin install git://github.com/imedo/awesome_email.git

Learn how to use it below.

Introduction

Have you ever tried sending HTML emails to your users? If you did, you know for sure that it sucks big time: none of the usual ActionView helpers want to work, URL routing is disabled, layouts don’t work, and last but not least, the CSS you wrote for your email simply won’t work in any e-mail client except maybe Apple Mail. To solve all of the above problems, the awesome_email plugin comes to the rescue. Just install it into your vendor/plugins folder, and the rest comes by itself.
If you are interested in what works in which Email client check this link: A guide to css support in Email

What does it do?

There are a few interesting components in awesome_email:

  • awesome_email adds layout support to emails. That means that you can use templates for e-mails just like you would with normal Rails Views.
  • The HTML Mail’s CSS is automatcally inlined. That means that your designer and/or CSS guy can design the email in a web browser without worrying about how it might look like in excotic email clients. Yes, it works in Outlook, too, and no, it doesn’t work in Outlook 2007 without tweaking. The reason is a “stupid decision from Microsoft about Outlook 2007”, but we’re working on that one.
  • ConvertEntities replaces Umlauts and other crazy symbols like ä, Ö etc. with their HTML Entitiy counterparts e.g. ä and so on.
  • HelperMethods allow you to dump the content of the CSS file right into a style tag inside the header of your HTML mail.

How to use it

In your Mailer.delivery_xxx methods you can use

1
2
layout    "template_filename"
css       "css_filename"

to define which layout should be used and which css file should be used to create inline styles

CSS inlining

The cummulated style of each DOM element will be set as an style attribute when using css inlining.

Example:

your css file:

1
2
3
#some-id { font-size:2em; }
.some-class { color:red; }

your template:

1
2
<p id="some-id" class="some-class">Hello World!</p>

will result in the following code:

1
2
<p id="some-id" class="some-class" style="color:red; font-size:2em;">Hello World!</p>

Important!

Be sure to follow these simple conventions or otherwise awesome_emails’s magic will fail:

  • The layout must be located inside app/views/layouts/{mailer_name}
  • If you send mutlipart mails, check out the conventions on how to name your files: http://rails.rubyonrails.com/classes/ActionMailer/Base.html
    • So if you have these files inside of /app/views/{mailer_name}: signup_notification.text.plain.erb, signup_notification.text.html.erb ActionMailer will send a multipart mail with two parts: text/plain and text/html
  • Your CSS file must be inside of /public/stylesheets

Dependencies

gems: rails 2.0.2, hpricot, csspool

Getting it, License and Patches

Get the complete source code through http://github.com/imedo/awesome_email. License is MIT. That means that you can do whatever you want with the software, as long as the copyright statement stays intact. Please be a kind open source citizen, and give back your patches and extensions. Just fork the code on Github, and after you’re done, send us a pull request. Thanks for your help!

ToDo

  • More test coverage (as usual)
  • make it more flexible with view paths
  • rails 2.1 compatibility

Popularity: 100% [?]

Written by tkadauke

August 5th, 2008 at 2:46 pm

ActionMailer subject encoding of multipart e-mails

without comments

We recently stumbled upon a strange behaviour of ActionMailer when we tried to deliver multipart e-mails with an UTF-8 subject.

One would expect a correctly quoted subject along the lines of:

Subject: =?utf-8?Q?=5bBenachrichtigung_von_imedo=2ede=5d_xyz_hat_Sie_gedr=c3=bcckt?=

but instead we got an encoded string without the charset like this:

Subject: =??Q?=5bBenachrichtigung_von_imedo=2ede=5d_xyz_hat_Sie_gedr=c3=bcckt?=

Our investigations showed, that only multipart mails seem to show this behaviour and setting the charset explicitly in the deliver-method of the mailer doesn’t help.

So to get the task done we are currently using quoted_printable directly to encode the subject line, as all the other parts work just fine.


subject  quoted_printable(("[Benachrichtigung von imedo] XYZ hat sie gedrückt", 'utf-8')

We will look into submitting a patch for Rails, but for the time beeing this works just fine and we hope that it will help others that encounter the same problem.

Popularity: 1% [?]

Written by tkadauke

May 16th, 2008 at 1:33 pm

Posted in Uncategorized

Tagged with , , , , , , , , , ,