class.smtp.php

来自「通达OA2007SE源代码 非常好的」· PHP 代码 · 共 786 行 · 第 1/2 页

PHP
786
字号
        ($mime .= sprintf ('Content-Disposition: attachment; filename="%s"
', $name));
        if (!($mime .= sprintf ('%s
', $this->encode_file ($path))))
        {
          return false;
          continue;
        }
      }
      ($mime .= sprintf ('
----=%s--
', $this->boundary));
      return $mime;
    }
    function encode_file ($path)
    {
      if (!$fd = fopen ($path, 'rb'))
      {
        $this->error_handler (((''.'File Error: Could not open file ').$path));
        return false;
      }
      $file = fread ($fd, filesize ($path));
      $encoded = chunk_split (base64_encode ($file));
      fclose ($fd);
      return $encoded;
    }
    function clearaddresses ()
    {
      $this->to = array ();
    }
    function clearccs ()
    {
      $this->cc = array ();
    }
    function clearbccs ()
    {
      $this->bcc = array ();
    }
    function clearreplytos ()
    {
      $this->ReplyTo = array ();
    }
    function clearallrecipients ()
    {
      $this->to = array ();
      $this->cc = array ();
      $this->bcc = array ();
    }
    function clearattachments ()
    {
      $this->attachment = array ();
    }
    function clearcustomheaders ()
    {
      $this->CustomHeader = array ();
    }
    function error_handler ($msg)
    {
      $this->ErrorAlerts[] = $msg;
      if (($this->MailerDebug == true))
      {
        print '<h3>Mailer Error</h3>';
        print 'Description:<br>';
        printf ('<font color="FF0000">%s</font>', $msg);
      }
    }
    function addcustomheader ($custom_header)
    {
      $this->CustomHeader[] = $custom_header;
    }
    function usemsmailheaders ()
    {
      $MSHeader = '';
      if (($this->Priority == 1))
      {
        $MSPriority = 'High';
      }
      else
      {
        if (($this->Priority == 5))
        {
          $MSPriority = 'Low';
        }
        else
        {
          $MSPriority = 'Medium';
        }
      }
      ($MSHeader .= sprintf ('X-MSMail-Priority: %s
', $MSPriority));
      ($MSHeader .= sprintf ('Importance: %s
', $MSPriority));
      return $MSHeader;
    }
  }
  class smtp
  {
    var $SMTP_PORT = 25;
    var $CRLF = '
';
    var $smtp_conn = null;
    var $error = null;
    var $helo_rply = null;
    var $do_debug = null;
    function smtp ()
    {
      $this->smtp_conn = 0;
      $this->error = null;
      $this->helo_rply = null;
      $this->do_debug = 0;
    }
    function connected ()
    {
      if (!empty ($this->smtp_conn))
      {
        $sock_status = socket_get_status ($this->smtp_conn);
        if ($sock_status['eof'])
        {
          $this->Close ();
          return false;
        }
        return true;
      }
      return false;
    }
    function connect ($host, $port = 0, $tval = 30)
    {
      $this->error = null;
      if ($this->connected ())
      {
        $this->error = array ('error' => 'Already connected to a server');
        return false;
      }
      if (empty ($port))
      {
        $port = $this->SMTP_PORT;
      }
      $this->smtp_conn = fsockopen ($host, $port, $errno, $errstr, $tval);
      if (empty ($this->smtp_conn))
      {
        $this->error = array ('error' => 'Failed to connect to server', 'errno' => $errno, 'errstr' => $errstr);
        return false;
      }
      $announce = $this->get_lines ();
      return true;
    }
    function close ()
    {
      $this->error = null;
      $this->helo_rply = null;
      if (!empty ($this->smtp_conn))
      {
        fclose ($this->smtp_conn);
        $this->smtp_conn = 0;
      }
    }
    function data ($msg_data)
    {
      $this->error = null;
      if (!$this->connected ())
      {
        $this->error = array ('error' => 'Called Data() without being connected');
        return false;
      }
      $this->send_line ('DATA');
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 354))
      {
        $this->error = array ('error' => 'DATA command not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      $msg_data = str_replace ('
', '
', $msg_data);
      $msg_data = str_replace ('
', '
', $msg_data);
      $lines = explode ('
', $msg_data);
      $field = substr ($lines[0], 0, strpos ($lines[0], ':'));
      $in_headers = false;
      if ((!empty ($field) AND !strstr ($field, ' ')))
      {
        $in_headers = true;
      }
      $max_line_length = 998;
      $line = each ($lines)[1];
      if ()
      {
        $lines_out = null;
        if ((($line == '') AND $in_headers))
        {
          $in_headers = false;
        }
        while (($max_line_length < strlen ($line)))
        {
          $pos = strrpos (substr ($line, 0, $max_line_length), ' ');
          $lines_out[] = substr ($line, 0, $pos);
          $line = substr ($line, ($pos + 1));
          if ($in_headers)
          {
            $line = ('	'.$line);
            continue;
          }
        }
        $lines_out[] = $line;
        $line_out = each ($lines_out)[1];
        if ()
        {
          if (($line_out[0] == '.'))
          {
            $line_out = ('.'.$line_out);
          }
          ($tmpdata .= ($line_out.$this->CRLF));
        }
      }
      $this->send_line ((($tmpdata.$this->CRLF).'.'));
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 250))
      {
        $this->error = array ('error' => 'DATA not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      return true;
    }
    function hello ($host = '')
    {
      $this->error = null;
      if (!$this->connected ())
      {
        $this->error = array ('error' => 'Called Hello() without being connected');
        return false;
      }
      if (empty ($host))
      {
        $host = 'localhost';
      }
      $this->send_line (('HELO '.$host));
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 250))
      {
        $this->error = array ('error' => 'HELO not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      $this->helo_rply = $rply;
      return true;
    }
    function authhello ($host = '', $user = '', $pass = '')
    {
      $this->error = null;
      if (!$this->connected ())
      {
        $this->error = array ('error' => 'Called Hello() without being connected');
        return false;
      }
      if (empty ($host))
      {
        $host = 'localhost';
      }
      $this->send_line (('EHLO '.$host));
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 250))
      {
        $this->error = array ('error' => 'EHLO not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      $this->helo_rply = $rply;
      $this->send_line ('AUTH LOGIN');
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 334))
      {
        $this->error = array ('error' => 'AUTH LOGIN not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      $this->send_line (base64_encode ($user));
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 334))
      {
        $this->error = array ('error' => 'USER not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      $this->send_line (base64_encode ($pass));
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 235))
      {
        $this->error = array ('error' => 'PASSWORD not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      return true;
    }
    function mailfrom ($from)
    {
      $this->error = null;
      if (!$this->connected ())
      {
        $this->error = array ('error' => 'Called Mail() without being connected');
        return false;
      }
      $this->send_line (('MAIL FROM:'.$from));
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if (($code != 250))
      {
        $this->error = array ('error' => 'MAIL not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      return true;
    }
    function quit ($close_on_error = true)
    {
      $this->error = null;
      if (!$this->connected ())
      {
        $this->error = array ('error' => 'Called Quit() without being connected');
        return false;
      }
      $this->send_line ('QUIT');
      $byemsg = $this->get_lines ();
      $rval = true;
      $e = null;
      $code = substr ($byemsg, 0, 3);
      if (($code != 221))
      {
        $e = array ('error' => 'SMTP server rejected quit command', 'smtp_code' => $code, 'smtp_rply' => substr ($byemsg, 4));
        $rval = false;
      }
      if ((empty ($e) OR $close_on_error))
      {
        $this->Close ();
      }
      return $rval;
    }
    function recipient ($to)
    {
      $this->error = null;
      if (!$this->connected ())
      {
        $this->error = array ('error' => 'Called Recipient() without being connected');
        return false;
      }
      $this->send_line (('RCPT TO:'.$to));
      $rply = $this->get_lines ();
      $code = substr ($rply, 0, 3);
      if ((($code != 250) AND ($code != 251)))
      {
        $this->error = array ('error' => 'RCPT not accepted from server', 'smtp_code' => $code, 'smtp_msg' => substr ($rply, 4));
        return false;
      }
      return true;
    }
    function get_lines ()
    {
      $data = '';
      while ($str = fgets ($this->smtp_conn, 515))
      {
        ($data .= $str);
        if ((substr ($str, 3, 1) == ' '))
        {
          break;
        }
      }
      if ($this->do_debug)
      {
        $tmp = ereg_replace ('(
|
)', '', $data);
        echo (('<font style="font-size:12px; font-family: Courier New; background-color: white; color: black;"><- <b>'.htmlspecialchars ($tmp)).'</span><br>
');
        flush ();
      }
      return $data;
    }
    function send_line ($data)
    {
      fputs ($this->smtp_conn, ($data.$this->CRLF));
      if ($this->do_debug)
      {
        $data = htmlspecialchars ($data);
        echo (('<font style="font-size:12px; font-family: Courier New; background-color: white; color: black;">-> '.nl2br ($data)).'</font><br>
');
        flush ();
      }
    }
  }
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?