Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Not understanding foreach()

I know the root of my problem is not understanding foreach(), but I don't see why this wouldn't work:
$mbox = imap_open($ServerName, $UserName,$PassWord) or die("Could not open Mailbox - try again later!"); 
	$body_att=extract_attachments($mbox, 10);
	foreach($body_att as $single_att)
	{
		//var_dump($single_att);
		$x= $single_att->filename;
		echo "Filename: $x<P>";
	}

Open in new window

0
hrolsons
Asked:
hrolsons
  • 2
1 Solution
 
hrolsonsAuthor Commented:
Sorry, you might need this:
function extract_attachments($connection, $message_number) {
   
    $attachments = array();
    $structure = imap_fetchstructure($connection, $message_number);
   
    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($connection, $message_number, $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']);
                }
            }
			//echo "$attachments[$i]['name']<br>";
           
        }
       
    }
   
    return $attachments;
   
}

Open in new window

0
 
hrolsonsAuthor Commented:
I figured it out:
	foreach($body_att as $single_att)
	{
		//var_dump($single_att);
		$x= $single_att['filename'];
		echo "Filename: $x<P>";
	}

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now