energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file Mail.class.php

Documentation is available at Mail.class.php

  1. <?php
  2.  
  3. /**
  4.  * Содержит класс Mail
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author dr.Pavka
  9.  * @copyright Energine 2007
  10.  * @version $Id$
  11.  */
  12.  
  13.  
  14. /**
  15.  * Отправщик сообщения
  16.  *
  17.  * @package energine
  18.  * @subpackage core
  19.  * @author dr.Pavka
  20.  * @final
  21.  */
  22. final class Mail extends Object {
  23.     /**
  24.      * End Of Line
  25.      *
  26.      */
  27.     const EOL = "\n";
  28.  
  29.     /**
  30.      * MimeBoundary
  31.      *
  32.      * @var string 
  33.      * @access private
  34.      */
  35.     private $MIMEBoundary;
  36.  
  37.     /**
  38.      * Тема письма
  39.      *
  40.      * @var string 
  41.      * @access private
  42.      */
  43.     private $subject = false;
  44.  
  45.     /**
  46.      * Адрес отправителя
  47.      * Формат: Имя отправителя <em@il_address>
  48.      * @var string 
  49.      * @access private
  50.      */
  51.     private $sender;
  52.  
  53.     /**
  54.      * Перечень получателей
  55.      *
  56.      * @var array 
  57.      * @access private
  58.      */
  59.     private $to = array();
  60.  
  61.     /**
  62.      * Текст сообщения
  63.      *
  64.      * @var string 
  65.      * @access private
  66.      */
  67.     private $text = false;
  68.  
  69.     /**
  70.      * Заголовки письма
  71.      *
  72.      * @var array 
  73.      * @access private
  74.      */
  75.     private $headers = array();
  76.  
  77.     /**
  78.      * Reply-to
  79.      *
  80.      * @var string 
  81.      * @access private
  82.      */
  83.     private $replyTo = array();
  84.  
  85.     /**
  86.      * Массив файлов для аттачмента
  87.      *
  88.      * @var array 
  89.      * @access private
  90.      */
  91.     private $attachments = array();
  92.  
  93.     /**
  94.      * Конструктор класса
  95.      *
  96.      * @return void 
  97.      */
  98.     public function __construct({
  99.         parent::__construct();
  100.         $this->sender = $this->getConfigValue('mail.from');
  101.     }
  102.  
  103.     /**
  104.      * Устанавливает аттрибут от
  105.      *
  106.      * @param string $email 
  107.      * @param string $name 
  108.      * @return Mail 
  109.      * @access public
  110.      */
  111.  
  112.     public function setFrom($email$name false{
  113.         $this->sender = ($name)?'=?UTF-8?B?'.base64_encode($name).'?=<'.$email.'>':$email;
  114.         return $this;
  115.     }
  116.  
  117.     /**
  118.      * Устанавливает тему письма
  119.      *
  120.      * @param string $subject 
  121.      * @return Mail 
  122.      * @access public
  123.      */
  124.  
  125.     public function setSubject($subject{
  126.         $this->subject = '=?UTF-8?B?'.base64_encode(strip_tags($subject)).'?=';
  127.         return $this;
  128.  
  129.     }
  130.  
  131.     /**
  132.      * Добавляет получателя к списку получателей
  133.      *
  134.      * @return Mail 
  135.      * @access public
  136.      */
  137.  
  138.     public function addTo($email$name false{
  139.         $email trim($email);
  140.         $this->to[$email($name)?'=?UTF-8?B?'.base64_encode($name).'?=<'.$email.'>':$email;
  141.         return $this;
  142.     }
  143.     /**
  144.      * Очищает список получателей
  145.      * 
  146.      * @access public
  147.      * @return Mail 
  148.      */
  149.     public function clearRecipientList(){
  150.         $this->to = array();
  151.         
  152.         return $this;
  153.     }
  154.     /**
  155.      * Добавление reply-to заголовка
  156.      *
  157.      * @return Mail 
  158.      * @access public
  159.      */
  160.  
  161.     public function addReplyTo($email$name false{
  162.         $this->replyTo[$email($name)?'=?UTF-8?B?'.base64_encode($name).'?=<'.$email.'>':$email;
  163.         return $this;
  164.     }
  165.  
  166.     /**
  167.      * Устанавливает текст сообщения
  168.      *
  169.      * @param string $text 
  170.      * @param mixed $data данные для шаблонизатора
  171.      * @return Mail 
  172.      * @access public
  173.      */
  174.  
  175.     public function setText($text$data false{
  176.         if ($data{
  177.             if (is_array($data)) {
  178.                 extract($data);
  179.             }
  180.             $errorLevel error_reporting(E_ERROR);
  181.             $text addslashes($text);
  182.             eval("\$text = \"$text\";");
  183.             error_reporting($errorLevel);
  184.         }
  185.         $this->text = $text;
  186.         return $this;
  187.     }
  188.  
  189.     /**
  190.      * Возвращает текст сообщения
  191.      *
  192.      * @return string 
  193.      * @access public
  194.      */
  195.  
  196.     public function getText({
  197.         return $this->text;
  198.     }
  199.  
  200.     /**
  201.      * Добавить аттач
  202.      *
  203.      * @param mixed $file 
  204.      * @return void 
  205.      * @access public
  206.      */
  207.  
  208.     public function addAttachment($file$fileName false{
  209.         if (file_exists($file)) {
  210.             $fileContent base64_encode(stripslashes(file_get_contents($file)));
  211.             $fileName (!$fileName)?basename($file):$fileName;
  212.             $this->attachments[$fileName$fileContent;
  213.         }
  214.         
  215.         return $this;
  216.     }
  217.  
  218.  
  219.     /**
  220.      * Отправляет сообщение
  221.      *
  222.      * @return boolean 
  223.      * @access public
  224.      */
  225.  
  226.     public function send({
  227.         $MIMEBoundary1 md5(time()).rand(1000,9999);
  228.         $MIMEBoundary2 md5(time()).rand(1000,9999);
  229.         
  230.         $this->headers = array('X-Mailer: PHP v'.phpversion());
  231.         $this->headers['MIME-Version: 1.0';
  232.         
  233.         # Common Headers
  234.         $this->headers['From: '.$this->sender;
  235.         $this->headers[(!empty($this->replyTo))?'Reply-To: '.implode(','$this->replyTo):'Reply-To: '.$this->sender;
  236.         $this->headers['Return-Path: '.$this->sender;
  237.         $this->headers["Content-Type: multipart/mixed;
  238.         boundary=\"".$MIMEBoundary1."\"".self::EOL;
  239.  
  240.         $message "This is a multi-part message in MIME format.".self::EOL.self::EOL;
  241.         $message .= "--".$MIMEBoundary1.self::EOL;
  242.  
  243.         $message .= "Content-Type: multipart/alternative;
  244.         boundary=\"".$MIMEBoundary2."\"".self::EOL.self::EOL;
  245.  
  246.         # Text Version
  247.         $message .= "--".$MIMEBoundary2.self::EOL;
  248.         $message .= "Content-Type: text/plain; charset=UTF-8".self::EOL;
  249.         $message .= "Content-Transfer-Encoding: 8bit".self::EOL.self::EOL;
  250.         $message .= strip_tags($this->text).self::EOL.self::EOL;
  251.  
  252.         # HTML Version
  253.         $message .= "--".$MIMEBoundary2.self::EOL;
  254.         $message .= "Content-Type: text/html; charset=UTF-8".self::EOL;
  255.         $message .= "Content-Transfer-Encoding: 8bit".self::EOL.self::EOL;
  256.         $message .= '<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD><BODY>'.$this->text.'</BODY></HTML>'.self::EOL.self::EOL;
  257.  
  258.         # Finished
  259.         $message .= "--".$MIMEBoundary2."--".self::EOL.self::EOL;  // finish with two eol's for better security. see Injection.
  260.         if (!empty($this->attachments)) {
  261.             foreach ($this->attachments as $attachName => $attach{
  262.                 $message .= "--".$MIMEBoundary1.self::EOL;
  263.                 $message .= "Content-Type: application/octet-stream; name=\"".$attachName."\"".self::EOL;
  264.                 $message .= "Content-Transfer-Encoding: base64".self::EOL;
  265.                 $message .= "Content-Disposition: attachment;".self::EOL;
  266.                 $message .= "       filename=\"".$attachName."\"".self::EOL.self::EOL;
  267.                 $message .= chunk_split($attach).self::EOL;
  268.             }
  269.         }
  270.  
  271.         $message .= "--".$MIMEBoundary1."--";
  272.         $headers implode(self::EOL$this->headers);
  273.  
  274.         if(!empty($this->to)) {
  275.             $result mail(implode(','$this->to)$this->subject$message$headers);
  276.         }
  277.         else {
  278.             $result false;
  279.         }
  280.  
  281.         return $result;
  282.     }
  283. }
В создании документации нам помог: phpDocumentor