IMAP attachment retrieving command
Asked Answered
M

3

12

I am working on a mail client using IMAP and I am looking for the command for receiving the attachments of a message.

Mystical answered 20/1, 2011 at 9:47 Comment(0)
A
30

All message info is retrieved using the FETCH command. You have two options on how to use it, however.

First, you can retrieve the entire email message, verbatim. In that case, you're going to need to include a MIME parser in your client to figure out the structure of the message. (Each platform has at least one or two popular MIME parsers; since you haven't told us what you're coding in, I can't recommend one for you.) Once you get the message structure from your MIME parser, you'll need some client logic to determine which parts are attachments. It's worth looking at RFC 2183 to get you started. In general, parts with a Content-Disposition starting with "attachment" are going to be attachments, but all mail client authors go through a phase of trial and error getting it right. In order to download the entire email message, you'd issue the IMAP command

$ UID FETCH <uid> BODY.PEEK[]

Second, you can have the IMAP server parse the message structure for you by issuing a FETCH BODYSTRUCTURE (note: no square brackets). You'll have to parse the returned BODYSTRUCTURE data yourself; the IMAP RFC explains the format and gives a few examples.

# message, no attachments:
("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "FORMAT" "flowed") NIL NIL "7BIT" 1469 50 NIL NIL NIL NIL)

# message, one attachment
(("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "QUOTED-PRINTABLE" 56 1 NIL NIL NIL NIL)("AUDIO" "X-WAV" ("NAME" "voicemail.wav") NIL NIL "BASE64" 152364 NIL ("attachment" ("FILENAME" "voicemail.wav")) NIL NIL) "MIXED" ("BOUNDARY" "----_=_NextPart_001_01C4ACB3.5AA7B8E2") NIL NIL NIL)

Once you've determined which parts you're interested in, you can issue a FETCH for the displayable message body. Your client can then just list the message attachments (parsed out of the BODY response) and can then go back and FETCH them if the user clicks on them. So the IMAP commands you'd be issuing would be along the lines of:

$ UID FETCH <uid> (BODY ENVELOPE)   # get structure and header info
$ UID FETCH <uid> (BODY[1])         # retrieving displayable body
$ UID FETCH <uid> (BODY[2])         # retrieving attachment on demand
Almshouse answered 20/1, 2011 at 13:3 Comment(0)
R
1

I believe what you are looking for is the IMAP v4 FETCH command.

Ruckman answered 20/1, 2011 at 9:55 Comment(1)
ok, but things is that I am once all mail has been downloaded then after what ever email is I am selecting, this mail I want to download attachment. now once my mail has been downloaded and then I am try to download attachment; it will say the $ OK UID FETCH completed so what can I do.?Mystical
S
0

You could use Context.IO's files resource to quickly and easily fetch attachments.

http://context.io/docs/2.0/accounts/files#get

Selfsame answered 9/8, 2013 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.