note 112486 added to function.imap-fetchstructure
| From: | Anonymous | Date: | Thu, 20 Jun 2013 22:53:01 +0000 |
| Subject: | note 112486 added to function.imap-fetchstructure | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195311@lists.php.net to get a copy of this message | ||
I improved the code from this post (
http://www.php.net/manual/en/function.imap-fetchstructure.php#51497)
a bit, now it handles multi embedded email correctly, and gives you a content with not marking
character, like the '=' (soft line break).
<?php
//return is structured as
// $mail = array ( 'body' => ...
// 'attachment' => array() );
function _get_body_attach($mbox, $mid) {
$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" ||
strtoupper($parts[$i]->disposition) == "INLINE") { /* Attachment or inline images */
$filedata = imap_fetchbody($mbox, $mid, $partstring);
if ( $filedata != '' ) {
// handles base64 encoding or plain text
$decoded_data = base64_decode($filedata);
if ( $decoded_data == false ) {
$attachment[] = array("filename" =>
$parts[$i]->parameters[0]->value,
"filedata" => $filedata);
} else {
$attachment[] = array("filename" =>
$parts[$i]->parameters[0]->value,
"filedata" => $decoded_data);
}
}
} elseif (strtoupper($parts[$i]->subtype) == "PLAIN" &&
strtoupper($parts[$i+1]->subtype) != "HTML") { /* plain text message */
$content .= imap_fetchbody($mbox, $mid, $partstring);
} elseif ( strtoupper($parts[$i]->subtype) == "HTML" ) {
/* HTML message takes priority */
$content .= imap_fetchbody($mbox, $mid, $partstring);
}
}
if ($parts[$i]->parts) {
if ( $parts[$i]->subtype != 'RELATED' ) {
// a glitch: embedded email message have one additional stack in the structure
with subtype 'RELATED', but this stack is not present when using imap_fetchbody() to fetch
parts.
$stack[] = array("p" => $parts, "i" => $i);
}
$parts = $parts[$i]->parts;
$i = 0;
} else {
$i++;
}
} /* while */
} /* complicated message */
$ret = array();
$ret['body'] = quoted_printable_decode($content);
$ret['attachment'] = $attachment;
return $ret;
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 216.228.112.22
----
Manual Page -- http://php.net/manual/en/function.imap-fetchstructure.php
Edit -- https://master.php.net/note/edit/112486
Del: integrated -- https://master.php.net/note/delete/112486/integrated
Del: useless -- https://master.php.net/note/delete/112486/useless
Del: bad code -- https://master.php.net/note/delete/112486/bad+code
Del: spam -- https://master.php.net/note/delete/112486/spam
Del: non-english -- https://master.php.net/note/delete/112486/non-english
Del: in docs -- https://master.php.net/note/delete/112486/in+docs
Del: other reasons-- https://master.php.net/note/delete/112486
Reject -- https://master.php.net/note/reject/112486
Search -- https://master.php.net/manage/user-notes.php