Example #1
0
 public function connect($timeout = self::TIMEOUT, $test = false)
 {
     Eden_Mail_Error::i()->argument(1, 'int')->argument(2, 'bool');
     $host = $this->_host;
     if ($this->_ssl) {
         $host = 'ssl://' . $host;
     } else {
         $host = 'tcp://' . $host;
     }
     $errno = 0;
     $errstr = '';
     $this->_socket = @stream_socket_client($host . ':' . $this->_port, $errno, $errstr, $timeout);
     if (!$this->_socket || strlen($errstr) > 0 || $errno > 0) {
         Eden_Mail_Error::get()->setMessage(Eden_Mail_Error::SERVER_ERROR)->addVariable($host . ':' . $this->_port)->trigger();
     }
     $this->_receive();
     if (!$this->_call('EHLO ' . $_SERVER['HTTP_HOST'], 250) && !$this->_call('HELO ' . $_SERVER['HTTP_HOST'], 250)) {
         $this->disconnect();
         Eden_Mail_Error::get()->setMessage(Eden_Mail_Error::SERVER_ERROR)->addVariable($host . ':' . $this->_port)->trigger();
     }
     if ($this->_tls && !$this->_call('STARTTLS', 220, 250)) {
         if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
             $this->disconnect();
             Eden_Mail_Error::get()->setMessage(Eden_Mail_Error::TLS_ERROR)->addVariable($host . ':' . $this->_port)->trigger();
         }
         if (!$this->_call('EHLO ' . $_SERVER['HTTP_HOST'], 250) && !$this->_call('HELO ' . $_SERVER['HTTP_HOST'], 250)) {
             $this->disconnect();
             Eden_Mail_Error::get()->setMessage(Eden_Mail_Error::SERVER_ERROR)->addVariable($host . ':' . $this->_port)->trigger();
         }
     }
     if ($test) {
         $this->disconnect();
         return $this;
     }
     if (!$this->_call('AUTH LOGIN', 250, 334)) {
         $this->disconnect();
         Eden_Mail_Error::i(Eden_Mail_Error::LOGIN_ERROR)->trigger();
     }
     if (!$this->_call(base64_encode($this->_username), 334)) {
         $this->disconnect();
         Eden_Mail_Error::i()->setMessage(Eden_Mail_Error::LOGIN_ERROR);
     }
     if (!$this->_call(base64_encode($this->_password), 235, 334)) {
         $this->disconnect();
         Eden_Mail_Error::i()->setMessage(Eden_Mail_Error::LOGIN_ERROR);
     }
     return $this;
 }
 /**
  * Connects to the server
  *
  * @return this
  */
 public function connect($test = false)
 {
     Eden_Mail_Error::i()->argument(1, 'bool');
     if ($this->_loggedin) {
         return $this;
     }
     $host = $this->_host;
     if ($this->_ssl) {
         $host = 'ssl://' . $host;
     }
     $errno = 0;
     $errstr = '';
     $this->_socket = fsockopen($host, $this->_port, $errno, $errstr, self::TIMEOUT);
     if (!$this->_socket) {
         //throw exception
         Eden_Mail_Error::get()->setMessage(Eden_Mail_Error::SERVER_ERROR)->addVariable($host . ':' . $this->_port)->trigger();
     }
     $welcome = $this->_receive();
     strtok($welcome, '<');
     $this->_timestamp = strtok('>');
     if (!strpos($this->_timestamp, '@')) {
         $this->_timestamp = null;
     } else {
         $this->_timestamp = '<' . $this->_timestamp . '>';
     }
     if ($this->_tls) {
         $this->_call('STLS');
         if (!stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
             $this->disconnect();
             //throw exception
             Eden_Mail_Error::get()->setMessage(Eden_Mail_Exception::TLS_ERROR)->addVariable($host . ':' . $this->_port)->trigger();
         }
     }
     //login
     if ($this->_timestamp) {
         try {
             $this->_call('APOP ' . $this->_username . ' ' . md5($this->_timestamp . $this->_password));
             return;
         } catch (Exception $e) {
             // ignore
         }
     }
     $this->_call('USER ' . $this->_username);
     $this->_call('PASS ' . $this->_password);
     return $this;
 }