note 51497 added to function.imap-fetchstructure

From: Date: Fri, 01 Apr 2005 11:47:00 +0000
Subject: note 51497 added to function.imap-fetchstructure
Groups: php.notes 
Request: Send a blank email to php-notes+get-87314@lists.php.net to get a copy of this message
I've created a function which simply extracts basic message content as a string ($content) and attachments as an array ($attachments). I've tested it with messages from Outlook, Outlook Express and Hotmail. Perhaps it's useful to anyone. <?php $struct = imap_fetchstructure($mbox, $mid); $parts = $struct->parts; $i = 0; if (!$parts) { /* Simple message, only 1 piece */ $attachment = array(); /* No attachments */ $content = imap_body($mbox, $mid); } else { /* Complicated message, multiple parts */ $endwhile = false; $stack = array(); /* Stack while parsing message */ $content = ""; /* Content of message */ $attachment = array(); /* Attachments */ while (!$endwhile) { if (!$parts[$i]) { if (count($stack) > 0) { $parts = $stack[count($stack)-1]["p"]; $i = $stack[count($stack)-1]["i"] + 1; array_pop($stack); } else { $endwhile = true; } } if (!$endwhile) { /* Create message part first (example '1.2.3') */ $partstring = ""; foreach ($stack as $s) { $partstring .= ($s["i"]+1) . "."; } $partstring .= ($i+1); if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */ $attachment[] = array("filename" => $parts[$i]->parameters[0]->value, "filedata" => imap_fetchbody($mbox, $mid, $partstring)); } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */ $content .= imap_fetchbody($mbox, $mid, $partstring); } } if ($parts[$i]->parts) { $stack[] = array("p" => $parts, "i" => $i); $parts = $parts[$i]->parts; $i = 0; } else { $i++; } } /* while */ } /* complicated message */ echo "Analyzed message $mid, result: <br />"; echo "Content: $content<br /><br />"; echo "Attachments:"; print_r ($attachment); ?> ---- Manual Page -- http://www.php.net/manual/en/function.imap-fetchstructure.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51497 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51497&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51497&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51497&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51497&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51497&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51497&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87314) next »