Jump to content

trouble downloading email attachment


fatjoez

Recommended Posts

hi guys

 

i'm busy trying to get a class working to download my email attachments via IMAP.

 

It's working for the most part BUT with some emails, the attachments don't download correctly, only the file is created but the result file is 0kb.

 

I believe the script is not correctly detecting the file type of some of these emails and as such am seeking your advice as to how I can get it detecting them correctly.

 

I've attached info of filetypes of 2 emails which attachments end up as 0kb files and the code of my script below

 

(or view the pastebin here: http://pastebin.zentrack.net/92349)

 

class readattachment
{
    function getdecodevalue($message,$coding)
    {
        switch ($coding) {
        case 1:
            $message = imap_8bit($message);
            break;
        case 2:
            $message = imap_binary($message);
            break;
        case 4:
            $message = imap_qprint($message);
            break;
        case 0:
        case 3:
        case 5:
            $message = imap_base64($message);
            break;
        }
        return $message;
    }
    
    function getdata($host, $login, $password, $savedirpath)
    {
                
        $mbox = imap_open($host, $login, $password) or die("Unable to connect to the server using the details provided.<br />The server error message was: <em> " . imap_last_error() . "</em>");
        
        $message = array();
        $message["attachment"]["type"][0] = "text";
        $message["attachment"]["type"][1] = "multipart";
        $message["attachment"]["type"][2] = "message";
        $message["attachment"]["type"][3] = "application";
        $message["attachment"]["type"][4] = "audio";
        $message["attachment"]["type"][5] = "image";
        $message["attachment"]["type"][6] = "video";
        $message["attachment"]["type"][7] = "other";
        
        $total_msg = imap_num_msg($mbox);
        
        for ($i = $total_msg; $i >= $total_msg-3; $i--) {
            $structure = imap_fetchstructure($mbox, $i , FT_UID);
            $header = imap_fetch_overview($mbox, $i , FT_UID);
            $parts = $structure->parts;
            for ($n = 1; $n < count($parts); $n++) {
                $message["pid"][$n] = ($n);
                $part = $parts[$n];
                
                if ($part->disposition == "ATTACHMENT") {
                    
                    $message["type"][$n] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                    $message["subtype"][$n] = strtolower($part->subtype);
                    $ext=$part->subtype;
                    echo "PART TYPE: $ext";
                    $params = $part->dparameters;
                    $filename=$part->dparameters[0]->value;
                    
                    $mege="";
                    $data="";
                    $mege = imap_fetchbody($mbox,$i,$n+1);
                    $filename="$filename";
                    echo $filename;
                    $fp=fopen($savedirpath.$filename,'w');
                    $data=$this->getdecodevalue($mege,$part->type);
                    fputs($fp,$data);
                    fclose($fp);
                }
                
            }
        }
        imap_close($mbox);
    }
}

 

and here is the details & fetchstructure output (via print_r) of an email which does not download correctly (0kb file result)

 

Problem Email Number #1

 

C10699A-16NOV2007.CSV [application/octet-stream]

C10699A-16NOV2007.CSV  View as text/x-comma-separated-values

 

The only real difference I can see is the Encoding transfer method for the attachment part in this email is 4 ( Quoted-Printable) and another email which I checked out (which downloads correctly) has an encoding transfer of 3 ( base 64)

 

but that said, as you can see above my script handles both transfer methods so I don't understand what's going wrong!!!

 

Downloaded file is 0kb. I'm assuming the Class isn't correctly identifying the file type of the attachment and so not downloading properly!!!

 

stdClass Object

(

    [type] => 1

    [encoding] => 0

    [ifsubtype] => 1

    [subtype] => MIXED

    [ifdescription] => 0

    [ifid] => 0

    [bytes] => 805141

    [ifdisposition] => 0

    [ifdparameters] => 0

    [ifparameters] => 1

    [parameters] => Array

        (

            [0] => stdClass Object

                (

                    [attribute] => BOUNDARY

                    [value] => ----=_NextPart_000_0013_01C82834.2D74AA10

                )

 

        )

 

    [parts] => Array

        (

            [0] => stdClass Object

                (

                    [type] => 1

                    [encoding] => 0

                    [ifsubtype] => 1

                    [subtype] => ALTERNATIVE

                    [ifdescription] => 0

                    [ifid] => 0

                    [bytes] => 6707

                    [ifdisposition] => 0

                    [ifdparameters] => 0

                    [ifparameters] => 1

                    [parameters] => Array

                        (

                            [0] => stdClass Object

                                (

                                    [attribute] => BOUNDARY

                                    [value] => ----=_NextPart_001_0014_01C82834.2D771B10

                                )

 

                        )

 

                    [parts] => Array

                        (

                            [0] => stdClass Object

                                (

                                    [type] => 0

                                    [encoding] => 0

                                    [ifsubtype] => 1

                                    [subtype] => PLAIN

                                    [ifdescription] => 0

                                    [ifid] => 0

                                    [lines] => 71

                                    [bytes] => 2237

                                    [ifdisposition] => 0

                                    [ifdparameters] => 0

                                    [ifparameters] => 1

                                    [parameters] => Array

                                        (

                                            [0] => stdClass Object

                                                (

                                                    [attribute] => CHARSET

                                                    [value] => windows-1250

                                                )

 

                                        )

 

                                )

 

                            [1] => stdClass Object

                                (

                                    [type] => 0

                                    [encoding] => 4

                                    [ifsubtype] => 1

                                    [subtype] => HTML

                                    [ifdescription] => 0

                                    [ifid] => 0

                                    [lines] => 105

                                    [bytes] => 4142

                                    [ifdisposition] => 0

                                    [ifdparameters] => 0

                                    [ifparameters] => 1

                                    [parameters] => Array

                                        (

                                            [0] => stdClass Object

                                                (

                                                    [attribute] => CHARSET

                                                    [value] => windows-1250

                                                )

 

                                        )

 

                                )

 

                        )

 

                )

 

            [1] => stdClass Object

                (

                    [type] => 3

                    [encoding] => 4

                    [ifsubtype] => 1

                    [subtype] => OCTET-STREAM

                    [ifdescription] => 0

                    [ifid] => 0

                    [bytes] => 797960

                    [ifdisposition] => 1

                    [disposition] => ATTACHMENT

                    [ifdparameters] => 1

                    [dparameters] => Array

                        (

                            [0] => stdClass Object

                                (

                                    [attribute] => FILENAME

                                    [value] => C10699A-16NOV2007.CSV

                                )

 

                        )

 

                    [ifparameters] => 1

                    [parameters] => Array

                        (

                            [0] => stdClass Object

                                (

                                    [attribute] => NAME

                                    [value] => C10699A-16NOV2007.CSV

                                )

 

                        )

 

                )

 

        )

 

)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.