note 109519 added to book.imap
| From: | TyronMadlener | Date: | Wed, 25 Jul 2012 12:36:33 +0000 |
| Subject: | note 109519 added to book.imap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189392@lists.php.net to get a copy of this message | ||
The Functions supplied by Wil Barath are great but have several bugs.
- Several headers, such as from,to,cc,subject are not mime decoded via iconv_mime_decode()
- The call to quoted_printable_decode() inside mail_decode_part() sometimes creates problems when
header lines are mime encoded
- non-multipart e-mails are returend without message body
Below are the methods I had to change to fix these bugs:
function mail_get_parts($imap,$mid,$part,$prefix) {
$attachments=array();
$attachments[$prefix] = mail_decode_part($imap,$mid,$part,$prefix);
if (isset($part->parts)) { // multipart
$prefix = ($prefix == "0")?"":"$prefix.";
foreach ($part->parts as $number=>$subpart) {
$attachments = array_merge($attachments,
mail_get_parts($imap,$mid,$subpart,$prefix.($number+1)));
}
} else {
$attachments[1] = mail_decode_part($imap,$mid,$part,1);
}
return $attachments;
}
function mail_decode_part($connection,$message_number,$part,$prefix) {
$attachment = array();
if($part->ifdparameters) {
foreach($part->dparameters as $object) {
$attachment[strtolower($object->attribute)]=$object->value;
if(strtolower($object->attribute) == 'filename' || strtolower($object->attribute)
== 'filename*') {
$attachment['is_attachment'] = true;
$attachment['filename'] = $object->value;
}
}
}
if($part->ifparameters) {
foreach($part->parameters as $object) {
$attachment[strtolower($object->attribute)]=$object->value;
if(strtolower($object->attribute) == 'name') {
$attachment['is_attachment'] = true;
$attachment['name'] = $object->value;
}
}
}
$attachment['data'] = imap_fetchbody($connection, $message_number, $prefix);
if($part->encoding == 3) { // 3 = BASE64
$attachment['data'] = base64_decode($attachment['data']);
}
elseif($part->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachment['data'] = explode("\r\n", $attachment['data']);
foreach ($attachment['data'] as &$line) {
$line = quoted_printable_decode($line);
}
$attachment['data'] = implode("\r\n", $attachment['data']);
}
return($attachment);
}
function iconv_mime_decode_failsafe($str) {
$decoded = iconv_mime_decode($str, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
if (! $decoded) {
$decoded = iconv_mime_decode($str . '=', ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
}
if (! $decoded) {
$decoded = $str;
}
return $decoded;
}
----
Server IP: 69.147.83.197
Probable Submitter: 81.198.27.15
----
Manual Page -- http://www.php.net/manual/en/book.imap.php
Edit -- https://master.php.net/note/edit/109519
Del: integrated -- https://master.php.net/note/delete/109519/integrated
Del: useless -- https://master.php.net/note/delete/109519/useless
Del: bad code -- https://master.php.net/note/delete/109519/bad+code
Del: spam -- https://master.php.net/note/delete/109519/spam
Del: non-english -- https://master.php.net/note/delete/109519/non-english
Del: in docs -- https://master.php.net/note/delete/109519/in+docs
Del: other reasons-- https://master.php.net/note/delete/109519
Reject -- https://master.php.net/note/reject/109519
Search -- https://master.php.net/manage/user-notes.php