# Extract data from email - Tmail, Hpricot

**URL:** <https://www.ruby-forum.com/t/extract-data-from-email-tmail-hpricot/147719>\
**Category:** Ruby\
**Created:** [September 4, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/extract-data-from-email-tmail-hpricot/147719 "2008-09-04T17:03:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![geo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f4b2a3/32.png) [@geo](https://www.ruby-forum.com/u/geo)\
**Post date:** [September 4, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/extract-data-from-email-tmail-hpricot/147719/1 "2008-09-04T17:03:09Z")

</div>

Hi all,

I have an html email that I would like to parse.

The problem I’m having is removing all html tags and getting past the  
header information. Then I want to extract all the information per row  
to put into a database.

the email is pasted here: [http://pastie.textmate.org/265259](http://pastie.textmate.org/265259)

I have tried Tmail, but can’t seem to extract just the body. Then I  
tried Hpricot and wasn’t sure what to use before the .inner\_html. So  
basically I’m very lost on where to start.

Any help is appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![geo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f4b2a3/32.png) [@geo](https://www.ruby-forum.com/u/geo)\
**Post date:** [September 4, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/extract-data-from-email-tmail-hpricot/147719/2 "2008-09-04T20:42:28Z")

</div>

George Cooper wrote:

> I have tried Tmail, but can’t seem to extract just the body. Then I  
> tried Hpricot and wasn’t sure what to use before the .inner\_html. So  
> basically I’m very lost on where to start.
> 
> Any help is appreciated.
> 
> Thanks!

It would help if you posted some code that didn’t work, so people can  
have a better idea of what you’re trying to do. Tmail should have been  
able to parse that without problem, however, extracting the body is  
easy. The box follows the empty line. You could use something like  
split, but duping such huge strings could be slow. When you read the  
mail, try to read a line at a time until you get the empty line, then  
read the rest into a buffer for hpricot.

---

<div class="post-metadata">

**Author:** ![geo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f4b2a3/32.png) [@geo](https://www.ruby-forum.com/u/geo)\
**Post date:** [September 8, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/extract-data-from-email-tmail-hpricot/147719/3 "2008-09-08T17:38:06Z")

</div>

Michael M. wrote:

> George Cooper wrote:
> 
> > I have tried Tmail, but can’t seem to extract just the body. Then I  
> > tried Hpricot and wasn’t sure what to use before the .inner\_html. So  
> > basically I’m very lost on where to start.
> > 
> > Any help is appreciated.
> > 
> > Thanks!
> 
> It would help if you posted some code that didn’t work, so people can  
> have a better idea of what you’re trying to do. Tmail should have been  
> able to parse that without problem, however, extracting the body is  
> easy. The box follows the empty line. You could use something like  
> split, but duping such huge strings could be slow. When you read the  
> mail, try to read a line at a time until you get the empty line, then  
> read the rest into a buffer for hpricot.

Below is the code I am using to try and get the body out of the html  
email (copy of email [http://pastie.org/265259](http://pastie.org/265259)) .

require ‘rubygems’  
require ‘tmail’

email = TMail::Mail.load( ‘emailhtml.eml’ )

puts email[‘body’] # comes back nil  
puts email[‘from’]  
puts email[‘Delivered-To’]  
puts email[‘to’] # comes back nil  
puts email[‘subject’]  
puts email[‘date’]  
puts email[‘X-Originalarrivaltime’]

results:  
nil  
removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid  
nil  
[Freddy] New Incidents captured on 2008-09-02  
Tue, 2 Sep 2008 19:05:00 -0400  
02 Sep 2008 23:10:35.0578 (UTC) FILETIME=[1B2659A0:01C90D51]

---

<div class="post-metadata">

**Author:** ![geo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f4b2a3/32.png) [@geo](https://www.ruby-forum.com/u/geo)\
**Post date:** [September 9, 2008, 1:00am UTC](https://www.ruby-forum.com/t/extract-data-from-email-tmail-hpricot/147719/4 "2008-09-09T01:00:24Z")

</div>

On Sep 8, 11:31 am, Geo \_C [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > It would help if you posted some code that didn’t work, so people can  
> > require ‘rubygems’  
> > require ‘tmail’
> 
> email = TMail::Mail.load( ‘emailhtml.eml’ )
> 
> puts email[‘body’] # comes back nil

Don’t see why it would be nil. I would contact Mikel.

[http://lindsaar.net/](http://lindsaar.net/)

> puts email[‘from’]  
> puts email[‘Delivered-To’]  
> puts email[‘to’] # comes back nil

I don’t see a ‘to’ in the header, so is this a surprise?

> Tue, 2 Sep 2008 19:05:00 -0400  
> 02 Sep 2008 23:10:35.0578 (UTC) FILETIME=[1B2659A0:01C90D51]

T.

---

<div class="post-metadata">

**Author:** ![geo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f4b2a3/32.png) [@geo](https://www.ruby-forum.com/u/geo)\
**Post date:** [September 11, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/extract-data-from-email-tmail-hpricot/147719/5 "2008-09-11T18:36:07Z")

</div>

I got Tmail to extract the body of my email. The solution (very simple  
and embarrassing) is below. Now I’m trying to figure out Hpricot, but  
examples seem to be fairly thin. If anyone knows of a good tutorial for  
beginners, please post. I have been using  
[http://code.whytheluckystiff.net/doc/hpricot/](http://code.whytheluckystiff.net/doc/hpricot/) , but could use something  
more basic.

Thanks for the help!

Thomas S. wrote:

> On Sep 8, 11:31ï¿½am, Geo \_C [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > It would help if you posted some code that didn’t work, so people can  
> > > require ‘rubygems’  
> > > require ‘tmail’
> > 
> > email = TMail::Mail.load( ‘emailhtml.eml’ )
> > 
> > puts email[‘body’] # comes back nil
> 
> Don’t see why it would be nil. I would contact Mikel.

I needed to use email.body instead of email[‘body’] to return the body.  
thanks Peter!

> [http://lindsaar.net/](http://lindsaar.net/)
> 
> > puts email[‘from’]  
> > puts email[‘Delivered-To’]  
> > puts email[‘to’] # comes back nil
> 
> I don’t see a ‘to’ in the header, so is this a surprise?

My mistake there. You are correct, there is no ‘to’ for me to use.

> > Tue, 2 Sep 2008 19:05:00 -0400  
> > 02 Sep 2008 23:10:35.0578 (UTC) FILETIME=[1B2659A0:01C90D51]
> 
> T.
