示例#1
0
 /**
  * Protected method for sending data to SMTP connection
  *
  * @param string $data data to be sent to SMTP server
  * @param string|bool $checkCode code to check for in server response, false to skip
  * @return string|null The matched code, or null if nothing matched
  * @throws \Cake\Network\Exception\SocketException
  */
 protected function _smtpSend($data, $checkCode = '250')
 {
     $this->_lastResponse = [];
     if ($data !== null) {
         $this->_socket->write($data . "\r\n");
     }
     $timeout = $this->_config['timeout'];
     while ($checkCode !== false) {
         $response = '';
         $startTime = time();
         while (substr($response, -2) !== "\r\n" && time() - $startTime < $timeout) {
             $response .= $this->_socket->read();
         }
         if (substr($response, -2) !== "\r\n") {
             throw new SocketException('SMTP timeout.');
         }
         $responseLines = explode("\r\n", rtrim($response, "\r\n"));
         $response = end($responseLines);
         $this->_bufferResponseLines($responseLines);
         if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
             if ($code[2] === '-') {
                 continue;
             }
             return $code[1];
         }
         throw new SocketException(sprintf('SMTP Error: %s', $response));
     }
 }