Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SPNO
- {
- private $smtp_host;
- private $smtp_socket;
- private $tls_socket;
- private $stream_src;
- private $stream_tls;
- private $auth_username;
- private $auth_password;
- private $recipient;
- const CARRIAGE_RETURN_LINE_FEED = "\r\n";
- public function __construct($smtp_name, $connection_socket = 25, $enable_tls = false, $auth_username, $auth_password)
- {
- $this->smtp_host = $smtp_name;
- $this->smtp_socket = $connection_socket;
- $this->tls_socket = $enable_tls;
- $this->stream_src = NULL;
- $this->stream_tls = NULL;
- $this->auth_username = $auth_username;
- $this->auth_password = $auth_password;
- }
- private function Close()
- {
- if(!empty($this->stream_src))
- {
- fclose($this->stream_src);
- $this->stream_src = 0;
- } else return false;
- }
- private function Connected()
- {
- if(!empty($this->stream_src))
- {
- $sock_status = stream_get_meta_data($this->stream_src);
- if(!$sock_status['eof']) return true;
- else $this->Close();
- return false;
- } else return false;
- }
- private function get_reply($tls_mode = false)
- {
- if($this->connected())
- {
- $data = "";
- $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
- while($str = @fgets($resource,515)) {
- $data .= $str;
- # if the 4th character is a space then we are done reading
- # so just break the loop
- if(substr($str,3,1) == " ") { break; }
- }
- if (!empty($data))
- {
- return $data;
- } else return false;
- } else return false;
- }
- public function setConnection($timeout = 30)
- {
- if(!$this->connected())
- {
- if ($this->tls_socket)
- {
- $this->stream_tls = fsockopen("tls://".$this->smtp_host, $this->tls_socket, $errno, $errstr, $timeout);
- }
- $this->stream_src = fsockopen($this->smtp_host, $this->smtp_socket, $errno, $errstr, $timeout);
- socket_set_timeout($this->stream_src, 1, 0);
- $this->get_reply();
- socket_set_timeout($this->stream_src, 0, 100000);
- if (!empty($this->stream_src))
- {
- return true;
- } else return false;
- } else return false;
- }
- public function startTls()
- {
- if($this->connected())
- {
- fwrite($this->stream_src, "STARTTLS" . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply(true);
- if(substr($reply, 0, 3) == 220)
- {
- if (!empty($this->tls_socket)) return true;
- else return false;
- } else return false;
- } else return false;
- }
- public function authLogin($tls_mode = false)
- {
- $login = base64_encode($this->auth_username);
- $password = base64_encode($this->auth_password);
- if($this->connected())
- {
- $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
- # AUTH LOGIN
- fwrite($resource, "AUTH LOGIN" . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- $code = substr($reply, 0, 3);
- if ($code != 334) return false;
- # envoie du login au serveur
- fwrite($resource, $login . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- $code = substr($reply, 0, 3);
- if ($code != 334) return false;
- # envoie du password au serveur
- fwrite($resource, $password . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- $code = substr($reply, 0, 3);
- if ($code == 235) return true;
- else return false;
- } else return false;
- }
- public function setEhlo($tls_mode = false)
- {
- if($this->connected())
- {
- $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
- fwrite($resource, "EHLO " . $_SERVER['REMOTE_ADDR'] . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- $code = substr($reply, 0, 3);
- if ($code == 250 OR $code == 220) return true;
- else return false;
- } else return false;
- }
- public function mailFrom($from, $tls_mode = false)
- {
- if($this->connected())
- {
- $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
- fwrite($resource, "MAIL FROM:<" . $from . ">" . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- if(substr($reply, 0, 3) == 250) return true;
- else return false;
- } else return false;
- }
- public function recipient($to, $tls_mode = false)
- {
- if($this->connected())
- {
- $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
- fwrite($resource, "RCPT TO:<" . $to . ">" . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- $code = substr($reply, 0, 3);
- if($code == 250 OR $code == 251) {
- $this->recipient = $to;
- return true;
- }
- else return false;
- } else return false;
- }
- public function sendMessage($subject, $message_to_send, $header = NULL, $tls_mode = false)
- {
- if($this->connected())
- {
- $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
- fwrite($resource, "DATA" . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- if(substr($reply, 0, 3) == 354)
- {
- $subject = str_replace(self::CARRIAGE_RETURN_LINE_FEED, "\n", $subject);
- $subject = str_replace("\r", "\n", $subject);
- $message_to_send = str_replace(self::CARRIAGE_RETURN_LINE_FEED, "\n", $message_to_send);
- $message_to_send = str_replace("\r", "\n", $message_to_send);
- $message_to_send = explode("\n", $message_to_send);
- if (!isset($header))
- {
- $header = "MIME-Version: 1.0" . self::CARRIAGE_RETURN_LINE_FEED;
- $header .= "Subject: " . $subject . self::CARRIAGE_RETURN_LINE_FEED;
- $header .= "From: " . $this->auth_username . " <" . $this->auth_username . ">" . self::CARRIAGE_RETURN_LINE_FEED;
- $header .= "To: " . $this->recipient . " <" . $this->recipient . ">" . self::CARRIAGE_RETURN_LINE_FEED;
- $header .= "Content-type: text/html; charset=UTF-8";
- }
- fwrite($resource, $header . self::CARRIAGE_RETURN_LINE_FEED);
- foreach ($message_to_send as $key => $value)
- {
- fwrite($resource, $value . self::CARRIAGE_RETURN_LINE_FEED);
- }
- fwrite($resource, self::CARRIAGE_RETURN_LINE_FEED . "." . self::CARRIAGE_RETURN_LINE_FEED);
- $reply = $this->get_reply($tls_mode);
- if(substr($reply, 0, 3) == 250) return true;
- else return false;
- } else return false;
- } else return false;
- }
- public function closeStream($tls_mode)
- {
- if($this->connected())
- {
- $resource = ($tls_mode) ? $this->stream_tls : $this->stream_src;
- fwrite($resource, "QUIT" . self::CARRIAGE_RETURN_LINE_FEED);
- $byemsg = $this->get_reply($tls_mode);
- if(substr($byemsg, 0, 3) == 221)
- {
- $this->Close();
- return true;
- } else return false;
- } else return false;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement