Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/07/2010, 08:41
danielfl1234
 
Fecha de Ingreso: julio-2010
Mensajes: 30
Antigüedad: 13 años, 9 meses
Puntos: 0
Mensaje correo imap con php

mi proble es que no soy capaz de bajarme un archivo adjunto del correo de gmail.

si puedo leer el correo ver que tiene o que no tiene un archivo adjunto pero no se como descargarlo.

soy un novato en esto aver si alguien me puede hechar una manita.

pongo el codigo que uso

!
Cita:
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<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>

<?
$imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", "mi [email protected]", "mipwd") 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 más nuevos y abajo los más 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 "Tamaño buzón: " . $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="Tamaño">Tamaño</th>
<th scope="col" title="Fecha">Fecha</th>
<th scope="col" title="Leido">Leido</th>
</tr>
</thead>
<?
//$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>


ahora desde aqui llamo a correo imap.php

que es este que me muestra casi todo lo que quiero ver de mi correo




<?
$message=$_GET["num"];



//if($mensaje->parts[1]->ifdisposition = "1" and $mensaje->parts[1]->disposition = "attachment"){Hacer lo de attachments}
$imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", "my [email protected]", "mi contrasema") 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>"; //nuevo.gif
$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>";
imap_close($imap);




$attachments = array();
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;
}
}
}

if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $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']);
}
}
}
}




$carpeta="carpetadearchivos/";
$file=$carpeta.$_GET["num"];
header("Content-Transfer-Encoding: binary");
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Length: ".filesize($file));
readfile($file);




/*
if(file_exists($carpeta.basename($_GET['num']))
{$file=$carpeta.$_GET["num"];
header("Content-Transfer-Encoding: binary");
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Length: ".filesize($file));
readfile($file);
}
else
{ */
?>
Usted esta accediendo a un archivo que no existe en la carpeta de descargas.
<?
//dasdsad










pero no descarga el adjunto



gracias de antemano.