Solved

Not understanding foreach()

Posted on 2009-07-06
2
242 Views
Last Modified: 2012-05-07
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
Comment
Question by:hrolsons
  • 2
2 Comments
 

Author Comment

by:hrolsons
ID: 24785782
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
 

Accepted Solution

by:
hrolsons earned 0 total points
ID: 24785846
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now