# IMAP stuff (or POP)

**URL:** <https://www.ruby-forum.com/t/imap-stuff-or-pop/82297>\
**Category:** Ruby\
**Created:** [January 18, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/imap-stuff-or-pop/82297 "2007-01-18T21:52:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![basic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/58956e/32.png) [@basic](https://www.ruby-forum.com/u/basic)\
**Post date:** [January 18, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/imap-stuff-or-pop/82297/1 "2007-01-18T21:52:01Z")

</div>

I’ve been trying to find a good source of imap or pop information with  
ruby and cant. maybe I’m missing somthing.

as I understand with imap you can select parts of the BODY like [TEXT]  
[1]. I understand what text does but I was underthe impression that [1]  
would select just the plain text. with that does’nt always seem to be  
the case with my test program. most of the results will come in plain  
text then some will contain a both multiparts. So does [1] actually  
do(mean).

What I’m trying to do is strip everything from the mail apart from the  
Enevelope and the plain text. can somebody point me in the right  
direction? this is the line of code that I’ve been using to pull the  
email with

mail.message = imap.fetch(message\_id, ‘BODY[1]’)[0].attr[‘BODY[1]’]

thanks

---

<div class="post-metadata">

**Author:** ![basic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/58956e/32.png) [@basic](https://www.ruby-forum.com/u/basic)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/imap-stuff-or-pop/82297/2 "2007-01-19T16:30:38Z")

</div>

If it is an option for you, try downloading the entire mail and use  
tmail or  
rubymail to parse/split it. Maybe the problem is with your imap server.

[http://www.lickey.com/rubymail/](http://www.lickey.com/rubymail/)  
[http://i.loveruby.net/en/projects/tmail/](http://i.loveruby.net/en/projects/tmail/)

Martin

---

<div class="post-metadata">

**Author:** ![basic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/58956e/32.png) [@basic](https://www.ruby-forum.com/u/basic)\
**Post date:** [January 20, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/imap-stuff-or-pop/82297/3 "2007-01-20T22:48:35Z")

</div>

On Jan 18, 2007, at 12:52, Phil C. wrote:

> What I’m trying to do is strip everything from the mail apart from the  
> Enevelope and the plain text. can somebody point me in the right  
> direction? this is the line of code that I’ve been using to pull the  
> email with
> 
> mail.message = imap.fetch(message\_id, ‘BODY[1]’)[0].attr[‘BODY[1]’]

I think that BODY[1] means grab the first section of the message  
body. You’ll need to figure out which section is text/plain first,  
then grab that section.

I think something like:

header = imap.fetch 397, ‘RFC822.HEADER’  
header.first.attr[‘RFC822.HEADER’] =~ /^Content-Type: .\*$/  
$&

# =\> “Content-Type: multipart/alternative; \r”

Since this is a multipart message, then figure out which chunk is what:

imap.fetch 397, ‘BODY[1.MIME]’

# =\> [#\<struct Net::IMAP::FetchData seqno=397, attr={"BODY

[1.MIME]"=\>“Content-Type: text/plain; charset=ISO-8859-1;  
format=flowed\r\nContent-Transfer-Encoding: 7bit\r\nContent-  
Disposition: inline\r\n\r\n”}\>]

imap.fetch 397, ‘BODY[2.MIME]’

# =\> [#\<struct Net::IMAP::FetchData seqno=397, attr={"BODY

[2.MIME]"=\>“Content-Type: text/html; charset=ISO-8859-1\r\nContent-  
Transfer-Encoding: 7bit\r\nContent-Disposition: inline\r\n\r\n”}\>]

imap.fetch 397, ‘BODY[3.MIME]’

# =\> [#]

So the first chunk is text/plain, grab it:

imap.fetch 397, ‘BODY[1]’  
=\> [#\<struct Net::IMAP::FetchData seqno=397, attr={“BODY[1]”=\>“…”}\>]

See also RFC 3501 section 6.4.5.

[http://www.rfc-editor.org/rfc/rfc3501.txt](http://www.rfc-editor.org/rfc/rfc3501.txt)

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![basic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/58956e/32.png) [@basic](https://www.ruby-forum.com/u/basic)\
**Post date:** [January 20, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/imap-stuff-or-pop/82297/4 "2007-01-20T22:55:36Z")

</div>

On Jan 19, 2007, at 04:01, Martin B. wrote:

> > the case with my test program. most of the results will come in plain
> 
> If it is an option for you, try downloading the entire mail and use  
> tmail or  
> rubymail to parse/split it. Maybe the problem is with your imap  
> server.
> 
> [http://www.lickey.com/rubymail/](http://www.lickey.com/rubymail/)  
> [http://i.loveruby.net/en/projects/tmail/](http://i.loveruby.net/en/projects/tmail/)

No need. The IMAP RFC, like most RFCs, takes a little practice to  
read. Plus you save bandwidth and processing time by only  
downloading the chunks of the message you need.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

A: Yes  
Q: Is top-posting bad?  
— Derek Milhous Zumsteg
