Отправка почты (Класс)

  1. class KMail {
  2.  
  3.     private $mb = false;
  4.  
  5.     private $header = false;
  6.  
  7.     private $subject = '(No subject)';
  8.  
  9.     private $to = array();
  10.  
  11.     private $body = '';
  12.  
  13.     public function __construct() {
  14.         $this->set_mb();    
  15.     }  
  16.  
  17.     public function send_mail_utf8($to, $subject, $message, $header) {
  18.         return mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=', $message, 'MIME-Version: 1.0' . PHP_EOL . $header);
  19.     }
  20.  
  21.     public function send() {
  22.  
  23.         if (!$this->get_mb()) {
  24.             throw new Exception('Ошибка установки уникального значения');            
  25.         }
  26.  
  27.         if (!$this->get_header()) {
  28.             $this->set_header();
  29.         }
  30.  
  31.         if (!$this->get_body()) {
  32.             throw new Exception('Отсутсвует тело письма');        
  33.         }
  34.  
  35.         if (sizeof($this->to) > 0) {
  36.             foreach($this->to as $to) {
  37.                 $this->send_mail_utf8($to, $this->get_subject(), $this->get_body() . $this->end_body(), $this->get_header());
  38.             }    
  39.         } else {
  40.             throw new Exception('Отсутствует адресат');    
  41.         }
  42.  
  43.     }
  44.  
  45.     public function add_to($mail) {
  46.         $this->to[] = $mail;    
  47.     }
  48.  
  49.     public function add_body_message($text) {
  50.         $this->body .= '--' . $this->mb . PHP_EOL .
  51.         'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL .
  52.         'Content-Disposition: inline' . PHP_EOL .
  53.         'Content-Transfer-Encoding: base64' . PHP_EOL . PHP_EOL .
  54.         chunk_split(base64_encode($text)) . PHP_EOL;    
  55.     }
  56.  
  57.     public function add_body_file($file_name, $file_stream) {
  58.         $this->body .= PHP_EOL . '--' . $this->mb . PHP_EOL .
  59.         'Content-Type: application/octet-stream; name="' . $file_name . '"' . PHP_EOL .
  60.         'Content-Disposition: attachment;' . PHP_EOL .
  61.         ' filename="' . $file_name . '"' . PHP_EOL .
  62.         'Content-Transfer-Encoding: base64' . PHP_EOL . PHP_EOL . chunk_split(base64_encode($file_stream));    
  63.     }
  64.  
  65.     public function end_body() {
  66.         return '--' . $this->mb . '--';    
  67.     }
  68.  
  69.     public function get_body() {
  70.         return $this->body;    
  71.     }
  72.  
  73.     public function set_mb() {
  74.         $this->mb = '_=_Multipart_Boundary_' . substr(md5(uniqid(time())), 0, 8);
  75.     }
  76.  
  77.     public function set_subject($subject) {
  78.         if ($subject) {
  79.             $this->subject = $subject;
  80.         }
  81.     }
  82.  
  83.     public function get_subject() {
  84.         return $this->subject;
  85.     }
  86.  
  87.     public function get_mb() {
  88.         return $this->mb;    
  89.     }
  90.  
  91.     public function set_header($email = 'No reply') {
  92.         $this->header = 'Content-Type: multipart/mixed; boundary="' . $this->get_mb() . '"' . PHP_EOL . 'X-Mailer: PHP' . PHP_EOL . 'Reply-To: ' . $email . PHP_EOL;    
  93.     }
  94.  
  95.     public function get_header() {
  96.         return $this->header;    
  97.     }
  98.  
  99. }
Пример
  1. require('kmail.class.php');
  2.  
  3. $mail = new KMail;
  4.  
  5. $mail->add_to('test@mail.ru');
  6. $mail->add_to('annonimus@gmail.com');
  7. $mail->set_subject('TESTUS');
  8. $mail->add_body_message('Test mail script');
  9. $mail->add_body_file('картинка.png', file_get_contents('mirror.png'));
  10. $mail->add_body_file('картинка2.png', file_get_contents('mirror2.png'));
  11. $mail->send();

Реклама

Мы в соцсетях

tw tg yt gt