Intenta comprender el código bien previamente a adaptarlo a tu trabajo si tquieres aportar algo puedes compartirlo con nosotros.
Solución:
No está proporcionando el número de sección correcto para los archivos adjuntos anidados. Debe pasar el número de sección en el paso recursivo.
function parse_parts($parts, $parentsection = "")
foreach($parts as $subsection => $part)
$section = $parentsection . ($subsection + 1);
if(isset($part->parts))
// some mails have one extra dimension
$this->parse_parts($part->parts, $section . "." );
elseif(isset($part->disposition))
if(in_array(strtolower($part->disposition), array('attachment','inline')))
$data = imap_fetchbody($this->stream, $this->msgno, $section );
echo 'Getting section ' . $section;
echo $part->dparameters[0]->value.' '.strlen($data)."n";
(No probado, pero debería hacer lo correcto …)
El siguiente código le muestra los directorios y el correo electrónico en el INBOX
$mailbox = imap_open ("correo.servidor.com:993/imap/ssl/novalidate-certINBOX", "[email protected]", "PASSWORD");
if (!$mailbox)
die('murio');
echo "Buzones
n";
$carpetas = imap_listmailbox($mailbox, "correo.patronato.unam.mx:993", "*");
if ($carpetas == false)
echo "Llamada fallida
n";
else
foreach ($carpetas as $val)
echo $val . "
n";
echo "Cabeceras en INBOX
n";
$cabeceras = imap_headers($mailbox);
if ($cabeceras == false)
echo "Llamada fallida
n";
else
foreach ($cabeceras as $val)
echo $val . "
n";
$numMessages = imap_num_msg($mailbox);
for ($i = $numMessages; $i > 0; $i--)
$header = imap_header($mailbox, $i);
$fromInfo = $header->from[0];
$replyInfo = $header->reply_to[0];
// print_r($header);
$details = array(
"fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
? $fromInfo->mailbox . "@" . $fromInfo->host : "",
"fromName" => (isset($fromInfo->personal))
? $fromInfo->personal : "",
"replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
? $replyInfo->mailbox . "@" . $replyInfo->host : "",
"replyName" => (isset($replyTo->personal))
? $replyto->personal : "",
"subject" => (isset($header->subject))
? $header->subject : "",
"udate" => (isset($header->udate))
? $header->udate : "",
"Unseen" => (isset($header->Unseen))
? $header->Unseen : "-"
);
$uid = imap_uid($mailbox, $i);
echo "";
echo "- From:" . $details["fromName"];
echo " " . $details["fromAddr"] . "
";
echo "- Subject: " . $details["subject"] . "
";
echo "- Estatus: " . $details["Unseen"] . "
";
echo '- Read
';
echo "
";
imap_close($mailbox);
El código test_imap_attachment.php que muestra los archivos adjuntos
function getAttachments($imap, $mailNum, $part, $partNum)
$attachments = array();
if (isset($part->parts))
foreach ($part->parts as $key => $subpart)
if($partNum != "")
$newPartNum = $partNum . "." . ($key + 1);
else
$newPartNum = ($key+1);
$result = getAttachments($imap, $mailNum, $subpart,
$newPartNum);
if (count($result) != 0)
array_push($attachments, $result);
else if (isset($part->disposition))
// print_r($part);
if (strtoupper($part->disposition) == "ATTACHMENT")
$partStruct = imap_bodystruct($imap, $mailNum, $partNum);
$attachmentDetails = array(
"name" => $part->dparameters[0]->value,
"subtype" => $partStruct->subtype,
"partNum" => $partNum,
"enc" => $partStruct->encoding
);
return $attachmentDetails;
return $attachments;
$mailbox = imap_open ("correo.servidor.com:993/imap/ssl/novalidate-certINBOX", "[email protected]", "PASSWORD");
$uid = $_GET['uid'];
$mailStruct = imap_fetchstructure($mailbox, $uid);
$attachments = getAttachments($mailbox, $uid, $mailStruct, "");
echo "Attachments: ";
echo "";
foreach ($attachments as $attachment)
echo '- ' .
$attachment["name"] . "
";
echo "
";
El siguiente código guarda el archivo en el mismo servidor: test_imap_download.php
function downloadAttachment($imap, $uid)
$structure = imap_fetchstructure($imap, $uid);
$attachments = '';
if(isset($structure->parts) && count($structure->parts))
for($i=0; $iparts); $i++)
if(strtoupper($structure->parts[$i]->disposition) == 'ATTACHMENT')
$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;
if($attachments[$i]['is_attachment'])
$attachments[$i]['attachment'] = imap_fetchbody($imap, $uid, $i+1);
if($structure->parts[$i]->encoding == 3) // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
elseif($structure->parts[$i]->encoding == 4) // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
file_put_contents('directorio/'.$attachments[$i]['filename'], $attachments[$i]['attachment']);
$mailbox = imap_open ("correo.servidor.com:993/imap/ssl/novalidate-certINBOX", "[email protected]", "PASSWORD");
$uid = $_GET["uid"];
$partNum = $_GET["partNum"];
downloadAttachment($mailbox, $uid);
He usado código de estas páginas:
http://www.sitepoint.com/exploring-phps-imap-library-2/
La descarga de archivos adjuntos al directorio con IMAP en PHP funciona de forma aleatoria
Puedes auxiliar nuestro ensayo añadiendo un comentario o valorándolo te lo agradecemos.