Forum: Ruby Understanding net/imap and multi-part e-mails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A38172c519609c0025458910e599826e?d=identicon&s=25 Ed L. (edlau)
on 2006-06-13 10:13
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
ts (Guest)
on 2006-06-13 10:23
(Received via mailing list)
>>>>> "E" == Ed Lau <ted_spam@mac.com> 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 <rfcbqtd3zqv.fsf@moulon.inra.fr>

 For example

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



Guy Decoux
A38172c519609c0025458910e599826e?d=identicon&s=25 Ed L. (edlau)
on 2006-06-13 19:40
ts wrote:
>>>>>> "E" == Ed Lau <ted_spam@mac.com> 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 <rfcbqtd3zqv.fsf@moulon.inra.fr>
>
>  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
This topic is locked and can not be replied to.