Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/09/2009, 03:59
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 1 mes
Puntos: 20
Aporte: Crear script PHP para tramitar emails vía IMAP tipo CRM

Buenas,

Despues de investigar en el tema de IMAP he conseguido hacer alguna cosa que otra, faltan pocas cosas para darme por satisfecho. Me gustaría que entre todos sacasemos adelante estas partes.

Actualmente, el script se conecta a GMAIL vía IMAP y te lista todos los mensajes que tienes, marcandotelos si han sido leidos o no.

Dejo el código, funciona 100%

PHP

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Chequear cuenta de correo</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <link href="imap_css.css" rel="stylesheet" type="text/css" media="all" />
  7.  
  8. </head>
  9. <body>
  10. <h1 class="titulo"> Webmail BETA </h1>
  11.  
  12. <?
  13. $imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", "[email protected]", "PASS") or die("No Se Pudo Conectar Al Servidor:" . imap_last_error());
  14. $checar = imap_check($imap);
  15.  
  16. // Detalles generales de todos los mensajes del usuario.
  17. $resultados = imap_fetch_overview($imap,"1:{$checar->Nmsgs}",0);
  18. // Ordenamos los mensajes arriba los más nuevos y abajo los más antiguos
  19. krsort($resultados);
  20. $cont = 0;
  21.  
  22. // Informacion del mailbox
  23. $check = imap_mailboxmsginfo($imap);
  24.  
  25. echo "<div class='estadisticas'>";
  26. if ($check) {
  27.     echo "Fecha: "     . $check->Date    . "<br/>" ;
  28.     //echo "Driver: "   . $check->Driver  . "<br />\n" ;
  29.     //echo "Mailbox: "  . $check->Mailbox . "<br />\n" ;
  30.     echo "Total Mensajes: $check->Nmsgs | Sin Leer: $check->Unread | Recientes: $check->Recent | Eliminados: $check->Deleted <br/>";
  31.     echo "Tamaño buzón: " . $check->Size . "<br/><br/>" ;
  32. } else {
  33.     echo "imap_check() failed: " . imap_last_error() . "<br />\n";
  34. }
  35. echo "</div>";
  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.  
  50.     $section = 1;
  51.     $mensaje = imap_fetchbody($imap, $num_mensaje, $section);
  52.     echo nl2br(strip_tags($mensaje,'<p>')); // Util para los mensajes HTML, los transforma en texto plano
  53.    
  54. }else{
  55.     echo "Mensaje no encontrado<br/>";
  56. }
  57. echo "<br />-------------------------------------------------------<br />";
  58.  
  59. ?>
  60. <table class='tabla1'>
  61. <thead>
  62.     <tr>
  63.         <th scope="col" title="Mensaje">Msj</th>
  64.         <th scope="col" title="Remitente">Remitente</th>
  65.         <th scope="col" title="Asunto">Asunto</th>
  66.         <th scope="col" title="Tamaño">Tamaño</th>
  67.         <th scope="col" title="Fecha">Fecha</th>
  68.         <th scope="col" title="Leido">Leido</th>
  69.     </tr>
  70. </thead>   
  71. <?
  72. //$i=0;
  73. foreach ($resultados as $detalles) {
  74.     echo "<tr>";
  75.         //echo "Para: $detalles->to <br>";
  76.        
  77.         // Ponemos 'Sin asunto' en caso que no tenga.
  78.         if ($detalles->subject == ''){$subject='Sin asunto';}
  79.         else{
  80.             //Evita asuntos tipo =?ISO-8859-1?Q?B=F8lla?=
  81.             $subject = utf8_decode(imap_utf8($detalles->subject));
  82.         }
  83.         $from = utf8_decode(imap_utf8($detalles->from));
  84.        
  85.         echo "<td><b>#$detalles->msgno</b></td>";
  86.         echo "<td><b>$from</b></td>";
  87.         echo "<td><a href='correo_imap.php?num=$detalles->msgno'><b>$subject</b></a></td>";
  88.         echo "<td><b>$detalles->size bytes</b></td>";
  89.         echo "<td><b>$detalles->date</b></td>";
  90.     if($detalles->seen == "0") {
  91.         echo "<td><b>Sin leer</b></td>";
  92.         $cont = $cont + 1;
  93.     } else {
  94.         echo "<td>Leido</td>";
  95.     }
  96.    
  97.        
  98.     //$servidorenvia = strstr($detalles->message_id, '@');
  99.     //echo "Dominio Que Envia: $servidorenvia<br><hr>";
  100.     echo "</tr>";
  101.    
  102. //  $i=$i+1;
  103. //  $mi_array=array($i=>$detalles->msgno,$from,$subject,$detalles->size,$detalles->date);
  104. }
  105. echo "</table>";
  106.  
  107. //foreach ($mi_array as $indice=>$actual)
  108. //    echo $actual . "<br>";
  109.    
  110.  
  111. imap_close($imap);
  112. ?>
  113. <div id="footer"> <p>Tratamiento de correo via IMAP - BETA 1.0</p></div>
  114. </body>
  115. </html>

CSS (imap_css.css)
Código CSS:
Ver original
  1. /* Hoja de estilo */
  2.  
  3. /* ESTRUCTURA BASICA
  4. -------------------------------------------*/
  5. html,body {font-size: 1em;}
  6.  
  7. body {
  8.     color: #333;
  9.     background-color: #fff;
  10.     height:100%;
  11.     width: 95%;
  12.     text-align: left;
  13.     min-width:850px;
  14.     margin: 2em auto;
  15.     padding: 0;
  16. }
  17.  
  18. #footer {
  19.     color: #666;
  20.     background:#eee;
  21.     border: 1px solid #e5e5e5;
  22.     border-width: 0 2px 2px 0;
  23.     text-align:center;
  24.     padding-top:5px;
  25.     font-family:Arial;
  26.     font-weight:bold;
  27.     margin-top:15px;
  28. }
  29.  
  30. a {text-decoration:none; color:#333;}
  31. a:hover {color:#2BBA82;}
  32.  
  33. /* ESTILO TEXTO
  34. ------------------------------------------- */
  35. .titulo{
  36.     color:#333;
  37.     font-size: 1.30em;
  38.     font-weight:normal;
  39.     text-align:justify;
  40.     line-height:20px;
  41.     padding:0px 5px 5px 20px;
  42.     margin:0.5em 0 1em;
  43. }
  44.  
  45. .asunto{
  46.     color:#60B6EF;
  47.     font-weight:bold;
  48.     font-size:1.2em;
  49. }
  50.  
  51. .estadisticas{
  52.     color:#333;
  53.     font-size: 1em;
  54.     font-weight:normal;
  55.     text-align:justify;
  56.     line-height:20px;
  57.     padding:0px 5px 5px 20px;
  58.     font-family:Arial;
  59. }
  60.  
  61. /* TABLAS ESTILO
  62. ------------------------------------------- */
  63. table,td
  64. {
  65.     border:1px solid #CCC;
  66.     border-collapse:collapse;
  67.     font:small/1.4 Verdana, "Tahoma", "Bitstream Vera Sans", Helvetica, sans-serif;
  68. }
  69. table
  70. {
  71.     border:none;
  72.     border:1px solid #CCC;
  73.     width:100%;
  74. }
  75. thead th,
  76. tbody th
  77. {
  78.  
  79.   background:#fafafb;
  80.   color:#666;  
  81.   padding:5px 10px;
  82.   border-left:1px solid #CCC;
  83.   font-weight:bold;
  84.   text-align:center;
  85.   font-size:11px;
  86.   vertical-align:middle;
  87. }
  88. tbody th
  89. {
  90.   background:#fafafb;
  91.   border-top:1px solid #CCC;
  92.   text-align:left;
  93.   font-weight:normal;
  94.  
  95. }
  96. tbody tr td
  97. {
  98.   padding:5px 10px;
  99.   color:#666;
  100.   cursor:pointer;
  101.   font-size:11px;
  102.   text-align:center;
  103.   vertical-align: middle;
  104. }
  105. tbody tr:hover {background:#F0F7FC;}
  106. tbody tr:hover td {background:#F0F7FC;}
  107.  
  108. tfoot td,
  109. tfoot th
  110. {
  111.   border-left:none;
  112.   border-top:1px solid #CCC;
  113.   padding:4px;
  114.   background:#FFF url(../imagenes/tablas/foot_bck.gif) repeat;
  115.   color:#666;
  116. }
  117. caption
  118. {
  119.     text-align:left;
  120.     font-size:100%;
  121.     padding:30px 0 10px 0px;
  122.     color:#666;
  123.     font-weight:bold;
  124.     width:500px;
  125. }
  126. table a:link {text-decoration:none; color:#60B6EF;}
  127. table a:visited {color:#333;}
  128. table a:hover {color:#2BBA82;text-decoration:none;}
  129. table a:active {color:#60B6EF;}


Quisiera conseguir lo siguiente:
1. Paginar los correos
2. Mostrar bien remitente, asunto, sin interrogante en lugar de acentos
3. Mostrar fecha en formato dd-mm-aaaa hh:mm
4. Saber cómo está escrito el mensaje si UTF-8 o ISO-8859-1 / (texto plano o html)
5. Saber si tiene adjuntos
6. Ver / Descargar correo original

Cada vez que consigamos un punto nuevo iré actualizando este post inicial.

Muchas gracias a todos!

Última edición por neodani; 19/10/2009 a las 16:31