[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/classes/ -> libmail.class.php (source)

   1  <?php /* $Id: libmail.class.php 77 2008-03-05 11:46:53Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/classes/libmail.class.php $ */
   2  /**

   3   *    @package web2project

   4   *    @subpackage mail

   5   */
   6  
   7  if (!defined('W2P_BASE_DIR')) {
   8      die('You should not access this file directly.');
   9  }
  10  
  11  /**

  12   *    This class encapsulates the PHP mail() function.

  13   *

  14   *    @version    1.0

  15   *    Example

  16   *    <code>

  17   *    include "libmail.php";

  18   *

  19   *    $m = new Mail; // create the mail

  20   *    $m->From( "leo@isp.com" );

  21   *    $m->To( "destination@somewhere.fr" );

  22   *    $m->Subject( "the subject of the mail" );

  23   *

  24   *    $message= "Hello world!\nthis is a test of the Mail class\nplease ignore\nThanks.";

  25   *    $m->Body( $message);    // set the body

  26   *    $m->Cc( "someone@somewhere.fr");

  27   *    $m->Bcc( "someoneelse@somewhere.fr");

  28   *    $m->Priority(4) ;    // set the priority to Low

  29   *    $m->Attach( "/home/leo/toto.gif", "image/gif" ) ;    // attach a file of type image/gif

  30   *    $m->Send();    // send the mail

  31   *    echo "the mail below has been sent:<br><pre>", $m->Get(), "</pre>";

  32   *    </code>

  33   *    @author    Leo West - lwest@free.fr

  34   *    @author    Emiliano Gabrielli - emiliano.gabrielli@dearchitettura.com

  35   *    @author    Pedro Azevedo - pedroa@web2project.net

  36   */
  37  
  38  require_once($AppUI->getLibraryClass('PHPMailer/class.phpmailer'));
  39  
  40  class Mail extends PHPMailer {
  41      /**

  42       *    list of To addresses

  43       *    @var    array

  44       */
  45      var $ato = array();
  46      /**

  47       *    @var    array

  48       */
  49      var $acc = array();
  50      /**

  51       *    @var    array

  52       */
  53      var $abcc = array();
  54      /**

  55       *    paths of attached files

  56       *    @var array

  57       */
  58  
  59      /**

  60       *    character set of message

  61       *    @var string

  62       */
  63      var $receipt = false;
  64      var $useRawAddress = true;
  65      var $defer;
  66  
  67      /**

  68       *    Mail constructor

  69       */
  70  	function Mail() {
  71          $this->autoCheck(true);
  72          $this->defer = w2PgetConfig('mail_defer');
  73          $this->canEncode = function_exists('imap_8bit') && 'us-ascii' != $this->charset;
  74          $this->hasMbStr = function_exists('mb_substr');
  75  
  76          $this->Mailer = (w2PgetConfig('mail_transport', 'php') == 'smtp' ? 'smtp' : 'mail');
  77          $this->Port = w2PgetConfig('mail_port', '25');
  78          $this->Host = w2PgetConfig('mail_host', 'localhost');
  79          $this->Hostname = w2PgetConfig('mail_host', 'localhost');
  80          $this->SMTPAuth = w2PgetConfig('mail_auth', false);
  81          $this->SMTPSecure = w2PgetConfig('mail_secure', '');
  82          $this->SMTPDebug = w2PgetConfig('mail_debug', false);
  83          $this->Username = w2PgetConfig('mail_user');
  84          $this->Password = w2PgetConfig('mail_pass');
  85          $this->Timeout = w2PgetConfig('mail_timeout', 0);
  86          $this->CharSet = isset($GLOBALS['locale_char_set']) ? w2PcheckCharset(strtolower($GLOBALS['locale_char_set'])) : 'us-ascii';
  87          $this->Encoding = $this->Charset != 'us-ascii' ? '8bit' : '7bit';
  88          //The from clause is fixed for all emails so that the users do not reply to one another

  89          $this->From(w2PgetConfig('admin_username') . '@' . w2PgetConfig('site_domain'), w2PgetConfig('company_name'));
  90      }
  91  
  92      /**

  93       *    activate or desactivate the email addresses validator

  94       *

  95       *    ex: autoCheck( TRUE ) turn the validator on

  96       *    by default autoCheck feature is on

  97       *

  98       *    @param boolean    $bool set to TRUE to turn on the auto validation

  99       *    @access public

 100       */
 101  	function autoCheck($bool) {
 102          $this->checkAddress = (bool)$bool;
 103          return true;
 104      }
 105  
 106      /**

 107       *    Define the subject line of the email

 108       *    @param string $subject any monoline string

 109       */
 110  	function Subject($subject, $charset = '') {
 111          $this->Subject = w2PgetConfig('email_prefix') . ' ' . $subject;
 112          return true;
 113      }
 114  
 115      /**

 116       *    set the sender of the mail

 117       *    @param string $from should be an email address

 118       */
 119  	function From($from, $fromname = '') {
 120          if (!is_string($from)) {
 121              return false;
 122          }
 123          $this->From = $from;
 124          $this->FromName = $fromname;
 125          if ($this->receipt) {
 126              $this->ConfirmReadingTo($from);
 127          }
 128          return true;
 129      }
 130  
 131      /**

 132       *    set the Reply-to header

 133       *    @param string $email should be an email address

 134       */
 135  	function ReplyTo($address) {
 136          if (!is_string($address)) {
 137              return false;
 138          }
 139          $this->AddReplyTo($address);
 140          if ($this->receipt) {
 141              $this->ConfirmReadingTo($address);
 142          }
 143          return true;
 144      }
 145  
 146      /**

 147       *    add a receipt to the mail ie.  a confirmation is returned to the "From" address (or "ReplyTo" if defined)

 148       *    when the receiver opens the message.

 149       *    @warning this functionality is *not* a standard, thus only some mail clients are compliants.

 150       */
 151  	function Receipt() {
 152          $this->receipt = true;
 153          return true;
 154      }
 155  
 156      /**

 157       *    set the mail recipient

 158       *

 159       *    The optional reset parameter is useful when looping through records to send individual mails.

 160       *    This prevents the 'to' array being continually stacked with additional addresses.

 161       *

 162       *    @param string $to email address, accept both a single address or an array of addresses

 163       *    @param boolean $reset resets the current array

 164       */
 165      function To($to, $reset = false) {
 166          if (is_array($to)) {
 167              $this->ato = $to;
 168          } else {
 169              if ($this->useRawAddress) {
 170                  if (preg_match("/^(.*)\<(.+)\>$/D", $to, $regs)) {
 171                      $to = $regs[2];
 172                  }
 173              }
 174              if ($reset) {
 175                  unset($this->ato);
 176                  $this->ato = array();
 177              }
 178              $this->ato[] = $to;
 179          }
 180  
 181          if ($this->checkAddress == true) {
 182              $this->CheckAdresses($this->ato);
 183          }
 184          
 185          foreach ($this->ato as $to_address) {
 186              if (strpos($to_address, '<') !== false) {
 187                  preg_match('/^.*<([^@]+\@[a-z0-9\._-]+)>/i', $to_address, $matches);
 188                  if (isset($matches[1])) {
 189                      $to_address = $matches[1];
 190                  }
 191              }
 192              $this->AddAddress($to_address);
 193          }        
 194          return true;
 195      }
 196  
 197      /**

 198       *    Cc()

 199       *    set the CC headers ( carbon copy )

 200       *    $cc : email address(es), accept both array and string

 201       */
 202      function Cc($cc) {
 203          if (is_array($cc)) {
 204              $this->acc = $cc;
 205          } else {
 206              $this->acc = explode(',', $cc);
 207          }
 208  
 209          if ($this->checkAddress == true) {
 210              $this->CheckAdresses($this->acc);
 211          }
 212  
 213          foreach ($this->acc as $cc_address) {
 214              if (strpos($cc_address, '<') !== false) {
 215                  preg_match('/^.*<([^@]+\@[a-z0-9\._-]+)>/i', $cc_address, $matches);
 216                  if (isset($matches[1])) {
 217                      $cc_address = $matches[1];
 218                  }
 219              }
 220              $this->AddCC($cc_address);
 221          }        
 222          
 223          return true;
 224      }
 225  
 226      /**

 227       *    set the Bcc headers ( blank carbon copy ).

 228       *    $bcc : email address(es), accept both array and string

 229       */
 230  	function Bcc($bcc) {
 231          if (is_array($bcc)) {
 232              $this->abcc = $bcc;
 233          } else {
 234              $this->abcc = explode(',', $bcc);
 235          }
 236  
 237          if ($this->checkAddress == true) {
 238              $this->CheckAdresses($this->abcc);
 239          }
 240  
 241          foreach ($this->abcc as $bcc_address) {
 242              if (strpos($bcc_address, '<') !== false) {
 243                  preg_match('/^.*<([^@]+\@[a-z0-9\._-]+)>/i', $bcc_address, $matches);
 244                  if (isset($matches[1])) {
 245                      $bcc_address = $matches[1];
 246                  }
 247              }
 248              $this->AddCC($bcc_address);
 249          }        
 250          
 251          return true;
 252      }
 253  
 254      /**

 255       *        set the body (message) of the mail

 256       *        define the charset if the message contains extended characters (accents)

 257       *        default to us-ascii

 258       *        $mail->Body( "m?l en fran?ais avec des accents", "iso-8859-1" );

 259       */
 260  	function Body($body, $charset = '') {
 261          $this->Body = w2PHTMLDecode($body);
 262  
 263          if (!empty($charset)) {
 264              @($this->charset = strtolower($charset));
 265              if ($this->charset != 'us-ascii') {
 266                  $this->Encoding = '8bit';
 267              }
 268          }
 269      }
 270  
 271      /**

 272       *        set the mail priority

 273       *        $priority : integer taken between 1 (highest) and 5 ( lowest )

 274       *        ex: $mail->Priority(1) ; => Highest

 275       */
 276  	function Priority($priority) {
 277          if ((!intval($priority)) || (intval($priority) < 1) || (intval($priority) > 5)) {
 278              return false;
 279          }
 280  
 281          $this->Priority = $priority;
 282          return true;
 283      }
 284  
 285  
 286      /**

 287       *    Overload the Send method from PHPMailer to provide defered mails

 288       *    @access public

 289       */
 290  	function Send() {
 291          if ($this->defer) {
 292              return $this->QueueMail();
 293          } else {
 294              return PHPMailer::Send();
 295          }
 296      }
 297  
 298  	function getHostName() {
 299          // Grab the server address, return a hostname for it.

 300          if ($host = gethostbyaddr($_SERVER['SERVER_ADDR'])) {
 301              return $host;
 302          } else {
 303              return '[' . $_SERVER['SERVER_ADDR'] . ']';
 304          }
 305      }
 306  
 307      /**

 308       * Queue mail to allow the queue manager to trigger

 309       * the email transfer.

 310       *

 311       * @access private

 312       */
 313  	function QueueMail() {
 314          global $AppUI;
 315  
 316          require_once $AppUI->getSystemClass('event_queue');
 317          $ec = new EventQueue;
 318          $vars = get_object_vars($this);
 319          return $ec->add(array('Mail', 'SendQueuedMail'), $vars, 'libmail', true);
 320      }
 321  
 322      /**

 323       * Dequeue the email and transfer it.  Called from the queue manager.

 324       *

 325       * @access private

 326       */
 327  	function SendQueuedMail($mod, $type, $originator, $owner, &$args) {
 328          extract($args);
 329          if ($this->transport == 'smtp') {
 330              $this->IsSMTP();
 331              return $this->Send();
 332          } else {
 333              $this->IsMail();
 334              return $this->Send();
 335          }
 336      }
 337  
 338      /**

 339       *    Returns the whole e-mail , headers + message

 340       *

 341       *    can be used for displaying the message in plain text or logging it

 342       *

 343       *    @return string

 344       */
 345  	function Get() {
 346          $mail = $this->CreateHeader();
 347          $mail .= $this->CreateBody();
 348          return $mail;
 349      }
 350  
 351      /**

 352       *    check an email address validity

 353       *    @access public

 354       *    @param string $address : email address to check

 355       *    @return TRUE if email adress is ok

 356       */
 357  	function ValidEmail($address) {
 358          if (preg_match('/^(.*)\<(.+)\>$/D', $address, $regs)) {
 359              $address = $regs[2];
 360          }
 361          return (bool)preg_match('/^[^@ ]+@([-a-zA-Z0-9..]+)$/D', $address);
 362      }
 363  
 364      /**

 365       *    check validity of email addresses

 366       *    @param    array $aad -

 367       *    @return if unvalid, output an error message and exit, this may -should- be customized

 368       */
 369  
 370  	function CheckAdresses($aad) {
 371          foreach ($aad as $ad) {
 372              if (!$this->ValidEmail($ad)) {
 373                  echo 'Class Mail, method Mail : invalid address ' . $ad;
 374                  exit;
 375              }
 376          }
 377          return true;
 378      }
 379      /**

 380       * alias for the mispelled CheckAdresses

 381       */
 382  	function CheckAddresses($aad) {
 383          return $this->CheckAdresses($aad);
 384      }
 385  
 386  
 387  } // class Mail

 388  ?>


Generated: Sat Jul 17 03:00:04 2010 Cross-referenced by PHPXref 0.7