/actionmailer-html2text

Automatically add plain text parts into HTML emails sent by ActionMailer.

Primary LanguageRubyMIT LicenseMIT

actionmailer-html2text Build Status

actionmailer-html2text automatically adds plain text parts to HTML emails sent by ActionMailer, using html2text, and inspired by this blog post.

For example:

<html>
<title>Ignored Title</title>
<body>
  <h1>Hello, World!</h1>

  <p>This is some e-mail content.
  Even though it has whitespace and newlines, the e-mail converter
  will handle it correctly.

  <p>Even mismatched tags.</p>

  <div>A div</div>
  <div>Another div</div>
  <div>A div<div>within a div</div></div>

  <a href="http://foo.com">A link</a>

</body>
</html>

Will be converted into:

Hello, World!

This is some e-mail content. Even though it has whitespace and newlines, the e-mail converter will handle it correctly.

Even mismatched tags.
A div
Another div
A div
within a div
[A link](http://foo.com)

Notes

  1. Any existing text/plain parts will not be overwritten, so you can still create custom text parts with template.text.erb and template.html.erb

  2. Any HTML email emails processed will become multipart in order to add the text/plain part.

  3. Issues and pull requests welcome!

Installing

Add the gem into your Gemfile and run bundle install:

gem 'actionmailer-html2text'

You can now enable automatic text parts on just a single mailer:

class WelcomeMailer < ApplicationMailer
  include ActionMailer::Html2Text                # Just add this

  def welcome_email(to)
    mail(to: to, subject: "Subject")
  end
end

Or you can add it to all of your mailers:

class ApplicationMailer < ActionMailer::Base
  include ActionMailer::Html2Text                # Just add this

  default from: "from@example.com"
  layout 'mailer'
end

Tests

bundle install
rspec

License

actionmailer-html2text is licensed under MIT.

Other versions

  1. openclerk/emails is a PHP e-mail framework that automatically generates text multiparts