Source Code For gmail.php

Turn Line Numbers Off
  1. <?php
  2. function getmicrotime(){
  3.     list($usec$sec) = explode(" ",microtime()); 
  4.     return ((float)$usec + (float)$sec); 
  5. }
  6. $time_before=getmicrotime();
  7. $refresh=3;
  8. if (!$_GET['avg_time']) {
  9.     $avg_time=3;
  10. }else{
  11.     $avg_time=$_GET['avg_time'];
  12. }
  13. if ($_GET['stop']=='ok'){
  14.         $next=$_GET['id']+1;
  15.         $log=fopen("gmail_log.html","a+");
  16.         fwrite($log,"<p>Stopped on ".date("d/m/Y @ H:i")." at message number ".$_GET['id']."</p>\n");
  17.         fwrite($log,"<a href=\"gmail.php?num=".$next."&my_addr=".$_GET['addr']."&restart=1&start=1&max_num=".$_GET['max_num']."&avg_time=$avg_time\">Restart</a></p>\n");
  18.         fclose($log);
  19. }
  20. if (!$_GET['num'] && $_GET['start']==1){
  21.         $time=intval($_GET['max_num']*$refresh+$_GET['max_num']*$avg_time);
  22.         $jour=intval(date("j",$time))-1;
  23.         $date=$jour." days ".date("H",$time)." hours ".date("i",$time)." minutes";
  24.         $log=fopen("gmail_log.html","w+");
  25.         fwrite($log,"<p>Started on ".date("d/m/Y @ H:i")."<br />$max_num messages to send, which should take about $date.</p>\n");
  26.         fclose($log);
  27. }
  28. if ($_GET['restart']==1){
  29.         $left=$_GET['max_num']-$_GET['num'];
  30.         $time=intval(($left $refresh) + ($left $avg_time));
  31.         $jour=intval(date("j",$time))-1;
  32.         $date=$jour." days ".date("H",$time)." hours ".date("i",$time)." minutes";
  33.         $log=fopen("gmail_log.html","a+");
  34.         fwrite($log,"<p>Restarted on ".date("d/m/Y @ H:i")."<br />$left messages left to send, which should take about $date.</p>\n");
  35.         fclose($log);
  36. }
  37. if ($_GET['num'] && $_GET['num']==$_GET['max_num']){
  38.         $log=fopen("gmail_log.html","a+");
  39.         fwrite($log,"<p>Finished on ".date("d/m/Y @ H:i")."</p>\n");
  40.         fclose($log);
  41.         echo "<p>$j messages sent.<br /><a href='gmail_log.html'>View Log</a></p>";
  42. }
  43. if ($_POST['action']=="go" && !$_GET['max_num']){
  44.   $my_addr=$_REQUEST['my_addr'];
  45.   if (eregi("@gmail.com",$my_addr)){
  46.      $nom_fichier $_FILES['fichier']['tmp_name'];
  47.       $nom_destination 'mailbox';
  48.       move_uploaded_file($nom_fichier$nom_destination);
  49.       //echo $_FILES['fichier']['error'];
  50.       $mail=fopen("mailbox","r");
  51.       $max_num=0;
  52.       while (!feof($mail)) {
  53.         $line.=fread($mail,4096);
  54.       }
  55.     fclose($mail);
  56.     $emails=explode("\nFrom - ",$line);
  57.     $i=0;
  58.     if (!is_dir("tmp_mail")) mkdir("tmp_mail",0755);
  59.     foreach($emails as $email) {
  60.         $max_num++;
  61.         if ($i!=0$email="\nFrom - ".$email;
  62.         $message_file=fopen("tmp_mail/message$i.txt","w+");
  63.         fwrite($message_file,$email);
  64.         fclose($message_file);
  65.         $mess=fopen("tmp_mail/message$i.txt","r");
  66.         $headers='';
  67.         $text='';
  68.         $in_header="yes";
  69.         $line_num=0;
  70.         while (!feof($mess)) {
  71.           $line=fgets($mess);
  72.           if (!eregi("[a-z0-9_-]",$line) && $line_num>0){
  73.             $in_header="no";
  74.           }
  75.           if ($in_header=="yes"){
  76.              $headers.=eregi_replace("^[ \n\t\r].*","",$line);
  77.           }else{
  78.              $text.=$line;
  79.           }
  80.         $line_num++;
  81.         }
  82.         fclose($mess);
  83.         $headers=trim(eregi_replace("\nTo: ([^\n]*)","",$headers));
  84.         ereg("\nSubject: ([^\n]*)",$headers,$sub);
  85.         $subject=trim($sub[1]);
  86.         $headers=eregi_replace("\nReceived: [^\n]*","",$headers);
  87.         $headers=eregi_replace("\ncc: [^\n]*","",$headers);
  88.         $headers=eregi_replace("\nbcc: [^\n]*","",$headers);
  89.         $headers=eregi_replace("\nSubject: [^\n]*","",$headers);
  90.         $sending=fopen("tmp_mail/message$i.txt","w");
  91.         fwrite($sending,$subject."\n{[]}\n".$headers."\n{[]}\n".rtrim($text));
  92.         //fwrite($sending,$headers);
  93.         fclose($sending);
  94.         $to=$my_addr;
  95.         $i++;
  96.     }
  97.     ?>
  98.     <html>
  99.     <head>
  100.     <?php
  101.     echo "<meta http-equiv=\"Refresh\" content=\"$refresh;url=http://jfoucher/gmail.php?start=1&max_num=$max_num&my_addr=$to&num=0\">";
  102.     echo "</head><body>";
  103.     if(!unlink("mailbox"))
  104.     {
  105.     echo "Could not remove temporary mailbox file";
  106.     }else{
  107.     echo "Temporary mailbox file removed";
  108.     }
  109.     //header("location: http://jfoucher/gmail.php?start=1&max_num=$max_num&my_addr=$to&num=0");
  110.     echo "</body></html>";
  111.   }else{
  112.   echo "Wrong address";
  113.   }
  114. }
  115. if ($_GET['start']==1){
  116.       $i=$_GET['num'];
  117.       if($mess=fopen("tmp_mail/message$i.txt","r")){
  118.         $buffer='';
  119.         while (!feof($mess)){
  120.           $buffer.=fread($mess,4096);
  121.         }
  122.         fclose($mess);
  123.         list($subject,$headers,$message)=explode("\n{[]}\n",$buffer);
  124.         $my_headers="User-Agent: GMail Uploader 0.2";
  125.         ereg("(\nFrom: [^\n]*)",$headers,$head);
  126.         $my_headers.=$head[1];
  127.         ereg("(\nMIME-Version: [^\n]*)",$headers,$head);
  128.         $my_headers.=$head[1];
  129.         ereg("(\nMailing-List: [^\n]*)",$headers,$head);
  130.         $my_headers.=$head[1];
  131.         ereg("(\nPrecedence: [^\n]*)",$headers,$head);
  132.         $my_headers.=$head[1];
  133.         ereg("(\nList-Unsubscribe: [^\n]*)",$headers,$head);
  134.         $my_headers.=$head[1];
  135.         ereg("(\nDate: [^\n]*)",$headers,$head);
  136.         $my_headers.=$head[1];
  137.         ereg("(\nReply-To: [^\n]*)",$headers,$head);
  138.         $my_headers.=$head[1];
  139.         ereg("(\nContent-Type: [^\n]*)",$headers,$head);
  140.         $my_headers.=$head[1];
  141.         ereg("(\nContent-Transfer-Encoding: [^\n]*)",$headers,$head);
  142.         $my_headers.=$head[1];
  143.         ereg("(\nMessage-ID: [^\n]*)",$headers,$head);
  144.         $my_headers.=$head[1];
  145.         ereg("(\nReferences: [^\n]*)",$headers,$head);
  146.         $my_headers.=$head[1];
  147.         ereg("(\nIn-Reply-To: [^\n]*)",$headers,$head);
  148.         $my_headers.=$head[1];
  149.         //echo $my_headers;
  150.         if (mail($_GET['my_addr'], $subject$message$my_headers)) {
  151.           unlink("tmp_mail/message$i.txt");
  152.           $time_spent=getmicrotime()-$time_before;
  153.           $avg_time=($_GET['avg_time']*$i $time_spent)/($i+1);
  154.           ?>
  155.           <html>
  156.           <head>
  157.           <?php
  158.           $max=$_GET['max_num']-1
  159.           if ($i<$max){
  160.             ?>
  161.             <meta http-equiv="Refresh" content="<?php echo $refresh?>;url=http://jfoucher/gmail.php?num=<?php echo ($i+1); ?>&my_addr=<?php echo $my_addr?>&max_num=<?php echo $_GET['max_num']; ?>&avg_time=<?php echo $avg_time?>&start=1">
  162.             <?php
  163.           }
  164.           ?>
  165.           </head>
  166.           <body>
  167.           <?php
  168.             echo "<p>Message <strong>$i</strong> sent to: $to Subject: $subject</p>
  169.             <p><a href=\"gmail.php?stop=ok&id=$i&I_Love8Maria=true&addr=$my_addr&max_num=".$_GET['max_num']."&avg_time=$avg_time\">Stop !</a><br />
  170.             <a href=\"gmail_log.html\" target=\"_blank\">View log</a></p>";
  171.           $left=$_GET['max_num']-$_GET['num'];
  172.           $time=intval(($left $refresh) + ($left $avg_time));
  173.           $jour=intval(date("j",$time))-1;
  174.           $heure=intval(date("H",$time))-1;
  175.           $date=$jour." days ".$heure." hours ".date("i",$time)." minutes";
  176.           echo  "<p>".$left." messages and ".$date." to go ($time seconds).</p>";
  177.           $log=fopen("gmail_log.html","a+");
  178.           fwrite($log,"<p>Message <strong>$i</strong> sent in $time_spent seconds ($subject)</p>\n");
  179.           fclose($log);
  180.         }else{
  181.           echo "<html><body>";
  182.           $log=fopen("gmail_log.html","a+");
  183.           fwrite($log,"<p style='color:#c00;font-size:1.3em;'>Mail <strong>$i</strong> failed</p>\n");
  184.           fclose($log);
  185.           echo "<p style='color:#c00;font-size:1.3em;'>Message <strong>$i</strong> failed ($subject)</p>
  186.           <p><a href=\"gmail.php?stop=ok&id=$i&max_num=".$_GET['max_num']."&addr=$my_addr&avg_time=$avg_time\">Stop !</a><br />
  187.           <a href=\"gmail_log.html\" target=\"_blank\">View log</a></p>";
  188.         }
  189.         ?>
  190.         </body>
  191.         </html>
  192.         <?php
  193.        }else{
  194.        echo "Could not open temporary email file";
  195.        }
  196. }else{
  197. ?>
  198.   <html>
  199.   <body>
  200.   <form action="gmail.php" method="post" enctype="multipart/form-data">
  201.   <p>
  202.   <label for="email">Email</label>
  203.   <input type="text" value="@gmail.com" name="my_addr" id="my_addr" />
  204.   </p>
  205.   <p>
  206.   <label for="fichier">mbox file</label>
  207.   <input type="file" name="fichier" id="fichier" />
  208.   </p>
  209.   <input type="submit" name="action" id="action" value="go" />
  210.   </form>
  211.   <p><a href="gmail_log.html">View Log</a></p>
  212.   </body>
  213.   </html>
  214.   <?php
  215. }
  216. ?>