imap_fetchbody()的正确选项;

I am making an email client, and I have a function which saves all the files attached to an email on server. Files are saving on the server, but the issue is that the size of every file becomes 0 kB, and the encoding for every file is equal to 3.

I want to know how I can select the correct option for imap_fetchbody() on the basis of the sub-type of an attachment.

This is my code:

if($attachments[$i]['is_attachment']){

    $attachments[$i]['attachment'] = imap_fetchbody($imap, $uid, $i + 1;);

    if($structure->parts[$i]->type == 1){
        // 3 = BASE64
        $attachments[$i]['attachment'] = imap_8bit($attachments[$i]['attachment']);
    }

    if($structure->parts[$i]->type == 2){
        // 3 = BASE64
        $attachments[$i]['attachment'] = imap_binary($attachments[$i]['attachment']);
    }

    if($structure->parts[$i]->type == 7){
        // 3 = BASE64
        $attachments[$i]['attachment'] = imap_base64($attachments[$i]['attachment']);
    }

    if($structure->parts[$i]->type == 4){
        // 3 = BASE64
        $attachments[$i]['attachment'] = imap_qprint($attachments[$i]['attachment']);
    }

    if($structure->parts[$i]->type == 3){
        // 3 = BASE64
        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
    }
    elseif($structure->parts[$i]->type == 4){
        // 4 = QUOTED - PRINTABLE
        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
    }
    //return $message;

}

You should be looking at the transfer encoding, not the type, since it's possible to encode a certain file type (e.g. an image, type 5 or TYPEIMAGE) with different encodings (e.g. base64 or quoted-printable).

Do something like this:

if($structure->parts[$i]->encoding == ENC8BIT){
    $attachments[$i]['attachment'] = imap_8bit($attachments[$i]['attachment']);
}

elseif($structure->parts[$i]->encoding == ENCBINARY){
    $attachments[$i]['attachment'] = imap_binary($attachments[$i]['attachment']);
}

elseif($structure->parts[$i]->encoding == ENCBASE64){
    $attachments[$i]['attachment'] = imap_base64($attachments[$i]['attachment']);
}

elseif($structure->parts[$i]->encoding == ENCQUOTEDPRINTABLE){
    $attachments[$i]['attachment'] = imap_qprint($attachments[$i]['attachment']);
}