class.smtp.php

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

PHP
786
字号
<?
  class phpmailer
  {
    var $Priority = 3;
    var $CharSet = 'iso-8859-1';
    var $ContentType = 'text/plain';
    var $Encoding = '8bit';
    var $From = 'root@localhost';
    var $FromName = 'root';
    var $Subject = '';
    var $Body = '';
    var $WordWrap = true;
    var $MailerDebug = false;
    var $UseMSMailHeaders = true;
    var $IPAddress = 'unknown';
    var $timezone = '+0800';
    var $Host = 'localhost';
    var $Port = 25;
    var $Helo = '';
    var $Timeout = 10;
    var $version = '';
    var $to = array ();
    var $cc = array ();
    var $bcc = array ();
    var $ReplyTo = array ();
    var $attachment = array ();
    var $CustomHeader = array ();
    var $boundary = false;
    var $ErrorAlerts = array ();
    var $blUseAuthLogin = false;
    var $AuthUser = '';
    var $AuthPass = '';
    function useauthlogin ($user, $pass)
    {
      $this->blUseAuthLogin = true;
      $this->AuthUser = $user;
      $this->AuthPass = $pass;
    }
    function ishtml ($bool)
    {
      if (($bool == true))
      {
        $this->ContentType = 'text/html';
      }
      else
      {
        $this->ContentType = 'text/plain';
      }
    }
    function start ()
    {
      global $appname;
      global $appversion;
      $this->Version = (($appname.' ').$appversion);
      $this->Helo = ereg_replace ('[^A-Za-z0-9]', '', $appname);
    }
    function addaddress ($address, $name = '')
    {
      $cur = count ($this->to);
      $this->to[$cur][0] = trim ($address);
      $this->to[$cur][1] = $name;
    }
    function addcc ($address, $name = '')
    {
      $cur = count ($this->cc);
      $this->cc[$cur][0] = trim ($address);
      $this->cc[$cur][1] = $name;
    }
    function addbcc ($address, $name = '')
    {
      $cur = count ($this->bcc);
      $this->bcc[$cur][0] = trim ($address);
      $this->bcc[$cur][1] = $name;
    }
    function addreplyto ($address, $name = '')
    {
      $cur = count ($this->ReplyTo);
      $this->ReplyTo[$cur][0] = trim ($address);
      $this->ReplyTo[$cur][1] = $name;
    }
    function send ()
    {
      global $use_sendmail;
      if ((((count ($this->to) + count ($this->cc)) + count ($this->bcc)) == 0))
      {
        $this->error_handler ('You must provide at least one recipient email address');
        return false;
      }
      $header = $this->create_header ();
      if (($body = $this->create_body () === false))
      {
        return false;
      }
      if ($use_sendmail)
      {
        if (($this->sendmail_send ($header, $body) === false))
        {
          return false;
        }
      }
      else
      {
        if (($this->smtp_send ($header, $body) === false))
        {
          return false;
        }
      }
      return sprintf ('%s%s', $header, $body);
    }
    function sendmail_send ($header, $body)
    {
      global $path_to_sendmail;
      if ((strtoupper (substr (PHP_OS, 0, 3)) == 'WIN'))
      {
        $this->error_handler ('Sendmail is not supported under Win32 systems');
        return false;
      }
      $sendmail = sprintf ('%s -t', $path_to_sendmail);
      if (!$mail = popen ($sendmail, 'w'))
      {
        $this->error_handler (sprintf ('Could not execute %s', $path_to_sendmail));
        return false;
      }
      fputs ($mail, $header);
      fputs ($mail, $body);
      pclose ($mail);
      return true;
    }
    function smtp_send ($header, $body)
    {
      global $enable_debug;
      $smtp = new SMTP ();
      $smtp->do_debug = $enable_debug;
      $hosts = explode (';', $this->Host);
      $index = 0;
      $connection = false;
      while ((($index < count ($hosts)) AND ($connection == false)))
      {
        if ($smtp->Connect ($hosts[$index], $this->Port, $this->Timeout))
        {
          $connection = true;
        }
        ++$index;
      }
      if (!$connection)
      {
        $this->error_handler ('SMTP Error: could not connect to SMTP host server(s)');
        return false;
      }
      if ($this->blUseAuthLogin)
      {
        if (!$smtp->AuthHello ($this->Helo, $this->AuthUser, $this->AuthPass))
        {
          $this->error_handler ('SMTP Error: Invalid username/password');
          return false;
        }
      }
      else
      {
        $smtp->Hello ($this->Helo);
      }
      $smtp->MailFrom (sprintf ('<%s>', $this->From));
      for ($i = 0; ($i < count ($this->to)); ++$i)
      {
        if (!$smtp->Recipient (sprintf ('<%s>', $this->to[$i][0])))
        {
          $this->error_handler ('SMTP Error: Recipient not accepted. Verify your relay rules');
          return false;
          continue;
        }
      }
      for ($i = 0; ($i < count ($this->cc)); ++$i)
      {
        if (!$smtp->Recipient (sprintf ('<%s>', $this->cc[$i][0])))
        {
          $this->error_handler ('SMTP Error: Recipient not accepted. Verify your relay rules');
          return false;
          continue;
        }
      }
      for ($i = 0; ($i < count ($this->bcc)); ++$i)
      {
        if (!$smtp->Recipient (sprintf ('<%s>', $this->bcc[$i][0])))
        {
          $this->error_handler ('SMTP Error: Recipient not accepted. Verify your relay rules');
          return false;
          continue;
        }
      }
      if (!$smtp->Data (sprintf ('%s%s', $header, $body)))
      {
        $this->error_handler ('SMTP Error: Data not accepted');
        return false;
      }
      $smtp->Quit ();
    }
    function addr_append ($type, $addr)
    {
      $addr_str = '';
      if ((trim ($addr[0][1]) != ''))
      {
        ($addr_str .= sprintf ('%s: "%s" <%s>', $type, $addr[0][1], $addr[0][0]));
      }
      else
      {
        ($addr_str .= sprintf ('%s: %s', $type, $addr[0][0]));
      }
      if ((1 < count ($addr)))
      {
        for ($i = 1; ($i < count ($addr)); ++$i)
        {
          if ((trim ($addr[$i][1]) != ''))
          {
            ($addr_str .= sprintf (', 
	"%s" <%s>', $addr[$i][1], $addr[$i][0]));
            continue;
          }
          else
          {
            ($addr_str .= sprintf (', 
	"%s"', $addr[$i][0]));
            continue;
          }
        }
        ($addr_str .= '
');
      }
      else
      {
        ($addr_str .= '
');
      }
      return $addr_str;
    }
    function wordwrap ($message, $length)
    {
      $line = explode ('
', $message);
      $message = '';
      for ($i = 0; ($i < count ($line)); ++$i)
      {
        $line_part = explode (' ', trim ($line[$i]));
        $buf = '';
        for ($e = 0; ($e < count ($line_part)); ++$e)
        {
          $buf_o = $buf;
          if (($e == 0))
          {
            ($buf .= $line_part[$e]);
          }
          else
          {
            ($buf .= (' '.$line_part[$e]));
          }
          if ((($length < strlen ($buf)) AND ($buf_o != '')))
          {
            ($message .= ($buf_o.'
'));
            $buf = $line_part[$e];
            continue;
          }
        }
        ($message .= ($buf.'
'));
      }
      return $message;
    }
    function create_header ()
    {
      global $use_sendmail;
      $this->Start ();
      $header = array ();
      $header[] = sprintf ('Received: from client %s for UebiMiau2.7 (webmail client); %s %s
', $this->IPAddress, date ('D, j M Y G:i:s'), $this->timezone);
      $header[] = sprintf ('Date: %s %s
', date ('D, j M Y G:i:s'), $this->timezone);
      $header[] = sprintf ('From: "%s" <%s>
', $this->FromName, trim ($this->From));
      if ((0 < count ($this->to)))
      {
        $header[] = $this->addr_append ('To', $this->to);
      }
      if ((0 < count ($this->cc)))
      {
        $header[] = $this->addr_append ('Cc', $this->cc);
      }
      if (((0 < count ($this->bcc)) AND $use_sendmail))
      {
        $header[] = $this->addr_append ('Bcc', $this->bcc);
      }
      if ((0 < count ($this->ReplyTo)))
      {
        $header[] = $this->addr_append ('Reply-to', $this->ReplyTo);
      }
      $header[] = sprintf ('Subject: %s
', trim ($this->Subject));
      $header[] = sprintf ('X-Priority: %d
', $this->Priority);
      $header[] = sprintf ('X-Mailer: %s
', $this->Version);
      $header[] = sprintf ('X-Original-IP: %s
', $this->IPAddress);
      $header[] = sprintf ('Content-Transfer-Encoding: %s
', $this->Encoding);
      $header[] = sprintf ('Return-Path: %s
', trim ($this->From));
      for ($index = 0; ($index < count ($this->CustomHeader)); ++$index)
      {
        $header[] = sprintf ('%s
', $this->CustomHeader[$index]);
      }
      if ($this->UseMSMailHeaders)
      {
        $header[] = $this->UseMSMailHeaders ();
      }
      if ((0 < count ($this->attachment)))
      {
        $header[] = sprintf ('Content-Type: multipart/mixed; charset="%s";
', $this->CharSet);
        $header[] = sprintf ('	boundary="--=%s"
', $this->boundary);
      }
      else
      {
        $header[] = sprintf ('Content-Type: %s; charset="%s";
', $this->ContentType, $this->CharSet);
      }
      $header[] = 'MIME-Version: 1.0
';
      return (join ('', $header).'
');
    }
    function create_body ()
    {
      if ($this->WordWrap)
      {
        $this->Body = $this->wordwrap ($this->Body, $this->WordWrap);
      }
      if ((0 < count ($this->attachment)))
      {
        if (!$body = $this->attach_all ())
        {
          return false;
        }
      }
      else
      {
        $body = $this->Body;
      }
      return trim ($body);
    }
    function addattachment ($path, $name = '', $type = 'application/octet-stream')
    {
      if (!is_file ($path))
      {
        $this->error_handler (sprintf ('Could not find %s file on filesystem', $path));
        return false;
      }
      $filename = basename ($path);
      if (($name == ''))
      {
        $name = $filename;
      }
      $this->boundary = ('_b'.md5 (uniqid (time ())));
      $cur = count ($this->attachment);
      $this->attachment[$cur][0] = $path;
      $this->attachment[$cur][1] = $filename;
      $this->attachment[$cur][2] = $name;
      $this->attachment[$cur][3] = $type;
      return true;
    }
    function attach_all ()
    {
      $mime = sprintf ('----=%s
', $this->boundary);
      ($mime .= sprintf ('Content-Type: %s
', $this->ContentType));
      ($mime .= 'Content-Transfer-Encoding: 8bit
');
      ($mime .= sprintf ('%s
', $this->Body));
      for ($i = 0; ($i < count ($this->attachment)); ++$i)
      {
        $path = $this->attachment[$i][0];
        $filename = $this->attachment[$i][1];
        $name = $this->attachment[$i][2];
        $type = $this->attachment[$i][3];
        ($mime .= sprintf ('----=%s
', $this->boundary));
        ($mime .= sprintf ('Content-Type: %s; name="%s"
', $type, $name));
        ($mime .= 'Content-Transfer-Encoding: base64
');

⌨️ 快捷键说明

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