SilverNightaFall
SilverNightaFall

Reputation: 4170

Parse body of an email using Ruby on Rails

How do you parse out just the body of an email using Ruby on Rails? Below is what I have come up with. I have down to the subject with the body, but all I want is the body. I am trying to parse this without using TMail or email based libraries.

show

<p>Body: <%= @text.parse_body %></p>

controller

  def show
    @text = Text.find(params[:id])
  end

model

class Text < ActiveRecord::Base
  attr_accessible :email


  def text_input
      @raw_email = email.to_s
  end

  def parse_body
    @parse_email = @raw_email.match(/(\r\n\r\n[\W\w\S\s\D\d\b]*\r\n)/i)
    @parse_body = @parse_email.to_s.gsub(/<[\W\w\S\s\D\d\b]*?>/, "")
  end

result @parse_email

\r\n\r\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\r\n<HTML>\r\n<HEAD>\r\n<META   
HTTP-EQUIV=3D\"Content-Type\" CONTENT=3D\"text/html; =\r\ncharset=3Diso-8859-1\">\r\n<META  
NAME=3D\"Generator\" CONTENT=3D\"MS Exchange Server version 
=\r\n6.5.7654.12\">\r\n<TITLE>Test for long subject</TITLE>\r\n</HEAD>\r\n<BODY>\r\n<!-- 
Converted from text/plain format -->\r\n\r\n<P><FONT 
SIZE=3D2>Test<BR>\r\n<BR>\r\n</FONT>\r\n</P>\r\n\r\n</BODY>\r\n" 1:"\r\n\r\n<!DOCTYPE HTML 
PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\r\n<HTML>\r\n<HEAD>\r\n<META HTTP-EQUIV=3D\"Content-
Type\" CONTENT=3D\"text/html; =\r\ncharset=3Diso-8859-1\">\r\n<META NAME=3D\"Generator\" 
CONTENT=3D\"MS Exchange Server version =\r\n6.5.7654.12\">\r\n<TITLE>Test for long 
subject</TITLE>\r\n</HEAD>\r\n<BODY>\r\n<!-- Converted from text/plain format --
>\r\n\r\n<P><FONT SIZE=3D2>Body<BR>\r\n<BR>\r\n</FONT>\r\n</P>\r\n\r\n</BODY>\r\n

Result of @parse_body

Body: Test for long subject Body

Result I am trying to get

Body

Upvotes: 1

Views: 3519

Answers (1)

Peter
Peter

Reputation: 132207

You should use a gem that does this for you, such as mail! You could do this:

mail = Mail.read(@raw_email)
mail.subject
mail.body

etc. Best to avoid writing your own parser in situations like this, if possible!

Note: I guess you say that you want to avoid mail libraries, but why? It will help a lot. This one is pure ruby, too.

Upvotes: 6

Related Questions