开发者

How to remove the Begin forwarded message: part of an email body in a rails 3 app

I am receiving forwarded email into my rails 3 app and is working ok.

But in a forwarded email it includes the following message at the top for example :

Begin forwarded message:

From: roger rabbit <sales@mysite.com>
Date: 23 May 2011 13:52:08 GMT+01:00
To: sam@yoursite.com
Subject: Hi threre

Dear Mike 

Yes please do the work for me!

This format changes as to which email client sent the message.

I want to strip this out of the body so i can show just the forwarded message like so :

Dear Mike 

Yes please do the work for me!

What is the best way to do this as i know i can开发者_高级运维 do it for this specific format but i need it to be more general / clever.

Is there some gem / lib i should be using ?

Any help would be great.

thanks Rick


I haven't heard of a gem or library that would help for this, but my first crack at this would be:

  1. Tackling the "Begin forwarded message:" that varies between clients could be the tricky one.
  2. Tackling the email headers should be easier as they should be predictable.

For number 1: I would take a guess that in most (hopefully all) cases, you'd want to discard any lines above the "From:" line. So, find the line number that "From:" is on (via regex probably), delete all lines above that. This would avoid having to get all variations of "Begin forwarded message:" that different email clients would come up with.

For number 2: Have a regex that checks for the email header information. Something that checks for "From: " or "To: " etc tied to the start of the line and then delete that line if it matches.

I would try to get as many test cases (example forwarded emails) as possible to make sure you're system is working correctly. Once you have a few specific examples that you're stuck on, we may be able to help further.


I think you should use split method of a String class. Try something like this:

in example, a - variable contains your message.

b = a.split("\n\r\n", 3) maybe b = a.split("\n\n", 3)

he will divide your file into substrings based on a delimiter (\n\n in you case) and return an array of these substrings.

So, finally you should simply do following:

c = b[2].to_s 

puts c => "Dear Mike \n\nYes please do the work for me!\n"

This is not a good solution, but this should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜