Skip to content

Instantly share code, notes, and snippets.

@develop7
Forked from loopj/smart-mailer.rb
Last active August 29, 2015 14:10
Show Gist options
  • Save develop7/3634e1fcc48822765d1a to your computer and use it in GitHub Desktop.
Save develop7/3634e1fcc48822765d1a to your computer and use it in GitHub Desktop.
# Automatically generates the plain-text version of emails from the html
# template using premailer.
class SmartMailer < ActionMailer::Base
protected
def collect_responses(headers)
responses = super(headers)
if responses.none? { |r| r[:content_type] == 'text/plain' }
html = responses.find { |r| r[:content_type] == 'text/html' }
if html
premail = Premailer.new(html[:body], with_html_string: true)
responses << { content_type: 'text/plain', body: premail.to_plain_text }
end
end
responses
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment