I'm working on a mailbox parser and have the basics down for my rough cut...

Right now for 'forwarding' im simply doing the following:

//CONTROL STRUCTURE THAT LOOPS THROUGH ALL MSGS IN THE BOX

//BUNCH OF LOGIC TO DETERMINE HOW TO HANDLE THE EMAIL

imap_mail('import@domain.com', $subject.' ['.$mail_from.']', $body, 'From: '.$mail_from);
imap_mail_move($mbox,$i,'INBOX._PROCESSED');

The only downside here is that emails with attachments lose their attachments.

Is there an 'elegant' way to forward an entire message without picking it apart and piecing it back together?

    scrupul0us wrote:

    The only downside here is that emails with attachments lose their attachments.

    While I haven't delved much into using IMAP in PHP, can't you simply check if the message to be forwarded has attachments and, if so, iterate through them to download and re-attach them to the new outgoing message?

      i suppose you could... its all rather new to me as well... a search of this on the forums lead to a few threads, none of which ever got answered 🙁

        brad... supposing I went with your solution... the best I've come up with thus far is accessing the attachments name:

        <?php
        //GET MESSAGE STRUCTURE
        $info = imap_fetchstructure($mbox, $i);
        
        //GET NUM PARTS
        $numparts = count($info->parts);
        
        //MULTIPART?
        if ($numparts > 1) 
        {   
        foreach ($info->parts as $part) { if ($part->disposition == "ATTACHMENT") { //THIS IS THE ATTACHMENT NAME //$part->dparameters[0]->value; } } } ?>

        now, I've been muddling over the user comments [man]imap_fetch_structure[/man] and maybe I'm just blind, but, I really don't see anthing that jumps out to say

        "here is the attachment data"

        the next step would then be figuring out how to take it and push it onto another message...

        :: sighs ::

          actually, I dont even need to detect all attachments... if i could simply read .eml attachments and append their content to the message im already sending, that would be more than sufficient

            See if this page helps.

            It looks like it's a somewhat lengthy/involved process; imap_fetch_structure() will allow you to see the "parts" of a multi-part message, and from there you can determine if the "part" you're examining is an attachment (based on MIME-Type or Content-Disposition or something of that nature) and, if so, retrieve it's disposition/name/etc. Then you can retrieve the actual content of that "part" of the e-mail using imap_fetch_body().

            At least, that's what I gathered by skimming the above page.

              <?php
              ini_set('display_errors','1');
              imap_timeout(IMAP_OPENTIMEOUT,15);
              $mbox = imap_open("{imap.domain.tld:143/novalidate-cert}INBOX.ATTACHMENTTEST", "user@domain.tld", "password") or die();
              
              $message_count = imap_num_msg($mbox);
              
              if($message_count == 0)
              {
              	exit();
              }
              
              for ($i = 1; $i <= $message_count; ++$i) 
              {
              	//PULL HEADER OBJECT AND ASSIGN SUBJECT TO A VARIABLE
              	$header = imap_header($mbox, $i);
              	$subject = $header->subject;
              
              //FETCH BODY OBJECT AND ASSIGN PLAIN/TEXT TO A VARIABLE
              if($body = imap_fetchbody($mbox, $i, 1.1))
              {
              	//ASSOCIATED MIME CONTENT/ENCODING
              }
              else
              {
              	//NO ASSOCIATED MIME CONTENT/ENCODING
              	$body = imap_fetchbody($mbox, $i, 1);
              }
              
              $structure = imap_fetchstructure($mbox, $i);
              $attachments = array();
              if(isset($structure->parts) && count($structure->parts)) {
              
              	for($j = 0; $j < count($structure->parts); $j++) {
              
              		$attachments[$j] = array(
              			'is_attachment' => false,
              			'filename' => '',
              			'name' => '',
              			'attachment' => ''
              		);
              
              		if($structure->parts[$j]->ifdparameters) {
              			foreach($structure->parts[$j]->dparameters as $object) {
              				if(strtolower($object->attribute) == 'filename') {
              					$attachments[$j]['is_attachment'] = true;
              					$attachments[$j]['filename'] = $object->value;
              				}
              			}
              		}
              
              		if($structure->parts[$j]->ifparameters) {
              			foreach($structure->parts[$j]->parameters as $object) {
              				if(strtolower($object->attribute) == 'name') {
              					$attachments[$j]['is_attachment'] = true;
              					$attachments[$j]['name'] = $object->value;
              				}
              			}
              		}
              
              		if($attachments[$j]['is_attachment']) {
              			$attachments[$j]['attachment'] = imap_fetchbody($mbox, $i, $i++);
              			if($structure->parts[$j]->encoding == 3) { // 3 = BASE64
              				$attachments[$j]['attachment'] = base64_decode($attachments[$j]['attachment']);
              			}
              			elseif($structure->parts[$j]->encoding == 4) { // 4 = QUOTED-PRINTABLE
              				$attachments[$j]['attachment'] = quoted_printable_decode($attachments[$j]['attachment']);
              			}
              		}
              	}
              }
              echo '<pre>';
              print_r($attachments);
              }	
              ?>
              

                So lets boil it down either further...

                <?php
                ini_set('display_errors','1');
                imap_timeout(IMAP_OPENTIMEOUT,15);
                $mbox = imap_open("{imap.domain.tld:143/novalidate-cert}INBOX.ATTACHMENTTEST", "user@domain.tld", "password") or die();
                $message_count = imap_num_msg($mbox);
                if($message_count == 0)
                {
                	exit();
                }
                
                for ($msgno = 1; $msgno <= $message_count; $msgno++) 
                {
                	echo '<pre>';
                	print_r(imap_fetchstructure($mbox, $msgno));	
                }	
                ?>
                

                That returns:

                stdClass Object
                (
                    [type] => 1
                    [encoding] => 0
                    [ifsubtype] => 1
                    [subtype] => REPORT
                    [ifdescription] => 0
                    [ifid] => 0
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => report-type
                                    [value] => feedback-report
                                )
                
                        [1] => stdClass Object
                            (
                                [attribute] => boundary
                                [value] => boundary-1138-29572-2659438-6122
                            )
                
                    )
                
                [parts] => Array
                    (
                        [0] => stdClass Object
                            (
                                [type] => 0
                                [encoding] => 0
                                [ifsubtype] => 1
                                [subtype] => PLAIN
                                [ifdescription] => 0
                                [ifid] => 0
                                [lines] => 11
                                [bytes] => 569
                                [ifdisposition] => 0
                                [ifdparameters] => 0
                                [ifparameters] => 1
                                [parameters] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [attribute] => charset
                                                [value] => us-ascii
                                            )
                
                                    )
                
                            )
                
                        [1] => stdClass Object
                            (
                                [type] => 2
                                [encoding] => 1
                                [ifsubtype] => 1
                                [subtype] => FEEDBACK-REPORT
                                [ifdescription] => 0
                                [ifid] => 0
                                [bytes] => 235
                                [ifdisposition] => 1
                                [disposition] => inline
                                [ifdparameters] => 0
                                [ifparameters] => 0
                                [parameters] => stdClass Object
                                    (
                                    )
                
                            )
                
                        [2] => stdClass Object
                            (
                                [type] => 2
                                [encoding] => 1
                                [ifsubtype] => 1
                                [subtype] => RFC822
                                [ifdescription] => 0
                                [ifid] => 0
                                [lines] => 55
                                [bytes] => 2302
                                [ifdisposition] => 1
                                [disposition] => inline
                                [ifdparameters] => 0
                                [ifparameters] => 0
                                [parameters] => stdClass Object
                                    (
                                    )
                
                                [parts] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [type] => 1
                                                [encoding] => 0
                                                [ifsubtype] => 1
                                                [subtype] => ALTERNATIVE
                                                [ifdescription] => 0
                                                [ifid] => 0
                                                [ifdisposition] => 0
                                                [ifdparameters] => 0
                                                [ifparameters] => 1
                                                [parameters] => Array
                                                    (
                                                        [0] => stdClass Object
                                                            (
                                                                [attribute] => boundary
                                                                [value] => ----------=_1279767188-7874-7337
                                                            )
                
                                                    )
                
                                                [parts] => Array
                                                    (
                                                        [0] => stdClass Object
                                                            (
                                                                [type] => 0
                                                                [encoding] => 0
                                                                [ifsubtype] => 1
                                                                [subtype] => HTML
                                                                [ifdescription] => 0
                                                                [ifid] => 0
                                                                [lines] => 23
                                                                [bytes] => 1020
                                                                [ifdisposition] => 1
                                                                [disposition] => inline
                                                                [ifdparameters] => 0
                                                                [ifparameters] => 1
                                                                [parameters] => Array
                                                                    (
                                                                        [0] => stdClass Object
                                                                            (
                                                                                [attribute] => charset
                                                                                [value] => utf-8
                                                                            )
                
                                                                    )
                
                                                            )
                
                                                    )
                
                                            )
                
                                    )
                
                            )
                
                    )
                
                )
                
                

                now the email it's ran against is an AOL scomp report... so theres a ATTXXXXX.dat and the original email reported attached... yet, neither of these are showing their faces... in all the examples ive seen, at the very least, the attachment name should be visible in the stdClass output...

                What the heck

                  The 'parts' sub array looks promising, but the overall structure appears to be nested.

                    and to come full circle this does what I need (to a certain extent) based on these functions:

                    http://us.php.net/manual/en/book.imap.php#96414

                    	$body = '';
                    	$mail = mail_mime_to_array($mbox,$i,FALSE);
                    	foreach($mail as $msg)
                    	{
                    		$body .= nl2br($msg['data']);
                    	}
                    

                    The resulting message it rather long, but, it gets the data back out

                      Write a Reply...