# Understanding net/imap and multi-part e-mails

**URL:** <https://www.ruby-forum.com/t/understanding-net-imap-and-multi-part-e-mails/63222>\
**Category:** Ruby\
**Created:** [June 13, 2006, 10:13am UTC](https://www.ruby-forum.com/t/understanding-net-imap-and-multi-part-e-mails/63222 "2006-06-13T10:13:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [June 13, 2006, 10:13am UTC](https://www.ruby-forum.com/t/understanding-net-imap-and-multi-part-e-mails/63222/1 "2006-06-13T10:13:18Z")

</div>

Hello:

I would like to use Net::IMAP to connect to an IMAP server and produce a  
list of unread mail messages. For each message, I would like to know 1)  
the sender, 2) the subject and 3) a snippet of the body (i.e., first few  
lines).

I’ve managed to figure out 1 and 2 (sender and subject) but am stumped  
on 3. For a plain text message, I see that I can do something like this:

message = imap.fetch(message\_id, [“ENVELOPE”, “RFC822.TEXT”])[0]  
body = message.attr[“RFC822.TEXT”]  
snippet = body[0, 100] # first 100 characters

But what about a multi-part MIME message? I’ve noticed that by fetching  
BODYSTRUCTURE, I can get the parts of the mail message. Is there a way  
to extract what I want from the BodyTypeText or BodyTypeMultipart  
objects? I’ve noticed that these objects give the correct size and  
number of lines, etc., but they don’t seem to hold the actual plain text  
or HTML content.

I’ve read the docs a few times, but think that I’m missing something  
fundamental here. How do I use Net::IMAP as-is to do what I want?

Thanks in advance.

–Ed Lau

---

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [June 13, 2006, 10:23am UTC](https://www.ruby-forum.com/t/understanding-net-imap-and-multi-part-e-mails/63222/2 "2006-06-13T10:23:51Z")

</div>

> > > > > “E” == Ed Lau [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

E\> But what about a multi-part MIME message?

You have an example in comp.lang.ruby

Thread “Retrieve email attachments using Net::IMAP”

```
  message <removed_email_address@domain.invalid>

```

For example

```
  puts imap.fetch(message_id, "BODY[2]")

```

Guy Decoux

---

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [June 13, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/understanding-net-imap-and-multi-part-e-mails/63222/3 "2006-06-13T19:40:02Z")

</div>

ts wrote:

> > > > > > “E” == Ed Lau [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> E\> But what about a multi-part MIME message?
> 
> You have an example in comp.lang.ruby
> 
> Thread “Retrieve email attachments using Net::IMAP”
> 
> ```
> message <removed_email_address@domain.invalid>
> 
> ```
> 
> For example
> 
> ```
> puts imap.fetch(message_id, "BODY[2]")
> 
> ```
> 
> Guy Decoux

Thanks! BODY[1] for the plain text version is exactly what I needed. I  
didn’t know you could access the various parts of the body using array  
index notation.

–Ed
