ChiliProject is not maintained anymore. Please be advised that there will be no more updates.

We do not recommend that you setup new ChiliProject instances and we urge all existing users to migrate their data to a maintained system, e.g. Redmine. We will provide a migration script later. In the meantime, you can use the instructions by Christian Daehn.

incoming HTML email with no plain text enhancement to mail_handler.rb

Added by Anthony Ordner at 2011-11-15 05:20 pm

I have been having problems with Exchange / Outlook sometimes sending only the HTML portion of the email and it was a real mess trying to read it with all of the tags stripped. I hacked an enhancement that first converts the </p> and </br> tags to \n and then strips the tags. This is much nicer to read. It changes plain_text_body within mail_handler.rb... I thought I would share in case it is helpful. Please remember this is a hack job as I do not know much about Ruby yet but have done a fair share of other programming over the years.... Be kind.

So instead of this:

Just testing again. Just testing again 2.Just testing again 3. Part 1. Part 2. Thank youtony Anthony Ordner

I now get this:

Again
Again, testing, again

Testing

tony

Anyhow enjoy if it helps...

tony

 1  # Returns the text/plain part of the email
 2  # If not found (eg. HTML-only email), returns the body with tags removed and now preserves line breaks so it is readable
 3  def plain_text_body
 4    return @plain_text_body unless @plain_text_body.nil?
 5    parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
 6    if parts.empty?
 7      parts << @email
 8    end
 9    plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
10    if plain_text_part.nil?
11      # no text/plain part found, assuming html-only email
12      # convert </p> to \n newline
13      @plain_text_body = @email.body.to_s.gsub("</p>", "\n");
14      # convert </br> to \n newline
15      @plain_text_body = @plain_text_body.gsub("</br>", "\n");
16      # strip html tags
17      @plain_text_body = strip_tags(@plain_text_body)
18      # remove doctype directive
19      @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
20    else
21      @plain_text_body = plain_text_part.body.to_s
22    end
23    @plain_text_body.strip!
24    @plain_text_body
25  end

Replies (2)

RE: incoming HTML email with no plain text enhancement to mail_handler.rb - Added by Felix Schäfer at 2011-11-27 06:40 pm

I'm not sure I really like this kind of solution. Doesn't redcloth (the textile library) a function to convert html to somewhat equivalent textile?

Thanks for sharing though, I'm sure you're not the only one having to use Exchange/Outlook ;-)

RE: incoming HTML email with no plain text enhancement to mail_handler.rb - Added by Anthony Ordner at 2011-11-28 12:27 am

I did a fair amount of googling and could not find much going from html to text.... most of it was text to html. If there is a cleaner way let me know.

Wowsher aka tony

(1-2/2)