Ver Mensaje Individual
  #3 (permalink)  
Antiguo 24/10/2009, 03:32
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 2 meses
Puntos: 20
Respuesta: Optimizar script IMAP

Cita:
Iniciado por GatorV Ver Mensaje
Sí son muchos correos mejor solo extrae una parte de los correos e implementa un paginador para poderte mover entre paginas de correos.

Saludos.
He comentado las lineas en las que recuperaba el body del mensaje, ya que para generar la vista previa de la tabla no lo veía necesario.

También he añadido un pequeño script para saber cuanto tarda en cargar la página y sigue siendo muy muy lento! y eso que solo muestra 15 mensajes! no 50 ni 100

La página fue creada en 26.1034967899 segundos



Código php:
Ver original
  1. <?$mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $tiempoinicial = $mtime; ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Chequear cuenta de correo</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <link href="imap_css.css" rel="stylesheet" type="text/css" media="all" />
  8.  
  9. </head>
  10. <body>
  11. <h1 class="titulo"> Webmail BETA </h1>
  12.  
  13. <?
  14. $imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", "user", "pass") or die("No Se Pudo Conectar Al Servidor:" . imap_last_error());
  15. $checar = imap_check($imap);
  16.  
  17. // Detalles generales de todos los mensajes del usuario.
  18. $resultados = imap_fetch_overview($imap,"1:{$checar->Nmsgs}",0);
  19. // Ordenamos los mensajes arriba los más nuevos y abajo los más antiguos
  20. krsort($resultados);
  21.  
  22.  
  23. // Informacion del mailbox
  24. $check = imap_mailboxmsginfo($imap);
  25.  
  26. echo "<div class='estadisticas'>";
  27. if ($check) {
  28.     echo "Fecha: "     . $check->Date    . "<br/>" ;
  29.     echo "Total Mensajes: $check->Nmsgs | Sin Leer: $check->Unread | Recientes: $check->Recent | Eliminados: $check->Deleted <br/>";
  30.     echo "Tamaño buzón: " . $check->Size . "<br/><br/>" ;
  31. } else {
  32.     echo "imap_check() failed: " . imap_last_error() . "<br />\n";
  33. }
  34. echo "</div>";
  35.  
  36.  
  37. // MOSTRAMOS EL MENSAJE
  38. echo "-------------------------------------------------------<br />";
  39. if (isset($_GET['num'])){
  40.     $num_mensaje=$_GET['num'];
  41.     echo "Mostrando cuerpo del mensaje #$num_mensaje<br/>";
  42.     $cont=0;
  43.     foreach ($resultados as $detalles) {
  44.         $cont = $cont + 1;
  45.         if ($cont == $num_mensaje){
  46.             $asunto=$detalles->subject;
  47.             echo "<p class='asunto'>$asunto</p>";}
  48.     }
  49.     $section = 1;
  50.     $mensaje = imap_fetchbody($imap, $num_mensaje, $section);
  51.     echo nl2br(strip_tags($mensaje,'<p>')); // Util para los mensajes HTML, los transforma en texto plano
  52.    
  53. }else{
  54.     echo "Mensaje no encontrado<br/>";
  55. }
  56. echo "<br />-------------------------------------------------------<br />";
  57.  
  58. ?>
  59. <table class='tabla1'>
  60. <thead>
  61.     <tr>
  62.         <th scope="col" title="Mensaje">Msj</th>
  63.         <th scope="col" title="Remitente">Remitente</th>
  64.         <th scope="col" title="Asunto">Asunto</th>
  65.         <th scope="col" title="Tamaño">Tamaño</th>
  66.         <th scope="col" title="Fecha">Fecha</th>
  67.         <th scope="col" title="Leido">Leido</th>
  68.         <th scope="col" title="Adjunto">ADJ</th>
  69.     </tr>
  70. </thead>    
  71. <?
  72.  
  73. foreach ($resultados as $detalles) {
  74.     echo "<tr>";
  75.        
  76.         // Ponemos 'Sin asunto' en caso que no tenga.
  77.         if ($detalles->subject == ''){$subject='Sin asunto';}
  78.         else{
  79.             //Evita asuntos tipo =?ISO-8859-1?Q?B=F8lla?=
  80.             $subject = utf8_decode(imap_utf8($detalles->subject));
  81.         }
  82.        
  83.         /* OBTENER EL FROM DEL MENSAJE */
  84.         $header = imap_header($imap, $detalles->msgno);
  85.         $from = $header->from;
  86.         foreach ($from as $id => $object) {
  87.             $fromname = $object->personal;
  88.             $fromaddress = $object->mailbox . "@" . $object->host;
  89.         } /* FIN DEL FROM */
  90.  
  91.        
  92.         echo "<td><b>#$detalles->msgno</b></td>";
  93.         echo "<td><b>$fromaddress</b></td>";
  94.         echo "<td><a href='leer_correo.php?num=$detalles->msgno'><b>$subject</b></a></td>";
  95.  
  96.         $tamanyo=$detalles->size;
  97.         $size=round($tamanyo/1024,2); // Pasamos a KB
  98.         if ($tamanyo<100000){
  99.             echo "<td><b> $size KB</b></td>";
  100.         }
  101.         else{
  102.             $size=round($size/1024,2); // Pasamos a MB
  103.             echo "<td><b> $size MB</b></td>";
  104.            
  105.         }
  106.         $fecha = date("Y-m-d H:i:s", strtotime($detalles->date));
  107.  
  108.         echo "<td><b>$fecha</b></td>";
  109.     if($detalles->seen == "0") {
  110.         echo "<td><b>Sin leer</b></td>";
  111.         $cont = $cont + 1;
  112.     } else {
  113.         echo "<td>Leido</td>";
  114.     }
  115.    
  116.     /* ========== TRATAR ADJUNTOS ================== */
  117.    
  118.      $mid=$detalles->msgno;
  119.      $struct = imap_fetchstructure($imap, $mid);
  120.        
  121.         $parts = $struct->parts;
  122.         $i = 0;
  123.  
  124.         if (!$parts) { /* Simple message, only 1 piece */
  125.           $attachment = array(); /* No attachments */
  126.           //$content = imap_body($imap, $mid);
  127.         } else { /* Complicated message, multiple parts */
  128.        
  129.           $endwhile = false;
  130.        
  131.           $stack = array(); /* Stack while parsing message */
  132.           $content = "";    /* Content of message */
  133.           $attachment = array(); /* Attachments */
  134.        
  135.           while (!$endwhile) {
  136.             if (!$parts[$i]) {
  137.               if (count($stack) > 0) {
  138.                 $parts = $stack[count($stack)-1]["p"];
  139.                 $i     = $stack[count($stack)-1]["i"] + 1;
  140.                 array_pop($stack);
  141.               } else {
  142.                 $endwhile = true;
  143.               }
  144.             }
  145.          
  146.             if (!$endwhile) {
  147.               /* Create message part first (example '1.2.3') */
  148.               $partstring = "";
  149.               foreach ($stack as $s) {
  150.                 $partstring .= ($s["i"]+1) . ".";
  151.               }
  152.               $partstring .= ($i+1);
  153.            
  154.               if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */
  155.                 $attachment[] = array("filename" => $parts[$i]->parameters[0]->value,
  156.                                       "filedata" => imap_fetchbody($imap, $mid, $partstring));
  157.               } //elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */
  158.                 //$content .= imap_fetchbody($imap, $mid, $partstring);
  159.               //}
  160.             }
  161.  
  162.             if ($parts[$i]->parts) {
  163.               $stack[] = array("p" => $parts, "i" => $i);
  164.               $parts = $parts[$i]->parts;
  165.               $i = 0;
  166.             } else {
  167.               $i++;
  168.             }
  169.           } /* while */
  170.         } /* complicated message */
  171.  
  172.        // echo "Analyzed message $mid, result: <br />";
  173.        // echo "Contenido: $content<br /><br />";
  174.         $num_adjuntos=count($attachment);
  175.         //echo "Numero de adjuntos: $num_adjuntos <br /><br />";
  176.        
  177.         if ($num_adjuntos>0){
  178.             echo "<td>Si</td>";
  179.             for ($i=0;$i<$num_adjuntos;$i++){
  180.                 $nombre=$attachment[$i]['filename'];
  181.                 //echo "Nombre del fichero: $nombre <br />";
  182.             }
  183.         }else{
  184.             echo "<td>No</td>";
  185.         }
  186.    
  187.     /* ========== FIN ADJUNTO ================== */
  188.     echo "</tr>";  
  189.  
  190. } // fin foreach resultados
  191. echo "</table>";
  192.  
  193. imap_close($imap);
  194.  
  195. $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $tiempofinal = $mtime; $tiempototal = ($tiempofinal - $tiempoinicial); echo "La página fue creada en ".$tiempototal." segundos";
  196. ?>
  197. <div id="footer"> <p>Tratamiento de correo via IMAP - BETA 1.5</p></div>
  198. </body>
  199. </html>