I am working on a mail client using IMAP and I am looking for the command for receiving the attachments of a message.
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
I believe what you are looking for is the IMAP v4 FETCH command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With