Created
August 31, 2012 12:32
-
-
Save papricek/3552200 to your computer and use it in GitHub Desktop.
Auto text part
This file contains hidden or 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
# You just put this in config/initializers. Dont create text.erb files! ;) | |
# Text parts are auto-generated from html.erb templates. | |
include ActionView::Helpers::SanitizeHelper | |
class TextMailInterceptor | |
def self.delivering_email(message) | |
html = message.html_part.try(:body).try(:to_s) || message.body.to_s | |
text = HTMLEntities.new.decode(html).gsub(/<a.+?href=("|')([^"']+).*?>(.+?)<\/a>/, "\\2: \\3") | |
attachments = message.attachments | |
message.body = '' | |
if message.multipart? | |
message.part :content_type => "multipart/alternative", :charset => message.charset do |p| | |
p.part :content_type => "text/plain", :body => strip_tags(text), :charset => message.charset | |
p.part :content_type => "text/html", :body => html, :charset => message.charset | |
end | |
else | |
message.html_part = Mail::Part.new({:content_type => "text/html", :body => html, :charset => message.charset}) | |
message.text_part = Mail::Part.new({:content_type => "text/plain", :body => strip_tags(text), :charset => message.charset}) | |
end | |
attachments.each do |attachment| | |
message.add_part(attachment) | |
end | |
end | |
end | |
ActionMailer::Base.register_interceptor(TextMailInterceptor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment