обычно электронные письма в порядке, а вложения сохраняются правильно. Но некоторые электронные письма имеют этот формат, и эти вложения не сохраняются. Я сделал исправление в коде «// Питер добавил ------------ ПРОБЛЕМА ЗДЕСЬ», но теперь в этих затронутых сообщениях сохраняется целое сообщение, а не просто вложение.
Есть идеи, пожалуйста?
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] => Apple-Mail-A17091CC-3A05-4A36-9F8A-581EB4AB0A36
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 0
[encoding] => 4
[ifsubtype] => 1
[subtype] => PLAIN
[ifdescription] => 0
[ifid] => 0
[lines] => 33
[bytes] => 889
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => charset
[value] => utf-8
)
)
)
[1] => stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => MIXED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => boundary
[value] => Apple-Mail-6E5E5872-40F8-49F0-B3AF-18975041DC58
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 0
[encoding] => 4
[ifsubtype] => 1
[subtype] => HTML
[ifdescription] => 0
[ifid] => 0
[lines] => 117
[bytes] => 7333
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => charset
[value] => utf-8
)
)
)
[1] => stdClass Object
(
[type] => 3
[encoding] => 3
[ifsubtype] => 1
[subtype] => PDF
[ifdescription] => 0
[ifid] => 0
[bytes] => 257790
[ifdisposition] => 1
[disposition] => inline
[ifdparameters] => 1
[dparameters] => Array
(
[0] => stdClass Object
(
[attribute] => filename
[value] => Exp2EMail.pdf
)
)
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => name
[value] => Exp2EMail.pdf
)
[1] => stdClass Object
(
[attribute] => x-apple-part-url
[value] => 31D06834-B725-4866-973D-6397CB9CEB37
)
)
)
[2] => stdClass Object
(
[type] => 0
[encoding] => 0
[ifsubtype] => 1
[subtype] => HTML
[ifdescription] => 0
[ifid] => 0
[bytes] => 178
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => charset
[value] => us-ascii
)
)
)
)
)
)
)
мой код:
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to email: ' . imap_last_error());
/* get all new emails. If set to 'ALL' instead
* of 'NEW' retrieves all the emails, but can be
* resource intensive, so the following variable,
* $max_emails, puts the limit on the number of emails downloaded.
*
*/
$emails = imap_search($inbox,'ALL');
/* useful only if the above search is set to 'ALL' */
$max_emails = 16000000;
/* if any emails found, iterate through each email */
if($emails) {
$count = 1;
/* put the newest emails on top */
rsort($emails);
//print_r($emails); die;
/* for every email... */
foreach($emails as $email_number)
{
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
//print_r($overview); //die;
//echo $overview[0]["udate"];
//echo $overview[0]->udate;
//die;
/* get mail message */
$message = imap_fetchbody($inbox,$email_number,2);
/* get mail structure */
$structure = imap_fetchstructure($inbox, $email_number);
//print_r($message);
//print_r($structure);
$attachments = array();
/* if any attachments found... */
if(isset($structure->parts) && count($structure->parts))
{
for($i = 0; $i < count($structure->parts); $i++)
{
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters)
{
foreach($structure->parts[$i]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters)
{
foreach($structure->parts[$i]->parameters as $object)
{
if(strtolower($object->attribute) == 'name')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
//peter added ------------PROBLEM HERE
if(isset($structure->parts[$i]->parts))
{
//echo 'ano';
for($d = 0; $d < count($structure->parts[$i]->parts); $d++)
{
foreach($structure->parts[$i]->parts[$d]->parameters as $object)
{
//print_r($object);
if(strtolower($object->attribute) == 'name')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
$attachments[$i]['encoding'] = $structure->parts[$i]->parts[$d]->encoding;
}
//print_r($attachments);
}
}
}
if($attachments[$i]['is_attachment'])
{
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
//$attachments[$i]['attachment'] = 'aaaa';
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 3 = BASE64 encoding */
elseif($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
// /* 3 = BASE64 encoding */
// elseif($attachments[$i]['encoding'] == 3)
// {
// $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
// }
// /*4 = QUOTED-PRINTABLE encoding */
// elseif($attachments[$i]['encoding'] == 4)
// {
// $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
// }
}
}
}
// print_r($attachments);
/* iterate through each attachment and save it */
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
if(empty($filename)) $filename = $attachment['filename'];
if (mb_substr($filename, 0, 8)=='=?UTF-8?' || mb_substr($filename, 0, 8)=='=?utf-8?') { $filename = imap_utf8($filename); $filename = utf8_decode($filename); }
if(empty($filename)) $filename = time() . ".dat";
/* prefix the email number to the filename in case two emails
* have the attachment with the same file name.
*/
$date = date('Ymd_His',$overview[0]->udate);
$fp = fopen($date. ' - '. $email_number . "-" . $filename, "w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
// print_r($filename);
// print_r($attachment);
}
}
if($count++ >= $max_emails) break;
}
}
/* close the connection */
imap_close($inbox);