[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/mail/src/ -> lmbMailer.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  @define('PHPMAILER_DIR', dirname(__FILE__) . '/../lib/phpmailer-1.72/');
  10  @define('LIMB_USE_PHPMAIL', false);
  11  @define('LIMB_SMTP_PORT', '25');
  12  @define('LIMB_SMTP_HOST', 'localhost');
  13  @define('LIMB_SMTP_AUTH', false);
  14  @define('LIMB_SMTP_USER', '');
  15  @define('LIMB_SMTP_PASSWORD', '');
  16  
  17  /**

  18   * class lmbMailer.

  19   *

  20   * @package mail

  21   * @version $Id: lmbMailer.class.php 5945 2007-06-06 08:31:43Z pachanga $

  22   */
  23  class lmbMailer
  24  {
  25    protected $attachments = array();
  26  
  27    protected function _createMailer()
  28    {
  29      include_once(PHPMAILER_DIR . '/class.phpmailer.php');
  30  
  31      $mailer = new PHPMailer();
  32      $mailer->LE = "\r\n";
  33  
  34      if(LIMB_USE_PHPMAIL)
  35        return $mailer;
  36  
  37      $mailer->IsSMTP();
  38      $mailer->Host = LIMB_SMTP_HOST;
  39      $mailer->Port = LIMB_SMTP_PORT;
  40  
  41      if(LIMB_SMTP_AUTH == true)
  42      {
  43        $mailer->SMTPAuth = true;
  44        $mailer->Username = LIMB_SMTP_USER;
  45        $mailer->Password = LIMB_SMTP_PASSWORD;
  46      }
  47      return $mailer;
  48    }
  49  
  50    function addAttachment($path, $name = "", $encoding = "base64", $type = "application/octet-stream")
  51    {
  52      $this->attachments[] = array(
  53        'path' => $path,
  54        'name' => $name,
  55        'encoding' => $encoding,
  56        'type' => $type
  57      );
  58    }
  59  
  60    function sendPlainMail($recipients, $sender, $subject, $body, $charset = 'utf-8')
  61    {
  62      $mailer = $this->_createMailer();
  63  
  64      $mailer->IsHTML(false);
  65      $mailer->CharSet = $charset;
  66  
  67      if(!empty($this->attachments))
  68        $this->_addAttachments($mailer);
  69  
  70      $recipients = $this->processMailRecipients($recipients);
  71  
  72      foreach($recipients as $recipient)
  73        $mailer->AddAddress($recipient['address'], $recipient['name']);
  74  
  75      if(!$sender = $this->processMailAddressee($sender))
  76        return false;
  77  
  78      $mailer->From = $sender['address'];
  79      $mailer->FromName = $sender['name'];
  80      $mailer->Subject = $subject;
  81      $mailer->Body    = $body;
  82  
  83      return $mailer->Send();
  84    }
  85  
  86    function sendHtmlMail($recipients, $sender, $subject, $html, $text = null, $charset = 'utf-8')
  87    {
  88      $mailer = $this->_createMailer();
  89  
  90      $mailer->IsHTML(true);
  91      $mailer->CharSet = $charset;
  92  
  93      $mailer->Body = $html;
  94  
  95      if(!empty($this->attachments))
  96        $this->_addAttachments($mailer);
  97  
  98      if(!is_null($text))
  99        $mailer->AltBody = $text;
 100  
 101      $recipients = $this->processMailRecipients($recipients);
 102  
 103      foreach($recipients as $recipient)
 104        $mailer->AddAddress($recipient['address'], $recipient['name']);
 105  
 106      if(!$sender = $this->processMailAddressee($sender))
 107        return false;
 108  
 109      $mailer->From = $sender['address'];
 110      $mailer->FromName = $sender['name'];
 111      $mailer->Subject = $subject;
 112  
 113      return $mailer->Send();
 114    }
 115  
 116    function processMailRecipients($recipients)
 117    {
 118      if(!is_array($recipients))
 119         $recipients = array($recipients);
 120      $result = array();
 121      foreach($recipients as $recipient)
 122      {
 123        if($recipient = $this->processMailAddressee($recipient))
 124          $result[] = $recipient;
 125      }
 126  
 127      return $result;
 128    }
 129  
 130    function processMailAddressee($adressee)
 131    {
 132      if(is_array($adressee))
 133      {
 134        if(isset($adressee['address']) && isset($adressee['name']))
 135          return $adressee;
 136  
 137        return null;
 138      }
 139      elseif(preg_match('~("|\')?([^"\']+)("|\')?\s+<([^>]+)>~', $adressee, $matches))
 140        return array('address' => $matches[4], 'name' => $matches[2]);
 141      else
 142        return array('address' => $adressee, 'name' => '');
 143    }
 144  
 145    protected function _addAttachments($mailer)
 146    {
 147      foreach($this->attachments as $attachment)
 148        $mailer->AddAttachment($attachment['path'],
 149                               $attachment['name'],
 150                               $attachment['encoding'],
 151                               $attachment['type']);
 152    }
 153  }
 154  
 155  ?>


Generated: Sun Oct 12 04:41:30 2008 Cross-referenced by PHPXref 0.7