Search code examples
imapmime

Obtaining IMAP attachments using BODY parts


I am creating an email client with API functionalities. One of the functionalities is to provide an API call to download a given attachment.

To obtain an attachment, given the filename and unique email ID (using the GMail X-MSG-ID unique identifier), I'm downloading the whole email, using the FETCH command with the RFC822 command. This is naturally very heavy.

What I want to do is download only the BODY part that is that attachment, such as BODY[1], BODY[2], etc. I know that obtaining the BODYSTRUCTURE gives me a list of parts in the format ("PART","ETC")("PART","ETC"). What I want to know is how these parts map to the BODY[0], BODY[1], etc.

Is the order that parts appear in the BODYSTRUCTURE response a direct mapping to the BODY indices? So if calling BODYSTRUCTURE I obtain ("123","ETC")("456","ETC")("789","ETC"), can I assume that BODY[0] is the "123" and that BODY[1] is the "456"? Or is there another way to map the elements in parenthesis in a BODYSTRUCTURE response to the BODY[0], BODY[1], etc?

Thank you


Solution

  • I have solved this through trial and error.

    It would appear the BODY indeces are as they come in the BODYSTRUCTURE response. So, using the above example, if in the BODYSTRUCTURE response you obtain ("123","ETC")("456","ETC"), then when you call BODY[1] you will obtain the "123" part, when you call BODY[2] you will obtain "456", and so on.