# Retrieving e-mail from a gmail label

**URL:** <https://www.ruby-forum.com/t/retrieving-e-mail-from-a-gmail-label/176481>\
**Category:** Ruby\
**Created:** [October 1, 2009, 6:10am UTC](https://www.ruby-forum.com/t/retrieving-e-mail-from-a-gmail-label/176481 "2009-10-01T06:10:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [October 1, 2009, 6:10am UTC](https://www.ruby-forum.com/t/retrieving-e-mail-from-a-gmail-label/176481/1 "2009-10-01T06:10:59Z")

</div>

I’m trying to download all the e-mails under a particular label in a  
g-mail account. Using net/imap I am able to pull parts of e-mails  
(header, body, subject, etc) from a specific label by using:

require ‘rubygems’  
require ‘net/imap’  
require ‘tmail’

username = ‘username’  
password = ‘password’  
gmail\_label = ‘Books’

imap = Net::IMAP.new(‘[imap.gmail.com](http://imap.gmail.com)’, port = 993, true, nil, false)  
imap.login(username,password)  
imap.select(gmail\_label)  
imap.search([“NOT”, “DELETED”]).each do |m|  
envelope = imap.fetch(m, “ENVELOPE”)[0].attr[‘ENVELOPE’]  
puts “#{envelope.from[0].name}: \t#{envelope.subject}”  
puts imap.fetch(m, “BODY[TEXT]”)

end  
imap.logout()  
imap.disconnect()

However, when I print the body portion, it prints with newline and  
return characters etc. What I am after is a clean version of the body.  
Is there a method that returns the entire e-mail as a string? If so I  
can use TMail to parse it. Alternatively, does TMail have a method to  
parse just the body?

Thanks.
