1

i'm trying for my program to download all file attachments, but for some reason it doesn't download all of them only downloads a certain file attachment. This is the code:

<?php

set_time_limit(3000);
$hostname = '{someoutlookemail.outlook.com:993/imap/ssl}INBOX';
$username = 'someoutlookemail@outlook.com';
$password = 'apass';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');


$max_emails = 16;

/* if emails are returned, cycle through each... */
if($emails) {

    $count = 1;

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,1.1);
        $structure = imap_fetchstructure($inbox,$email_number);

         $attachments = array();

        /* if any attachments found... */
        if(isset($structure->parts) && count($structure->parts)) 
        {
            for($i = 0; $i < count($structure->parts); $i++) 
            {
                $attachments[$i] = array(
                    'is_attachment' => false,
                    'filename' => '',
                    'name' => '',
                    'attachment' => ''
                );

                if($structure->parts[$i]->ifdparameters) 
                {
                    foreach($structure->parts[$i]->dparameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'filename') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object->value;
                        }
                    }
                }

                if($structure->parts[$i]->ifparameters) 
                {
                    foreach($structure->parts[$i]->parameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'name') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['name'] = $object->value;
                        }
                    }
                }

                if($attachments[$i]['is_attachment']) 
                {
                    $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);

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

        /* iterate through each attachment and save it */
        foreach($attachments as $attachment)
        {
            if($attachment['is_attachment'] == 1)
            {
                $filename = $attachment['name'];
                if(empty($filename)) $filename = $attachment['filename'];

                if(empty($filename)) $filename = time() . ".dat";

                /* prefix the email number to the filename in case two emails
                 * have the attachment with the same file name.
                 */
                $fp = fopen("./" . $email_number . "-" . $filename, "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);
            }

        }

        if($count++ >= $max_emails) break;


        /* output the email header information */
        $output.= '<p><div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<p>Subject: <span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<p>From: <span class="from">'.$overview[0]->from.'</span>';
        $output.= '<p>Date: <span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';

        /* output the email body */
        $output.= '<p>Message:<div class="body">'.$message.'';
        $output.= '<p>attachment:'.$filename.'';
        $output.= '<table><tr><hr size="1" width="100%%" noshade color="black" ></tr></table>';


    }

    echo $output;
}

/* close the connection */
imap_close($inbox);
?>

whats wrong ? i tried to check all of my code and i dont understand why it only downloads some, i'm downloading the same type of file for now (.xml), one of them downloads and the other doesn't, the one who downloads has 1mb size and the other one 600kb, please help, i'have searched everywhere, and i can't find a solution.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Have you tried if(empty($filename)) $filename = $i.time() . ".dat"; where $i is an incremental number, sometime it takes less than a second to save the files, so you are overwritting them – Ivan G. Jun 28 '17 at 12:10
  • i changed it like you said, but it still doesn't download all files, it downloads a certain type of file always, and if i have two of that same file, it downloads it, but different ones besides that one, it doesn't download... –  Jun 28 '17 at 12:19
  • Possible duplicate of [Downloading attachments to directory with IMAP in PHP, randomly works](https://stackoverflow.com/questions/2649579/downloading-attachments-to-directory-with-imap-in-php-randomly-works) – LuFFy Jun 28 '17 at 12:38

1 Answers1

0

Your script it working fine for me, all the files are being download. The only problem is that is not showing correctly, you should iterate on the attachments, when you echo the output. Something like this

<?php

set_time_limit(3000);


/* try to connect */


/* if emails are returned, cycle through each... */
if($emails) {

    $count = 1;

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,1.1);
        $structure = imap_fetchstructure($inbox,$email_number);

        $attachments = array();

        /* if any attachments found... */
        if(isset($structure->parts) && count($structure->parts)) 
        {
            for($i = 0; $i < count($structure->parts); $i++) 
            {
                $attachments[$i] = array(
                    'is_attachment' => false,
                    'filename' => '',
                    'name' => '',
                    'attachment' => ''
                );

                if($structure->parts[$i]->ifdparameters) 
                {
                    foreach($structure->parts[$i]->dparameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'filename') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object->value;
                        }
                    }
                }

                if($structure->parts[$i]->ifparameters) 
                {
                    foreach($structure->parts[$i]->parameters as $object) 
                    {
                        if(strtolower($object->attribute) == 'name') 
                        {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['name'] = $object->value;
                        }
                    }
                }

                if($attachments[$i]['is_attachment']) 
                {
                    $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);

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

        /* iterate through each attachment and save it */
        foreach($attachments as $key=>$attachment)
        {
            if($attachment['is_attachment'] == 1)
            {
                $filename = $attachment['name'];
                if(empty($filename)) $filename = $attachment['filename'];

                if(empty($filename)) $filename = time() . ".dat";

                /* prefix the email number to the filename in case two emails
                * have the attachment with the same file name.
                */
                $fp = fopen("./" . $email_number . "-" . $filename, "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);

                $attachments[$key]['filename'] = $filename;
            }

        }

        if($count++ >= $max_emails) break;


        /* output the email header information */
        $output.= '<p><div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<p>Subject: <span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<p>From: <span class="from">'.$overview[0]->from.'</span>';
        $output.= '<p>Date: <span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';

        /* output the email body */
        $output.= '<p>Message:<div class="body">'.$message.'';
        foreach ($attachments as $attachment) {
        $output.= '<p>attachment:'.$attachment['filename'].'';
        }
        $output.= '<table><tr><hr size="1" width="100%%" noshade color="black" ></tr></table>';


    }

    echo $output;
}

/* close the connection */
imap_close($inbox);
?>
Ivan G.
  • 146
  • 2
  • i changed the output on the attachments, and it still keeps giving me the same error, it says: fopen(./1-=?iso-8859-1?Q?513178643=5F2017-05-01=5F2017-05-31=5FGlobal=5FSaft=5FFatu?= =?iso-8859-1?Q?ra=E7=E3o.XML?=): failed to open stream –  Jun 29 '17 at 09:51
  • Also even if i download one at a time, it still only downloads the same file that has always been working, even if i delete all attachments and download one by one, it still only downloads that specific one... i dont understand why –  Jun 29 '17 at 10:27