[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/phpmailer/ -> mailerc.php (source)

   1  <?php
   2  ////////////////////////////////////////////////////
   3  // mailerc - phpmailer client
   4  //
   5  // Version 0.07, Created 2001-01-03
   6  //
   7  // Client application for sending outgoing 
   8  // messages from a file. 
   9  //
  10  // Author: Brent R. Matzelle <bmatzelle@yahoo.com>
  11  //
  12  // License: LGPL, see LICENSE
  13  ////////////////////////////////////////////////////
  14  
  15  require ("class.phpmailer.php");
  16  
  17  // Gather global server arg variables
  18  if(!isset($_SERVER))
  19      $_SERVER = $HTTP_SERVER_VARS;
  20  $cargv = $_SERVER["argv"];
  21  $cargc = $_SERVER["argc"];
  22  
  23  define("PROG_VERSION", "0.01");
  24  define("PROG_NAME", $cargv[0]);
  25  @set_time_limit(0); // unlimited
  26  
  27  // Ignore warning messages
  28  error_reporting(E_ERROR);
  29  
  30  /**
  31   * mailerc - mailerc extension class
  32   * @author Brent R. Matzelle
  33   */
  34  class mailerc extends phpmailer
  35  {
  36      /**
  37       * Send an email from a file created with the
  38       * SendToQueue() function.
  39       * @public
  40       * @returns bool
  41       */
  42      function SendFromFile($filePath) {
  43          $qarray = array();
  44          $to_list = array();
  45          $header = "";
  46          $body = "";
  47  
  48          // Make sure is there and accessible
  49          if(!is_file($filePath))
  50          {
  51              $this->error_handler(sprintf("Cannot access: %s", $filePath));
  52              return false;
  53          }
  54          
  55          // upon getting header do not forget to gather the 
  56          // server info (date, recieved())
  57          $qarray = file($filePath);
  58          
  59          if(count($qarray) < 1)
  60          {
  61              $this->error_handler("Invalid queue file");
  62              return false;
  63          }
  64  
  65          // Create the header and the body (just set header)
  66          $header = $this->received();
  67          $header .= sprintf("Date: %s\r\n", $this->rfc_date());
  68          
  69          $msg_start = 0;
  70          for($i = 0; $i < count($qarray); $i++)
  71          {
  72              if($qarray[$i] == "----END PQM HEADER----\r\n")
  73              {
  74                  $msg_start = $i + 1;
  75                  break;
  76              }
  77          }
  78          
  79          for($i = $msg_start; $i < count($qarray); $i++)
  80              $body .= $qarray[$i];
  81  
  82          $this->Mailer = $this->qvar($qarray, "Mailer");
  83          if($this->Mailer == "sendmail")
  84          {
  85              $this->Sendmail = $this->qvar($qarray, "Sendmail");
  86              $this->Sender   = $this->qvar($qarray, "Sender");
  87  
  88              if(!$this->sendmail_send($header, $body))
  89                  return false;
  90          }
  91          elseif($this->Mailer == "mail")
  92          {
  93              $this->Sender  = $this->qvar($qarray, "Sender");
  94              $this->Subject = $this->qvar($qarray, "Subject");
  95  
  96              $to_list = explode(";", $this->qvar($qarray, "to"));
  97              for($i = 0; $i < count($to_list); $i++)
  98                  $this->AddAddress($to_list[0], "");
  99  
 100              // This might not work because of not sending 
 101              // both a header and body.
 102              if(!$this->mail_send($header, $body))
 103                  return false;
 104          }
 105          elseif($this->Mailer == "smtp")
 106          {
 107              $this->Host     = $this->qvar($qarray, "Host");
 108              $this->Port     = $this->qvar($qarray, "Port");
 109              $this->Helo     = $this->qvar($qarray, "Helo");
 110              $this->Timeout  = $this->qvar($qarray, "Timeout");
 111              $this->SMTPAuth = (int)$this->qvar($qarray, "SMTPAuth");
 112              $this->Username = $this->qvar($qarray, "Username");
 113              $this->Password = $this->qvar($qarray, "Password");
 114              $this->From     = $this->qvar($qarray, "From");
 115  
 116              $to_addr = $this->qvar($qarray, "to");
 117              if(!empty($to_addr))
 118              {
 119                  $to_list = explode(";", $to_addr);
 120                  for($i = 0; $i < count($to_list); $i++)
 121                      $this->AddAddress($to_list[0], "");
 122              }
 123  
 124              $to_addr = $this->qvar($qarray, "cc");
 125              if(!empty($to_addr))
 126              {
 127                  $to_list = explode(";", $to_addr);
 128                  for($i = 0; $i < count($to_list); $i++)
 129                      $this->AddCC($to_list[0], "");
 130              }
 131  
 132              $to_addr = $this->qvar($qarray, "bcc");
 133              if(!empty($to_addr))
 134              {
 135                  $to_list = explode(";", $to_addr);
 136                  for($i = 0; $i < count($to_list); $i++)
 137                      $this->AddBCC($to_list[0], "");
 138              }
 139  
 140              if(!$this->smtp_send($header, $body))
 141                  return false;
 142          }
 143          else
 144          {
 145              $this->error_handler(sprintf("%s mailer is not supported", $this->Mailer));
 146              return false;
 147          }
 148          
 149          return true;
 150      }
 151      
 152      /**
 153       * Return the given queue variable from the pqm header file. Returns 
 154       * an empty string if the data was not found.
 155       * @private
 156       * @return string
 157       */
 158      function qvar($qarray, $data) {
 159          $i = 0;
 160          $pqm_marker = "----END PQM HEADER----\n";
 161  
 162          while($qarray[$i] != $pqm_marker)
 163          {
 164              $item = explode(": ", $qarray[$i]);
 165              //echo $item[0] . "\n"; // debug
 166              if($item[0] == $data)
 167                  return rtrim($item[1]);
 168              $i++;
 169          }
 170  
 171          return ""; // failure
 172      }
 173  }
 174  
 175  /**
 176   * Print out the program version information.
 177   * @private
 178   * @returns void
 179   */
 180  function print_version()
 181  {
 182     printf("mailerc %s - phpmailer client\n\n" .
 183       "This program is distributed in the hope that it will be useful,\n" .
 184       "but WITHOUT ANY WARRANTY; without even the implied warranty of \n" .
 185       "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the \n" .
 186       "GNU Lesser General Public License for more details.\n\n" .
 187       "Written by: Brent R. Matzelle\n", PROG_VERSION);
 188  }
 189  
 190  /*
 191    Print out the help message to the console.
 192    @private
 193    @returns void
 194   */
 195  function print_help()
 196  {
 197    printf("mailerc %s, phpmailer queue daemon.\n", PROG_VERSION);
 198    printf("Usage: %s [OPTION] [FILE]\n", PROG_NAME);
 199    printf("
 200  Options:
 201    -h,  --help                            print this help.
 202    -V,  --version                         print version information.
 203    -s,  --send                            send [FILE]\n");
 204  }
 205  
 206  /**
 207   * Sends a given message from a pqm (Phpmailer Queue Message) file.
 208   * @private
 209   * @returns bool
 210   */
 211  function send_message($filePath)
 212  {
 213      // Open the file and read the header contents and set 
 214      // another message.  Then run the phpmailer send file.
 215      $mail = new mailerc();
 216      if(!$mail->SendFromFile($filePath))
 217          printf("error: %s\n", $mail->ErrorInfo);
 218      else
 219          printf("success: sent\n");
 220  }
 221  
 222  /*
 223    Pseudo main()
 224   */
 225  if($cargc < 1)
 226  {
 227      print_version();
 228      exit;
 229  }
 230  
 231  switch($cargv[1])
 232  {
 233      case "-h":
 234      case "--help":
 235          print_help();
 236          break;
 237      case "-V":
 238      case "--version":
 239          print_version();
 240          break;
 241      case "-s":
 242      case "--send":
 243          send_message($cargv[2]);
 244          break;
 245      default:
 246          print_help();
 247  }
 248  
 249  return 0;  // return success
 250  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7