note 109519 deleted from book.imap by danbrown
| From: | danbrown@php.net | Date: | Mon, 30 Jul 2012 15:54:15 +0000 |
| Subject: | note 109519 deleted from book.imap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189498@lists.php.net to get a copy of this message | ||
Note Submitter: Tyron Madlener
----
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;
}