Location: PHPKode > projects > RepMan > repman/inc/mail.php
<?

  $date=date("Ymd");
  
  $errc = 0;
  $errd = array ();
  $nac = 0;
  $nad = array ();
  $goodc = 0;
  $goodd = array ();
  if ($gdata=Query("SELECT id,status FROM sess WHERE processed='$date';")) {
    foreach ($gdata as $ses) {
    	switch ($ses[1]) {
     	  case '-1':
          $nad[$nac++]=$ses[0];
     	    break;
     	  case '0':
     	    $goodd[$goodc++]=$ses[0];
     	    break;     	  
        default:
          $errd[$errc++]=$ses[0];
      }
    }
  } else {
    echo "<p>No processed session.</p>\n";
  }
  
  // ****** e-mail pro adminy
  
  // komu
  $to = $GLOBALS["notify_email"];   // na tento e-mail posleme vsechno
  
  // uvedeni predmetu
  $subj = 'Status:';

                  // ********************** zacatek tela e-mailu ************************
  $body = "   
    <html>
      <style type=\"text/css\">
      <!--
        h1 {
          font-family: 'MS Sans Serif', 'Sans';
          font-size: 18pt;
          font-weight: bold;
          margin-bottom: 20px;
        }
        h2 {
          font-family: 'MS Sans Serif', 'Sans';
          font-size: 12pt;
          font-weight: bold;
          line-height: 20px;
        }
        body {
          font-family: 'MS Sans Serif', 'Sans';
          font-size: 10pt;
          font-weight: normal;
        }
      //-->
      </style>    
    <body>
      <h1>RepMan summary - $date</h1>\n";

  /* tvorba informaci
     do Predmetu (subject)
     a Obsahu e-mailu (body) */

  if ($errc) {    // Errory - dokoncene ale chybove
    $subj .= " ERROR(".$errc."x)";
    $body .= "<font color=\"#CC0000\"><h2>Error</h2></font>\n";
    foreach ($errd as $rd) {
      if ($whe=Query("SELECT client.name,job.job,status FROM sess LEFT JOIN (client,job) ON (client.id=sess.client_id AND job.id=sess.job_id) WHERE sess.id=$rd;")) {
        foreach ($whe as $kdoe) {
    	    $body .= "&nbsp;&nbsp;".$kdoe[0]."&nbsp;&nbsp;".$kdoe[1]."&nbsp;&nbsp;<font color=\"#CC0000\">#".$kdoe[2]."</font><br>\n";
    	  }
    	} else { logerr("nemuzu selektovat klienty a joby z DB!!");}
    }
  }

  if ($nac) {     // N-A (not available) - neprovedene
    $subj .= " N-A(".$nac."x)";
    $body .= "<font color=\"#FF9900\"><h2>N/A - Not Available</h2></font>\n";
    foreach ($nad as $nd) {
      if ($whn=Query("SELECT client.name,job.job FROM sess LEFT JOIN (client,job) ON (client.id=sess.client_id AND job.id=sess.job_id) WHERE sess.id=$nd;")) {
        foreach ($whn as $kdon) {
    	    $body .= "&nbsp;&nbsp;".$kdon[0]."&nbsp;&nbsp;".$kdon[1]."<br>\n";
    	  }
    	} else { logerr("nemuzu selektovat klienty a joby z DB!!");}
    }
  }

  if ($goodc) {   // OK - provedene a uspesne
    $subj .= " OK(".$goodc."x)";
    $body .= "<font color=\"#00CC33\"><h2>OK</h2></font>\n";
    foreach ($goodd as $gd) {
      if ($whg=Query("SELECT client.name,job.job FROM sess LEFT JOIN (client,job) ON (client.id=sess.client_id AND job.id=sess.job_id) WHERE sess.id=$gd;")) {
        foreach ($whg as $kdog) {
    	    $body .= "&nbsp;&nbsp;".$kdog[0]."&nbsp;&nbsp;".$kdog[1]."<br>\n";
    	  }
    	} else { logerr("nemuzu selektovat klienty a joby z DB!!");}
    }
  }
  
  // == odkaz do prohlizece
  $body .= "<br><br><small><a href=\"".$GLOBALS["http_url"]."index.php?group=$date\" title=\"Open this report in web browser\">".$GLOBALS["http_url"]."index.php?group=$date</a></small>\n";

                  // ********************** konec tela e-mailu ************************
  $body .= "
    </body>
    </html>";
  
  
  // To send HTML mail, the Content-type header must be set
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
  $headers .= 'From: RepMan <hide@address.com>' . "\r\n";
  /*
  // Additional headers
  $headers .= 'To: Mary <hide@address.com>, Kelly <hide@address.com>' . "\r\n";
  $headers .= 'From: Birthday Reminder <hide@address.com>' . "\r\n";
  $headers .= 'Cc: hide@address.com' . "\r\n";
  $headers .= 'Bcc: hide@address.com' . "\r\n";
  */

  if (mail($to, $subj, $body, $headers)) {
    echo "<br><br><br><small>message sent to '".$GLOBALS["notify_email"]."'</small><br>\n";
  } else {
    echo "<br><br><br><small>message error</small><br>\n";
    logerr("Cann't send e-mail to '".$GLOBALS["notify_email"]."'");
  }
  
  
  // ********************************** MAILING LIST *************************************** 
  if ($mlistd = Query("SELECT user.name,user.email,user.sms FROM mailing LEFT JOIN(user) ON(user.id=mailing.us_id) GROUP BY(name);")) {
    
    foreach ($mlistd as $mlistl) {
      
      echo "<br><br><small>Message for '$mlistl[0]'/$mlistl[1]:<br>\n";

      // **start of e-mail**
      $to = $mlistl[1];  // komu
      $subj = '';      // predmet
                      // telo e-mailu
      $body = "          
        <html>
          <style type=\"text/css\">
          <!--
            h1 {
              font-family: 'MS Sans Serif', 'Sans';
              font-size: 18pt;
              font-weight: bold;
              margin-bottom: 20px;
            }
            h2 {
              font-family: 'MS Sans Serif', 'Sans';
              font-size: 14pt;
              font-weight: bold;
              /*margin-bottom: 12pt;*/
              line-height: 20px;
            }
            body {
              font-family: 'MS Sans Serif', 'Sans';
              font-size: 10pt;
              font-weight: normal;
            }
          //-->
          </style>    
        <body>
          <h1>RepMan summary - $date</h1>\n";
      
      // **errors**         
   	  if ($ssdata = Query("SELECT client.name,job.job,user.email,sess.status,mailing.cl_id,mailing.jo_id,user.name,sess.client_id,sess.job_id FROM sess LEFT JOIN (client,job) ON (client.id=sess.client_id AND job.id=sess.job_id),mailing LEFT JOIN(user) ON (user.id=mailing.us_id)  WHERE sess.processed='$date' AND ((sess.client_id=mailing.cl_id AND sess.job_id=mailing.jo_id) OR (mailing.cl_id=0 AND sess.job_id=mailing.jo_id) OR (sess.client_id=mailing.cl_id AND mailing.jo_id=0)) AND user.name='$mlistl[0]' AND sess.status>'0';")) {
        if ($mlistl[2] == "0") {
          $subj .= " ERROR(".count($ssdata)."x)";
        } else {
          $subj .= "ERR(".count($ssdata).")";
        }
        $body .= "<font color=\"#CC0000\"><h2>Error</h2></font>\n";
        foreach ($ssdata as $sslist) {
          $body .= "&nbsp;&nbsp;".$sslist[0]."&nbsp;&nbsp;".$sslist[1]."&nbsp;&nbsp;<font color=\"#CC0000\">#".$sslist[3]."</font><br>\n";
        }
      }  

      // **N/A-s**         
   	  if ($ssdata = Query("SELECT client.name,job.job,user.email,sess.status,mailing.cl_id,mailing.jo_id,user.name,sess.client_id,sess.job_id FROM sess LEFT JOIN (client,job) ON (client.id=sess.client_id AND job.id=sess.job_id),mailing LEFT JOIN(user) ON (user.id=mailing.us_id)  WHERE sess.processed='$date' AND ((sess.client_id=mailing.cl_id AND sess.job_id=mailing.jo_id) OR (mailing.cl_id=0 AND sess.job_id=mailing.jo_id) OR (sess.client_id=mailing.cl_id AND mailing.jo_id=0)) AND user.name='$mlistl[0]' AND sess.status='-1';")) {
        if ($mlistl[2] == "0") {
          $subj .= " N/A(".count($ssdata)."x)";
        } else {
          $subj .= "NA(".count($ssdata).")";
        }
        $body .= "<font color=\"#FF9900\"><h2>N/A - Not Available</h2></font>\n";

        foreach ($ssdata as $sslist) {
          $body .= "&nbsp;&nbsp;".$sslist[0]."&nbsp;&nbsp;".$sslist[1]."<br>\n";
        }
      }  

      // **OK-s**         
   	  if ($ssdata = Query("SELECT client.name,job.job,user.email,sess.status,mailing.cl_id,mailing.jo_id,user.name,sess.client_id,sess.job_id FROM sess LEFT JOIN (client,job) ON (client.id=sess.client_id AND job.id=sess.job_id),mailing LEFT JOIN(user) ON (user.id=mailing.us_id)  WHERE sess.processed='$date' AND ((sess.client_id=mailing.cl_id AND sess.job_id=mailing.jo_id) OR (mailing.cl_id=0 AND sess.job_id=mailing.jo_id) OR (sess.client_id=mailing.cl_id AND mailing.jo_id=0)) AND user.name='$mlistl[0]' AND sess.status='0';")) {
        if ($mlistl[2] == "0") {
          $subj .= " OK(".count($ssdata)."x)";
        } else {
          $subj .= "OK(".count($ssdata).")";
        }
        $body .= "<font color=\"#00CC33\"><h2>OK</h2></font>\n";
        foreach ($ssdata as $sslist) {
          $body .= "&nbsp;&nbsp;".$sslist[0]."&nbsp;&nbsp;".$sslist[1]."<br>\n";
        }
      }  

      // == odkaz do prohlizece
      $body .= "<br><br><small><a href=\"".$GLOBALS["http_url"]."index.php?group=$date&name=$mlistl[0]\" title=\"Open this report in web browser\">".$GLOBALS["http_url"]."index.php?group=$date&name=$mlistl[0]</a></small>\n";

      // ---------- konec tela e-mailu 
      $body .= "
        </body>
        </html>";

      // To send HTML mail, the Content-type header must be set
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
      $headers .= 'From: RepMan <hide@address.com>' . "\r\n";
      
      if ($mlistl[2] == "0") {
        // normalni HTML e-mail
        if (mail($to, 'Status: '.$subj, $body, $headers)) {
          echo "message sent to '".$mlistl[1]."'</small><br><br>\n";
        } else {
          echo "message error</small><br><br>\n";
          logerr("Cann't send e-mail to '".$mlistl[1]."'");
        }
      } else {
        // sms e-mail = obsahuje jenom predmet se stavem
        if (mail($to, $date." ".$subj, "", 'From: RepMan <hide@address.com>' . "\r\n")) {
          echo "message sent to '".$mlistl[1]." (as SMS)'</small><br><br>\n";
        } else {
          echo "message error</small><br><br>\n";
          logerr("Cann't send SMS e-mail to '".$mlistl[1]."'");
        }        
      }
    }
  } else {
    echo "No users in mailing list.";
    logdebug("No users in mailing list");
  }
?>
Return current item: RepMan