Guest User

Untitled

a guest
Jan 25th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.94 KB | None | 0 0
  1. <?php
  2. /**
  3. * SendMailSmtpClass
  4. *
  5. * Класс для отправки писем через SMTP с авторизацией
  6. * Может работать через SSL протокол
  7. * Тестировалось на почтовых серверах yandex.ru, mail.ru и gmail.com
  8. *
  9. * @author Ipatov Evgeniy <admin@ipatov-soft.ru>
  10. * @version 1.0
  11. */
  12. class SendMailSmtpClass {
  13.  
  14.     /**
  15.     *
  16.     * @var string $smtp_username - логин
  17.     * @var string $smtp_password - пароль
  18.     * @var string $smtp_host - хост
  19.     * @var string $smtp_from - от кого
  20.     * @var integer $smtp_port - порт
  21.     * @var string $smtp_charset - кодировка
  22.     *
  23.     */  
  24.     public $smtp_username;
  25.     public $smtp_password;
  26.     public $smtp_host;
  27.     public $smtp_from;
  28.     public $smtp_port;
  29.     public $smtp_charset;
  30.    
  31.     public function __construct($smtp_username, $smtp_password, $smtp_host, $smtp_from, $smtp_port = 25, $smtp_charset = "utf-8") {
  32.         $this->smtp_username = $smtp_username;
  33.         $this->smtp_password = $smtp_password;
  34.         $this->smtp_host = $smtp_host;
  35.         $this->smtp_from = $smtp_from;
  36.         $this->smtp_port = $smtp_port;
  37.         $this->smtp_charset = $smtp_charset;
  38.     }
  39.    
  40.     /**
  41.     * Отправка письма
  42.     *
  43.     * @param string $mailTo - получатель письма
  44.     * @param string $subject - тема письма
  45.     * @param string $message - тело письма
  46.     * @param string $headers - заголовки письма
  47.     *
  48.     * @return bool|string В случаи отправки вернет true, иначе текст ошибки    *
  49.     */
  50.     function send($mailTo, $subject, $message, $headers) {
  51.         $contentMail = "Date: " . date("D, d M Y H:i:s") . " UT\r\n";
  52.         $contentMail .= 'Subject: =?' . $this->smtp_charset . '?B?'  . base64_encode($subject) . "=?=\r\n";
  53.         $contentMail .= $headers . "\r\n";
  54.         $contentMail .= $message . "\r\n";
  55.        
  56.         try {
  57.             if(!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30)){
  58.                 throw new Exception($errorNumber.".".$errorDescription);
  59.             }
  60.             if (!$this->_parseServer($socket, "220")){
  61.                 throw new Exception('Connection error');
  62.             }
  63.            
  64.             $server_name = $_SERVER["SERVER_NAME"];
  65.             fputs($socket, "HELO $server_name\r\n");
  66.             if (!$this->_parseServer($socket, "250")) {
  67.                 fclose($socket);
  68.                 throw new Exception('Error of command sending: HELO');
  69.             }
  70.            
  71.             fputs($socket, "AUTH LOGIN\r\n");
  72.             if (!$this->_parseServer($socket, "334")) {
  73.                 fclose($socket);
  74.                 throw new Exception('Autorization error');
  75.             }
  76.            
  77.            
  78.            
  79.             fputs($socket, base64_encode($this->smtp_username) . "\r\n");
  80.             if (!$this->_parseServer($socket, "334")) {
  81.                 fclose($socket);
  82.                 throw new Exception('Autorization error');
  83.             }
  84.            
  85.             fputs($socket, base64_encode($this->smtp_password) . "\r\n");
  86.             if (!$this->_parseServer($socket, "235")) {
  87.                 fclose($socket);
  88.                 throw new Exception('Autorization error');
  89.             }
  90.            
  91.             fputs($socket, "MAIL FROM: <".$this->smtp_username.">\r\n");
  92.             if (!$this->_parseServer($socket, "250")) {
  93.                 fclose($socket);
  94.                 throw new Exception('Error of command sending: MAIL FROM');
  95.             }
  96.            
  97.             $mailTo = ltrim($mailTo, '<');
  98.             $mailTo = rtrim($mailTo, '>');
  99.             fputs($socket, "RCPT TO: <" . $mailTo . ">\r\n");    
  100.             if (!$this->_parseServer($socket, "250")) {
  101.                 fclose($socket);
  102.                 throw new Exception('Error of command sending: RCPT TO');
  103.             }
  104.            
  105.             fputs($socket, "DATA\r\n");    
  106.             if (!$this->_parseServer($socket, "354")) {
  107.                 fclose($socket);
  108.                 throw new Exception('Error of command sending: DATA');
  109.             }
  110.            
  111.             fputs($socket, $contentMail."\r\n.\r\n");
  112.             if (!$this->_parseServer($socket, "250")) {
  113.                 fclose($socket);
  114.                 throw new Exception("E-mail didn't sent");
  115.             }
  116.            
  117.             fputs($socket, "QUIT\r\n");
  118.             fclose($socket);
  119.         } catch (Exception $e) {
  120.             return  $e->getMessage();
  121.         }
  122.         return true;
  123.     }
  124.    
  125.     private function _parseServer($socket, $response) {
  126.         while (@substr($responseServer, 3, 1) != ' ') {
  127.             if (!($responseServer = fgets($socket, 256))) {
  128.                 return false;
  129.             }
  130.         }
  131.         if (!(substr($responseServer, 0, 3) == $response)) {
  132.             return false;
  133.         }
  134.         return true;
  135.        
  136.     }
  137. }
Add Comment
Please, Sign In to add comment