Hi all,
I have a piece of code, where it fetching the attachment details in a array for a specific message.
As i need to download the attachment, i need some guidance from the gurus here. I can show the UNSEEN message and then flag them as SEEN, but the only issue is to download the attachment automatically into a desired directory, as this script will run by cron every hour.
Code:

PHP Code:
<?php
$hostname 
'{mail.server.com}INBOX';
$username 'test';
$password 'test';

/* try to connect */
$mbox imap_open($hostname,$username,$password) or die('Cannot connect to Mail: ' imap_last_error());

$mid=1;
$structure imap_fetchstructure($mbox$mid);
//print_r ($structure);

$attachments = array();
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($mbox$mid$i+1);
            if(
$structure->parts[$i]->encoding == 3) { // 3 = BASE64
                
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
            }
            elseif(
$structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
            }
        }
    }
}
print_r($attachments);
?>
The output is below:
Array ( [0] => Array ( [is_attachment] => [filename] => [name] => [attachment] => ) [1] => Array ( [is_attachment] => 1 [filename] => [name] => stopSms.txt [attachment] => BATAL BH 23131 ) )
Any ideas how to download and improve this code?

Thanks in advance.