Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.99 KB | None | 0 0
  1. <?php
  2.  
  3.     class SPNO
  4.     {
  5.         private $smtp_host;    
  6.         private $smtp_socket;  
  7.         private $tls_socket;   
  8.         private $stream_src;  
  9.         private $stream_tls;   
  10.         private $auth_username;
  11.         private $auth_password;
  12.         private $recipient;    
  13.  
  14.         const CARRIAGE_RETURN_LINE_FEED = "\r\n";
  15.        
  16.         public function __construct($smtp_name, $connection_socket = 25, $enable_tls = false, $auth_username, $auth_password)
  17.         {
  18.             $this->smtp_host = $smtp_name;
  19.             $this->smtp_socket = $connection_socket;
  20.             $this->tls_socket = $enable_tls;
  21.             $this->stream_src = NULL;
  22.             $this->stream_tls = NULL;
  23.             $this->auth_username = $auth_username;
  24.             $this->auth_password = $auth_password;
  25.         }
  26.        
  27.         private function Close()
  28.         {  
  29.             if(!empty($this->stream_src))
  30.             {
  31.                 fclose($this->stream_src);
  32.                 $this->stream_src = 0;
  33.             } else return false;
  34.         }
  35.        
  36.         private function Connected()
  37.         {
  38.             if(!empty($this->stream_src))
  39.             {
  40.                 $sock_status = stream_get_meta_data($this->stream_src);
  41.                
  42.                 if(!$sock_status['eof']) return true;
  43.                 else $this->Close();
  44.                 return false;
  45.             } else return false;
  46.         }
  47.        
  48.         private function get_reply($tls_mode = false)
  49.         {
  50.             if($this->connected())
  51.             {
  52.                 $data = "";
  53.                 $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
  54.                
  55.                 while($str = @fgets($resource,515)) {
  56.                   $data .= $str;
  57.                   # if the 4th character is a space then we are done reading
  58.                   # so just break the loop
  59.                   if(substr($str,3,1) == " ") { break; }
  60.                 }
  61.                
  62.                 if (!empty($data))
  63.                 {
  64.                     return $data;
  65.                 } else return false;
  66.             } else return false;
  67.         }
  68.        
  69.         public function setConnection($timeout = 30)
  70.         {
  71.             if(!$this->connected())
  72.             {
  73.                 if ($this->tls_socket)
  74.                 {
  75.                     $this->stream_tls = fsockopen("tls://".$this->smtp_host, $this->tls_socket, $errno, $errstr, $timeout);
  76.                 }
  77.                
  78.                 $this->stream_src = fsockopen($this->smtp_host, $this->smtp_socket, $errno, $errstr, $timeout);
  79.  
  80.                 socket_set_timeout($this->stream_src, 1, 0);
  81.                 $this->get_reply();
  82.                 socket_set_timeout($this->stream_src, 0, 100000);
  83.                
  84.  
  85.                 if (!empty($this->stream_src))
  86.                 {
  87.                     return true;
  88.                 } else return false;
  89.             } else return false;
  90.         }
  91.        
  92.         public function startTls()
  93.         {
  94.             if($this->connected())
  95.             {          
  96.                 fwrite($this->stream_src, "STARTTLS" . self::CARRIAGE_RETURN_LINE_FEED);
  97.            
  98.                 $reply = $this->get_reply(true);
  99.                
  100.                 if(substr($reply, 0, 3) == 220)
  101.                 {
  102.                     if (!empty($this->tls_socket)) return true;
  103.                     else return false;
  104.                 } else return false;
  105.             } else return false;
  106.         }
  107.        
  108.         public function authLogin($tls_mode = false)
  109.         {
  110.            
  111.             $login = base64_encode($this->auth_username);
  112.             $password = base64_encode($this->auth_password);
  113.        
  114.             if($this->connected())
  115.             {
  116.                 $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
  117.                
  118.                 # AUTH LOGIN
  119.                
  120.                 fwrite($resource, "AUTH LOGIN" . self::CARRIAGE_RETURN_LINE_FEED);
  121.                
  122.                 $reply = $this->get_reply($tls_mode);
  123.                 $code = substr($reply, 0, 3);
  124.                
  125.                 if ($code != 334) return false;
  126.                
  127.                 # envoie du login au serveur
  128.                
  129.                 fwrite($resource, $login . self::CARRIAGE_RETURN_LINE_FEED);
  130.                
  131.                 $reply = $this->get_reply($tls_mode);
  132.                 $code = substr($reply, 0, 3);
  133.                
  134.                 if ($code != 334) return false;
  135.                
  136.                 # envoie du password au serveur
  137.                
  138.                 fwrite($resource, $password . self::CARRIAGE_RETURN_LINE_FEED);
  139.                
  140.                 $reply = $this->get_reply($tls_mode);
  141.                 $code = substr($reply, 0, 3);
  142.                
  143.                 if ($code == 235) return true;
  144.                 else return false;
  145.             } else return false;
  146.         }
  147.        
  148.         public function setEhlo($tls_mode = false)
  149.         {
  150.             if($this->connected())
  151.             {
  152.                 $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
  153.                 fwrite($resource, "EHLO " . $_SERVER['REMOTE_ADDR'] . self::CARRIAGE_RETURN_LINE_FEED);
  154.                
  155.                 $reply = $this->get_reply($tls_mode);
  156.                 $code = substr($reply, 0, 3);
  157.                
  158.                 if ($code == 250 OR $code == 220) return true;
  159.                 else return false;
  160.             } else return false;
  161.         }
  162.  
  163.         public function mailFrom($from, $tls_mode = false)
  164.         {
  165.             if($this->connected())
  166.             {
  167.                 $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
  168.                
  169.                 fwrite($resource, "MAIL FROM:<" . $from . ">" . self::CARRIAGE_RETURN_LINE_FEED);
  170.                
  171.                 $reply = $this->get_reply($tls_mode);
  172.  
  173.                 if(substr($reply, 0, 3) == 250) return true;
  174.                 else return false;
  175.             } else return false;
  176.         }
  177.        
  178.         public function recipient($to, $tls_mode = false)
  179.         {
  180.             if($this->connected())
  181.             {
  182.                 $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
  183.                 fwrite($resource, "RCPT TO:<" . $to . ">" . self::CARRIAGE_RETURN_LINE_FEED);
  184.                
  185.                 $reply = $this->get_reply($tls_mode);
  186.                 $code = substr($reply, 0, 3);
  187.                
  188.                 if($code == 250 OR $code == 251) {
  189.                     $this->recipient = $to;
  190.                     return true;
  191.                 }
  192.                 else return false;
  193.             } else return false;
  194.         }
  195.        
  196.         public function sendMessage($subject, $message_to_send, $header = NULL, $tls_mode = false)
  197.         {
  198.             if($this->connected())
  199.             {
  200.                 $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
  201.                 fwrite($resource, "DATA" . self::CARRIAGE_RETURN_LINE_FEED);
  202.  
  203.                 $reply = $this->get_reply($tls_mode);
  204.                
  205.                 if(substr($reply, 0, 3) == 354)
  206.                 {
  207.                
  208.                     $subject = str_replace(self::CARRIAGE_RETURN_LINE_FEED, "\n", $subject);
  209.                     $subject = str_replace("\r", "\n", $subject);
  210.                     $message_to_send = str_replace(self::CARRIAGE_RETURN_LINE_FEED, "\n", $message_to_send);
  211.                     $message_to_send = str_replace("\r", "\n", $message_to_send);
  212.                     $message_to_send = explode("\n", $message_to_send);
  213.                    
  214.                    
  215.                     if (!isset($header))
  216.                     {
  217.                         $header = "MIME-Version: 1.0" . self::CARRIAGE_RETURN_LINE_FEED;
  218.                         $header .= "Subject: " . $subject . self::CARRIAGE_RETURN_LINE_FEED;
  219.                         $header .= "From: " . $this->auth_username . " <" . $this->auth_username . ">" . self::CARRIAGE_RETURN_LINE_FEED;
  220.                         $header .= "To: " . $this->recipient . " <" . $this->recipient . ">" . self::CARRIAGE_RETURN_LINE_FEED;
  221.                         $header .= "Content-type: text/html; charset=UTF-8";
  222.                     }
  223.                    
  224.                     fwrite($resource, $header . self::CARRIAGE_RETURN_LINE_FEED);
  225.                     foreach ($message_to_send as $key => $value)
  226.                     {
  227.                         fwrite($resource, $value . self::CARRIAGE_RETURN_LINE_FEED);
  228.                     }
  229.                     fwrite($resource, self::CARRIAGE_RETURN_LINE_FEED . "." . self::CARRIAGE_RETURN_LINE_FEED);
  230.                    
  231.                     $reply = $this->get_reply($tls_mode);
  232.  
  233.                     if(substr($reply, 0, 3) == 250) return true;
  234.                     else return false;
  235.                 } else return false;
  236.             } else return false;
  237.         }
  238.        
  239.         public function closeStream($tls_mode)
  240.         {
  241.             if($this->connected())
  242.             {
  243.                 $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
  244.                 fwrite($resource, "QUIT" . self::CARRIAGE_RETURN_LINE_FEED);
  245.                
  246.                 $byemsg = $this->get_reply($tls_mode);
  247.  
  248.                 if(substr($byemsg, 0, 3) == 221)
  249.                 {
  250.                     $this->Close();
  251.                     return true;
  252.                 } else return false;
  253.             } else return false;
  254.         }
  255.     }
  256.  
  257. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement