PX : code

quickmail by Johan Fitie
Download this code


<?php
 
// (c) 2004-2005 J.H. Fitiť - www.johanfitie.com
 //   quickmail.php <- rss feed / xml output using css
 //   quickmail.php?wml <- wml output for wap browsers
 
define ("VERSION","090605-4");

   
showmailboxes ("your.mail.server","user","pass");
   exit (
0);

 
// showmailboxes

   
function showmailboxes($server,$user,$pass) {

    
$mode="rss";
    if (isset(
$_GET["wml"])) {$mode="wml";}
    if (isset(
$_GET["rss"])) {$mode="rss";}
    if (isset(
$_GET["css"])) {$mode="css";}

    
$timezone=3600*2;
    
$currdate=time()+$timezone+3600*(date("I"));

    
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    
header("Last-Modified: ".gmdate("D, d M Y H:i:s",$currdate)." GMT");
    
header("Cache-Control: no-cache, must-revalidate");
    
header("Pragma: no-cache");

    if (
$_GET["image"]=="envelope") {
     
$envel=       "iVBORw0KGgoAAAANSUhEUgAAABkAAAARBAMAAADalBo9AAAAB3RJTUUH1QULECUhKXpMhAAAAAlw";
     
$envel=$envel."SFlzAAALEgAACxIB0t1+/AAAAARnQU1BAACxjwv8YQUAAAAhUExURVLnGLW1tc7Ozr29vcbGxt7e";
     
$envel=$envel."3vf39+/v79bW1ufn5////00eqBYAAAABdFJOUwBA5thmAAAAuUlEQVR42k3OsQ6CMBSF4bqocUF8";
     
$envel=$envel."BHRgNTq4i4kzCdA6uXCFjaUgo0ZamDQmBeriZnxM2wSNf3KHbzoXoa6ROuOLAXn8Kcrw/qd+AJT/";
     
$envel=$envel."FNU8w4mBhp6OvGkjKgPxFADCmksi3SdiooYDiXLaSveKWI4l3fEmILI0lITtQUypFEyJeucCICBZ";
     
$envel=$envel."m2jNpO1DmOWV2mP3TVt6ACm9KfFpYTlbH15Y/1mtLs56UUB80rKWpmriH+daPbNrrPQBTiI9CJRt";
     
$envel=$envel."ZQoAAAAASUVORK5CYII=";
     
header ("Content-Type: image/png");
     
header ("Content-Disposition: inline; filename=envelope.png");
     echo 
base64_decode($envel);
     exit (
0);
    }

    if (
$mode=="css") {
     
header ("Content-Type: text/css");
     
header ("Content-Disposition: inline; filename=quickmail.css");
     echo 
"rss {\n";
     echo 
" display: block;\n";
     echo 
" background: #ffffff;\n";
     echo 
" color: #808080;\n";
     echo 
" font-family: verdana, arial, helvetica;\n";
     echo 
"}\n";
     echo 
"\n";
     echo 
"item {\n";
     echo 
" display: block;\n";
     echo 
" margin-top: 15px;\n";
     echo 
" margin-left: 15px;\n";
     echo 
" background: #ffffff url(http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?image=envelope) 5px 18px no-repeat;\n";
     echo 
" color: #808080;\n";
     echo 
" font-size: 11px;\n";
     echo 
"}\n";
     echo 
"\n";
     echo 
"title {\n";
     echo 
" display: block;\n";
     echo 
" margin-top: 5px;\n";
     echo 
" margin-left: 5px;\n";
     echo 
" border-bottom: 1px solid #d0d0d0;\n";
     echo 
" background: #f0f0ff;\n";
     echo 
" color: #000000;\n";
     echo 
" font-size: 11px;\n";
     echo 
"}\n";
     echo 
"\n";
     echo 
"pubDate, description, author, generator {\n";
     echo 
" display: block;\n";
     echo 
" margin-top: 2px;\n";
     echo 
" margin-left: 35px;\n";
     echo 
" font-size: 10px;\n";
     echo 
"}\n";
     echo 
"\n";
     echo 
"ttl, guid, webMaster, managingEditor, language, lastBuildDate, link, docs {\n";
     echo 
" display: none;\n";
     echo 
"}\n";
     exit (
0);
    }

    if (
$mode=="wml") {
     
header ("Content-Type: text/vnd.wap.wml");
     
header ("Content-Disposition: inline; filename=quickmail.wml");
     echo 
"<?xml version=\"1.0\"?>\n";
     echo 
"<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" ";
     echo 
"\"http://www.wapforum.org/DTD/wml_1.1.xml\">\n";
     echo 
"<wml>\n<card id=\"quickmail\" title=\"quickmail\">\n<p>\n";
     echo 
"$user@$server (";
     echo 
gmdate("d-m-y H:i",$currdate).")<br/>\n";
    }

    if (
$mode=="rss") {
     
header ("Content-Type: application/xml");
     
header ("Content-Disposition: inline; filename=quickmail.rss");
     echo 
"<?xml version=\"1.0\"?>\n";
     echo 
"<?xml-stylesheet type=\"text/css\" href=\"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?css\"?>\n";
     echo 
"\n<rss version=\"2.0\">\n";
     echo 
"\n <channel>\n";
     echo 
"\n  <title>quickmail</title>\n";
     echo 
"  <link>http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."</link>\n";
     echo 
"  <pubDate>".gmdate("D, d M Y H:i:s",$currdate)." GMT</pubDate>\n";
     echo 
"  <ttl>60</ttl>\n";
     echo 
"  <description>$user@$server</description>\n";
     echo 
"  <language>en-us</language>\n";
     echo 
"  <lastBuildDate>".gmdate("D, d M Y H:i:s",$currdate)." GMT</lastBuildDate>\n";
     echo 
"  <docs>http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."</docs>\n";
     echo 
"  <generator>quickmail ".VERSION."</generator>\n";
     echo 
"  <managingEditor>$user@$server</managingEditor>\n";
     echo 
"  <webMaster>$user@$server</webMaster>\n";
    }

    
$inbox=imap_open ("{".$server.":143}INBOX",$user,$pass);
    
$total=imap_num_msg($inbox); $sorted=imap_sort($inbox,SORTARRIVAL,0);

    if (
$total>0) {

     
$limit=$total-24;
     if (
$mode=="wml") {$limit=$total-9;}
     if (
$limit<1) {$limit=1;}

     if (
$_GET["msg"]) {$total=(int)$_GET["msg"];$limit=$total;}

     unset (
$output);
     
$ntotal=0;$nunread=0;
     for(
$i=$total;$i>=$limit;$i--)
     {
      
$ntotal++;
      
$headers=imap_header($inbox,$sorted[$i-1]);

      unset (
$hreadindicator);
      if (
$headers->Unseen=="U"||$headers->Recent=="N") {$hreadindicator="[*] ";$nunread++;}

      unset (
$hmaildate);unset ($hrssmaildate);
      
$thisdate=$headers->udate+$timezone+3600*(date("I"));
      
$hmaildate=gmdate("d-m-y H:i",$thisdate);
      
$hrssmaildate=gmdate("D, d M Y H:i:s",$thisdate);

      unset (
$hsubject);
      
$hsubject=mb_decode_mimeheader($headers->Subject);
      
$hsubject=eregi_replace("[^ -!#-%'-;a-~A-_0-9?-@]","",$hsubject);
      if (
strlen($hsubject)>50) {$hsubject=substr($hsubject,0,50);}

      unset (
$hauthorname);unset ($hauthoremail);
      
$hauthoremail=mb_decode_mimeheader($headers->fromaddress);
      list(
$hauthorname,$devnull)=split(" <",$hauthoremail,2);
      
$hauthorname=eregi_replace("[^ -!#-%'-;a-~A-_0-9?-@]","",$hauthorname);
      if (
strlen($hauthorname)>25) {$hauthorname=substr($hauthorname,0,25);}
      if (
strstr($hauthoremail,"<")) {list ($devnull,$hauthoremail)=split("<",$hauthoremail,2);}
      if (
strstr($hauthoremail,">")) {list ($hauthoremail,$devnull)=split(">",$hauthoremail,2);}
      
$hauthoremail=eregi_replace("[^ -!#-%'-;a-~A-_0-9?-@]","",$hauthoremail);

      unset (
$hbodypart);
      
$hbodypart=imap_fetchbody($inbox,$sorted[$i-1],1,FT_PEEK);
      
$hbodypart=eregi_replace("[^ -!#-%'-;a-~A-_0-9?-@]","",$hbodypart);
      
$hbodypart=substr($hbodypart,0,120);
      
$hbodypart=htmlentities($hbodypart);

      if (
$mode=="wml") {
       
$output=$output."<br/>\n$hmaildate<br/>\n$hreadindicator$hauthorname<br/>\n\"$hsubject\"<br/>\n";
       
$output=$output."<i>$hbodypart</i><br/>\n";
      }

      if (
$mode=="rss") {
       
$output=$output."\n  <item>\n";
       
$output=$output."   <title>$hreadindicator$hauthorname \"$hsubject\"</title>\n";
       
$output=$output."   <link>http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?msg=".$sorted[$i-1]."</link>\n";
       
$output=$output."   <pubDate>$hrssmaildate GMT</pubDate>\n";
       
$output=$output."   <guid isPermaLink=\"false\">".md5("msg".$sorted[$i-1])."</guid>\n";
       
$output=$output."   <author>$hauthoremail ($hauthorname)</author>\n";
       
$output=$output."   <description>$hbodypart</description>\n";
       
$output=$output."  </item>\n";
      }

     }
    }

    
imap_close($inbox);

    if (
$mode=="rss"&&!$_GET["msg"]) {
     echo 
"\n  <item>\n";
     echo 
"   <title>".gmdate("d-m-y H:i",$currdate)." - $nunread unread, $ntotal total</title>\n";
     echo 
"   <link>http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."</link>\n";
     echo 
"   <pubDate>".gmdate("D, d M Y",$currdate)." 23:59:59 GMT</pubDate>\n";
     echo 
"   <guid isPermaLink=\"false\">".md5("mbxstatus0")."</guid>\n";
     echo 
"   <author>$user@$server</author>\n";
     echo 
"   <description>Mailbox status</description>\n";
     echo 
"  </item>\n";
    }

    echo 
$output;

    if (
$mode=="wml") {
     echo 
"<br/>\n</p>\n</card>\n</wml>\n";
    }

    if (
$mode=="rss") {
     echo 
"\n </channel>\n\n</rss>\n";
    }

   }
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.