tags:

views:

125

answers:

2

I'm working on a zend framework based email project and I'm following some code samples online.. I can't understand this line of code which apparently loops through different 'parts' of an email message. I have no idea how it works btw and suspect that theres some error taking place which my parser isn't showing right.

foreach (new RecursiveIteratorIterator($mail->getMessage($i)) as $ii=>$part)

what does the above foreach loop mean?

+1  A: 

It creates a new recursive iterator based on the contents of the message with id $i and loops through the parts of the e-mail. E-mails generally consist of multi-part messages, so the getmessage method probably has a call to retrieve the first part of the message after retrieving headers. The method to get the part likely calls itself (recursively) with an incrementing id to return the part, hence the $ii=>$part.

It is difficult to expand without knowing the full contents of the method call.

Example for Directory recursion

$dir = new RecursiveDirectoryIterator(".");
foreach(new RecursiveIteratorIterator($dir) as $file) {
  // find .txt files
  if (preg_match('/.txt$/i', $file->getFilename())) {
  }
}

EDIT 2: Using the example on the linked site:

$foundPart = null;
foreach (new RecursiveIteratorIterator($mail->getMessage(1)) as $part) {
        if (strtok($part->contentType, ';') == 'text/plain') {
            $foundPart = $part;
            break;
        }
}

This retrieves all the parts and looks for a plaintext part. It is a way of looping through something recursively allowing manipulation on it.

Gazler
Actually I don't know what a recursive iterator is to begin with thats what I need to know and if I were to duplicate this for statement... how could it be done... just to understand the concept
Ali
getMessage actually returns the message number $i - its an object of Zend_Mail_Message class... what I don't understand is what does recursive iterator do here?
Ali
It loops (iterates) over the parts calling itself (recursively) The context in which I have used one before is for directory looping. Will add an example to the answer.
Gazler
The code I'm using is here http://zendframework.com/manual/1.5/en/zend.mail.read.html - just run a ctrl F search for recursive
Ali
Does it help now?
Gazler
Kinda.. but what I don't understand is how different is using recursive iterator in the above example than using the object itself as the foreach loop does allow to loop through the objects properties.
Ali
I believe you would need to know how many parts there are before you could retrieve them all as the getPart method requires an integer input.
Gazler
+1  A: 

In simple terms the statement will iterate through all of the elements that the mail->getMessage contains flattening the tree structure to make it more like a list.

Recursive iterators are used to traverse through a tree - (or more accurately iterators that can contain iterators). The RecursiveIteratorIterator class documentation doesn't explain it particularly well - but read through the examples.

Richard Harrison