Understanding net/imap and multi-part e-mails

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

“E” == Ed Lau [email protected] 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 <[email protected]>

For example

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

Guy Decoux

ts wrote:

“E” == Ed Lau [email protected] 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 <[email protected]>

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