Buenas,
He desarrollado el siguiente script que mira el buzón de tu cuenta de correo de gmail y muestra en una tabla los siguientes campos:
Msj = Numero de mensaje
Remitente = Dirección del from
Asunto = Asunto del correo
Tamaño = Tamaño en KB o MB segun corresponda
Fecha = Fecha en formato Y-m-d H:i:s
Leido = Te dice si el correo ha sido leido (SI/NO)
ADJ = Te dice si contiene adjuntos (SI/NO)
El problema es que es muy lento y la a la que haya muchos correos da error de timeout, por defecto tenía 30, lo he subido a 60 pero no consigo solucionar mucho las cosas.
Recurro a vuestra ayuda por si veis que se pueda optimizar mi script.
Muchas gracias de antemano!
LEER_CORREO.PHP
Código php:
Ver original<!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 BETA </h1>
<?
$imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", "user", "pass") or
die("No Se Pudo Conectar Al Servidor:" . imap_last_error());
// Detalles generales de todos los mensajes del usuario.
// Ordenamos los mensajes arriba los más nuevos y abajo los más antiguos
$cont = 0;
// Informacion del mailbox
echo "<div class='estadisticas'>";
if ($check) {
echo "Fecha: " . $check->Date . "<br/>" ; 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 "</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;
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>
<th scope="col" title="Adjunto">ADJ</th>
</tr>
</thead>
<?
foreach ($resultados as $detalles) {
echo "<tr>";
// Ponemos 'Sin asunto' en caso que no tenga.
if ($detalles->subject == ''){$subject='Sin asunto';}
else{
//Evita asuntos tipo =?ISO-8859-1?Q?B=F8lla?=
}
/* OBTENER EL FROM DEL MENSAJE */
$from = $header->from;
foreach ($from as $id => $object) {
$fromname = $object->personal;
$fromaddress = $object->mailbox . "@" . $object->host;
} /* FIN DEL FROM */
echo "<td><b>#$detalles->msgno</b></td>";
echo "<td><b>$fromaddress</b></td>";
echo "<td><a href='leer_correo.php?num=$detalles->msgno'><b>$subject</b></a></td>";
$tamanyo=$detalles->size;
$size=round($tamanyo/1024,2); // Pasamos a KB if ($tamanyo<100000){
echo "<td><b> $size KB</b></td>";
}
else{
$size=round($size/1024,2); // Pasamos a MB echo "<td><b> $size MB</b></td>";
}
echo "<td><b>$fecha</b></td>";
if($detalles->seen == "0") {
echo "<td><b>Sin leer</b></td>";
$cont = $cont + 1;
} else {
echo "<td>Leido</td>";
}
/* ========== TRATAR ADJUNTOS ================== */
$mid=$detalles->msgno;
$parts = $struct->parts;
$i = 0;
if (!$parts) { /* Simple message, only 1 piece */
$attachment = array(); /* No attachments */ } else { /* Complicated message, multiple parts */
$endwhile = false;
$stack = array(); /* Stack while parsing message */ $content = ""; /* Content of message */
$attachment = array(); /* Attachments */
while (!$endwhile) {
if (!$parts[$i]) {
$parts = $stack[count($stack)-1]["p"]; $i = $stack[count($stack)-1]["i"] + 1; } else {
$endwhile = true;
}
}
if (!$endwhile) {
/* Create message part first (example '1.2.3') */
$partstring = "";
foreach ($stack as $s) {
$partstring .= ($s["i"]+1) . ".";
}
$partstring .= ($i+1);
if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */ $attachment[] = array("filename" => $parts[$i]->parameters[0]->value, } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */ }
}
if ($parts[$i]->parts) {
$stack[] = array("p" => $parts, "i" => $i); $parts = $parts[$i]->parts;
$i = 0;
} else {
$i++;
}
} /* while */
} /* complicated message */
// echo "Analyzed message $mid, result: <br />";
// echo "Contenido: $content<br /><br />";
$num_adjuntos=count($attachment); //echo "Numero de adjuntos: $num_adjuntos <br /><br />";
if ($num_adjuntos>0){
echo "<td>Si</td>";
for ($i=0;$i<$num_adjuntos;$i++){
$nombre=$attachment[$i]['filename'];
//echo "Nombre del fichero: $nombre <br />";
}
}else{
echo "<td>No</td>";
}
/* ========== FIN ADJUNTO ================== */
echo "</tr>";
}
echo "</table>";
?>
<div id="footer"> <p>Tratamiento de correo via IMAP - BETA 1.5</p></div>
</body>
</html>
IMAP_CSS.CSS
Código CSS:
Ver original/* Hoja de estilo */
/* ESTRUCTURA BASICA
-------------------------------------------*/
html,body {font-size: 1em;}
body {
color: #333;
background-color: #fff;
height:100%;
width: 95%;
text-align: left;
min-width:850px;
margin: 2em auto;
padding: 0;
}
#footer {
color: #666;
background:#eee;
border: 1px solid #e5e5e5;
border-width: 0 2px 2px 0;
text-align:center;
padding-top:5px;
font-family:Arial;
font-weight:bold;
margin-top:15px;
}
a {text-decoration:none; color:#333;}
a:hover {color:#2BBA82;}
/* ESTILO TEXTO
------------------------------------------- */
.titulo{
color:#333;
font-size: 1.30em;
font-weight:normal;
text-align:justify;
line-height:20px;
padding:0px 5px 5px 20px;
margin:0.5em 0 1em;
}
.asunto{
color:#60B6EF;
font-weight:bold;
font-size:1.2em;
}
.estadisticas{
color:#333;
font-size: 1em;
font-weight:normal;
text-align:justify;
line-height:20px;
padding:0px 5px 5px 20px;
font-family:Arial;
}
/* TABLAS ESTILO
------------------------------------------- */
table,td
{
border:1px solid #CCC;
border-collapse:collapse;
font:small/1.4 Verdana, "Tahoma", "Bitstream Vera Sans", Helvetica, sans-serif;
}
table
{
border:none;
border:1px solid #CCC;
width:100%;
}
thead th,
tbody th
{
background:#fafafb;
color:#666;
padding:5px 10px;
border-left:1px solid #CCC;
font-weight:bold;
text-align:center;
font-size:11px;
vertical-align:middle;
}
tbody th
{
background:#fafafb;
border-top:1px solid #CCC;
text-align:left;
font-weight:normal;
}
tbody tr td
{
padding:5px 10px;
color:#666;
cursor:pointer;
font-size:11px;
text-align:center;
vertical-align: middle;
}
tbody tr:hover {background:#F0F7FC;}
tbody tr:hover td {background:#F0F7FC;}
tfoot td,
tfoot th
{
border-left:none;
border-top:1px solid #CCC;
padding:4px;
background:#FFF url(../imagenes/tablas/foot_bck.gif) repeat;
color:#666;
}
caption
{
text-align:left;
font-size:100%;
padding:30px 0 10px 0px;
color:#666;
font-weight:bold;
width:500px;
}
table a:link {text-decoration:none; color:#60B6EF;}
table a:visited {color:#333;}
table a:hover {color:#2BBA82;text-decoration:none;}
table a:active {color:#60B6EF;}