Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - Parsing an Email Message for just the reply, not the old thread?

I set my app to receive incoming emails via a post from a service. The controller that receives the posts looks a little like this:

    class IncomingMailsController < ApplicationController    
      require 'mail'
      skip_before_filter :verify_authenticity_token

      def create
        message = Mail.new(params[:message])
        message_plain = (params[:plain])
        Rails.logger.info   'message2.plain:'
        Rails.logger.info   message2

        render :text => 'success', :status => 200 # a status of 404 would reject the mail
      end
    end

That successfully is delivering the entire email message, replies,forward history etc. The issue is I'd like to be able to extract just the actual reply text.

Currently I get:

That's not a bad idea. Lets try that out.

On Nov 17, 2010, at 4:18 PM, XXXXX @ XXXXXXXX wrote:
> There's a new reply:

And I'd like to know how rails devs get just the reply:

That's not a bad idea. Lets try that out.

Ideas? Thanks

like image 562
AnApprentice Avatar asked Nov 18 '10 01:11

AnApprentice


1 Answers

There's no guaranteed way to get the entire message however it's common practice to make use of a separator and then use some code to parse out the response line.

If you take a look at the code in the open source project teambox for example you see the something really similar to the following:

def strip_responses(body)
# For GMail. Matches "On 19 August 2010 13:48, User <[email protected]<proj%2Bconversation%[email protected]>> wrote:"
body.strip.
  gsub(/\n[^\r\n]*\d{2,4}.*\+.*\[email protected].*:.*\z/m, '').
  split("---------separator---------").first.
  split("<div class='email'").first.
  strip
end
like image 65
Steve Smith Avatar answered Oct 13 '22 22:10

Steve Smith