Advertisement
Guest User

Untitled

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