He estado investigando sobre este tema y hasta ahora solo he podido acceder a los correos de una determinada carpeta y a los adjuntos de cada uno de estos correos, pero aun no se como poder bajarlos.
Aqui les presento el codigo, en cual cuando debiera descargar el adjunto de un correo lo que hace es salir el error:
"Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\correo\imap.php:11) in C:\xampp\htdocs\correo\correo_imap.php on line 140"
alquien sabe que esta ocurriendo en este caso??.
Archivo correo_imap.php
Código __default:
Ver original[HIGHLIGHT="PHP"]
<?php
include("imap.php");
$message=$_GET['num'];
//if($mensaje->parts[1]->ifdisposition = "1" and $mensaje->parts[1]->disposition = "attachment"){Hacer lo de attachments}
$imap = imap_open("{imap.uci.cu:993/imap/ssl}Yanelis", "
[email protected]", "2011.porti") or die("No Se Pudo Conectar Al Servidor:" . imap_last_error());
//Escoger un mensaje especifico
//Mostrar todas las propiedades del mensaje
//echo "<pre>\n\n";
//print_r(imap_fetchstructure($imap, $message));
//echo "\n\n</pre>";
$mensaje = imap_fetchstructure($imap, $message);
$partes = count($mensaje->parts);
$partesreales = $partes-1;
if(!$partes)
{
echo "El Mensaje No Tiene Archivos Adjuntos<br>";
}
echo "$partes<br>";
echo "El Mensaje Tiene $partesreales Archivos Adjunto(s)!<br>";
echo "Detalles del Archivo Adjunto".$message."<br>";
$analisar = $mensaje->parts[1];
//echo "Este es un Array : $analisar->parameters<br>"; //Array
$seguimos = $analisar->dparameters[0];
echo "Nombre Del Archivo Adjunto: $seguimos->value<br>";
$final = $mensaje->parts[1]->dparameters[0]->value;
$final = trim($final, "=,?"); //Elimina ? y = del inicio o final del nombre.
echo "Nombre Del Archivo Adjunto: $final<br>";
$tamano = $mensaje->parts[1]->bytes;
$tipo = $mensaje->parts[1]->subtype;
echo "El Archivo Adjunto Tiene ".$tamano ."bytes!<br>";
echo "El Archivo Adjunto es un Archivo ".$tipo."<br>";
$attachments = array();
if(isset($mensaje->parts) && count($mensaje->parts))
{
for($i = 0; $i < count($mensaje->parts); $i++)
{
$attachments[$i] = array('is_attachment' => false,'filename' => '','name' => '','attachment' => '');
if($mensaje->parts[$i]->ifdparameters)
{
foreach($mensaje->parts[$i]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($mensaje->parts[$i]->ifparameters)
{
foreach($mensaje->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, $message, $i+1);
if($mensaje->parts[$i]->encoding == 3)
{
// 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($mensaje->parts[$i]->encoding == 4)
{
// 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
imap_close($imap);
$carpeta="C:/test/";
$file = $carpeta.$_GET['num'];
$name = $file.".rar";
header("Content-Transfer-Encoding: binary");
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=$name");
header("Content-Length: ".filesize($file));
?>
[/HIGHLIGHT]
Archivo imap.php
Código __default:
Ver original[HIGHLIGHT="PHP"]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chequear cuenta de correo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="imap_css.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<h1 class="titulo"> Webmail </h1>
<?php
$imap = imap_open ("{imap.uci.cu:993/imap/ssl}Yanelis", "
[email protected]", "2011.porti") or die("No Se Pudo Conectar Al Servidor:" . imap_last_error());
$checar = imap_check($imap);
// Detalles generales de todos los mensajes del usuario.
$resultados = imap_fetch_overview($imap,"1:{$checar->Nmsgs}",0);
// Ordenamos los mensajes arriba los ms nuevos y abajo los ms antiguos
krsort($resultados);
$cont = 0;
// Informacion del mailbox
$check = imap_mailboxmsginfo($imap);
echo "<div class='estadisticas'>";
if ($check) {
echo "Fecha: " . $check->Date . "<br/>" ;
//echo "Driver: " . $check->Driver . "<br />\n" ;
//echo "Mailbox: " . $check->Mailbox . "<br />\n" ;
echo "Total Mensajes: $check->Nmsgs | Sin Leer: $check->Unread | Recientes: $check->Recent | Eliminados: $check->Deleted <br/>";
echo "Tamao buzn: " . $check->Size . "<br/><br/>" ;
} else {
echo "imap_check() failed: " . imap_last_error() . "<br />\n";
}
echo "</div>";
// MOSTRAMOS EL MENSAJE
echo "-------------------------------------------------------<br />";
if (isset($_GET['num'])){
$num_mensaje=$_GET['num'];
echo "Mostrando cuerpo del mensaje #$num_mensaje<br/>";
$cont=0;
foreach ($resultados as $detalles) {
$cont = $cont + 1;
if ($cont == $num_mensaje){
$asunto=$detalles->subject;
echo "<p class='asunto'>$asunto</p>";}
}
$section = 1;
$mensaje = imap_fetchbody($imap, $num_mensaje, $section);
echo nl2br(strip_tags($mensaje,'<p>')); // Util para los mensajes HTML, los transforma en texto plano
}else{
echo "Mensaje no encontrado<br/>";
}
echo "<br />-------------------------------------------------------<br />";
?>
<table class='tabla1'>
<thead>
<tr>
<th scope="col" title="Mensaje">Msj</th>
<th scope="col" title="Remitente">Remitente</th>
<th scope="col" title="Asunto">Asunto</th>
<th scope="col" title="Tamao">Tamao</th>
<th scope="col" title="Fecha">Fecha</th>
<th scope="col" title="Leido">Leido</th>
</tr>
</thead>
<?php
//$i=0;
foreach ($resultados as $detalles) {
echo "<tr>";
//echo "Para: $detalles->to <br>";
// Ponemos 'Sin asunto' en caso que no tenga.
if ($detalles->subject == ''){$subject='Sin asunto';}
else{
//Evita asuntos tipo =?ISO-8859-1?Q?B=F8lla?=
$subject = utf8_decode(imap_utf8($detalles->subject));
}
$from = utf8_decode(imap_utf8($detalles->from));
echo "<td><b>#$detalles->msgno</b></td>";
echo "<td><b>$from</b></td>";
echo "<td><a href='correo_imap.php?num=$detalles->msgno'><b>$subject</b></a></td>";
echo "<td><b>$detalles->size bytes</b></td>";
echo "<td><b>$detalles->date</b></td>";
if($detalles->seen == "0") {
echo "<td><b>Sin leer</b></td>";
$cont = $cont + 1;
} else {
echo "<td>Leido</td>";
}
//$servidorenvia = strstr($detalles->message_id, '@');
//echo "Dominio Que Envia: $servidorenvia<br><hr>";
echo "</tr>";
// $i=$i+1;
// $mi_array=array($i=>$detalles->msgno,$from,$subject,$detalles->size,$detalles->date);
}
echo "</table>";
//foreach ($mi_array as $indice=>$actual)
// echo $actual . "<br>";
imap_close($imap);
?>
<div id="footer"> <p>Tratamiento de correo via IMAP </p></div>
</body>
</html>
[/HIGHLIGHT]