E-mail is one of the most important ways to engage your users. And every time you touch a user's inbox, it reflects on your brand. But getting email right has become increasing difficult due to the complexities introduced by the thousands of web-based, desktop and mobile mail clients. Email formatting is like the "Hunger Games" where the major players include online services such as GMail, Yahoo, Hotmail or AOL, desktop clients such as Outlook and a myriad mobile devices ranging from iPhone and Android to Blackberry.
To deal with this landscape, the MIME standard allows systems to send e-mail with multiple parts:
plain/text for business-efficient devices such as the Blackberry, and
text/html for web-based e-mail readers, such as GMail. Furthermore,
ActionMailer supports multiple template formats: create an
.html.haml template along with a
.txt.haml template to generate both. We also know that
text/plain email helps deliverability, but we believe a disproportionately small amount of text e-mails are actually read - the vast majority of devices are capable of parsing some HTML.
Is it possible to avoid having to maintain two separate templates without sacrificing deliverability? How can we inject a
text/plain part into HTML e-mail that is both useful and "free"?
ActionMailer::Base defines an internal method called
collect_responses_and_parts_order (#ref), which iterates over templates and renders them. Let's override that method and examine the contents of the generated parts.
1 2 3 4
response is a MIME part with its boundary and the
parts_order is the order in which the parts appear in the final e-mail. The MIME RFC 1341 says that the parts must be generated in the increasing order of preference, ie.
text/html content-type part last, provided you want it to be the preferred format of your email.
We can find whether the generated e-mail contains a
plain/text part and otherwise generate one.
1 2 3 4 5
Generating the text part means stripping all HTML with links preserved. Nokogiri has a very convenient deep
1 2 3 4 5 6 7 8
Once we have all the parts, assemble them, get rid of duplicate text and links, and re-insert into the email as a
text/plain multipart block.
1 2 3 4 5
This has been extracted into the actionmailer-text gem. Include
ActionMailer::Text in your mailers.