Created
May 17, 2012 14:57
-
-
Save dblock/2719486 to your computer and use it in GitHub Desktop.
Insert an automatic text MIME part into HTML e-mail.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Insert an automatic text MIME part into HTML e-mail. | |
# (c) Daniel Doubrovkine, Art.sy 2012 | |
# MIT License | |
# | |
class ActionMailerWithTextPart < ActionMailer::Base | |
def collect_responses_and_parts_order(headers) | |
responses, parts_order = super(headers) | |
html_part = responses.detect { |response| response[:content_type] == "text/html" } | |
text_part = responses.detect { |response| response[:content_type] == "text/plain" } | |
if html_part && ! text_part | |
body_parts = [] | |
Nokogiri::HTML(html_part[:body]).traverse do |node| | |
if node.text? and ! (content = node.content ? node.content.strip : nil).blank? | |
body_parts << content | |
elsif node.name == "a" && (href = node.attr("href")) && href.match(/^https?:/) | |
body_parts << href | |
end | |
end | |
responses.insert 0, { content_type: "text/plain", body: body_parts.uniq.join("\n") } | |
parts_order.insert 0, "text/plain" | |
end | |
[responses, parts_order] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Rails 4: