addattachment(mail.zip);$smtp->sendmail($to, $from, $subject, $body, $cc, $bcc);class smtp {
 /* public variables */
 public $attachments = array(); /* private variables */ private $smtp_host; private $smtp_port; private $time_out; private $host_name; private $auth; private $user; private $pass; private $sock; /* constractor */
 public function smtp($smtp_host = null, $smtp_port = null, $user = null, $pass = null, $auth = true) {  $this->smtp_host = (!empty($smtp_host)) ? $smtp_host : smtp_host;  $this->smtp_port = (!empty($smtp_port)) ? $smtp_port : smtp_port;  $this->user = (!empty($user)) ? $user : smtp_port;  $this->pass = (!empty($pass)) ? $pass : smtp_port;  $this->auth = $auth;  $this->time_out = 15;  #  $this->host_name = localhost;  $this->sock = false; } /* main function */
 public function sendmail($to, $from, $subject = , $body = , $cc = , $bcc = ) {  $bndp = md5(uniqid()) . rand(1000, 9999);  $bnd  = md5(uniqid()) . rand(1000, 9999);  list ($msec, $sec) = explode( , microtime());  $mail_from = $this->strip_line_breaks($from);
  $mail_to = explode(,, $to);  $body = preg_replace(/(^|(\r\n))(\\.)/, , $body);  if ($cc != ) $mail_to = array_merge($mail_to, explode(,, $cc));  if ($bcc != ) $mail_to = array_merge($mail_to, explode(,, $bcc));  $headers  = mime-version:1.0 . eol;
  $headers .= to:  . $to . eol;  if ($cc != ) {  $headers .= cc:  . $cc . eol;  }  $headers .= from: $from . eol;  $headers .= subject:  . $subject . eol;  $headers .= date:  . date(r) . eol;  $headers .= x-mailer: webmail ver 1.0 (php version/ . phpversion() . ) . eol;  $headers .= message-id:  . eol;  if (count($this->attachments) > 0) {   $headers .= content-type: multipart/mixed; . eol . chr(9) .  boundary=\ . $bndp . \ . eol . eol;   $headers .= '--'.$bndp . eol;   $headers .= 'content-type : multipart/alternative; boundary=' . $bnd . '' . eol . eol;   $headers .= '--' . $bnd . eol;   $headers .= 'content-type: text/plain; charset=utf-8' . eol;   $headers .= content-transfer-encoding: 8bit . eol . eol;   $headers .= $body . eol;   $headers .= '--' . $bnd . eol;   $headers .= 'content-type: text/html; charset=utf-8' . eol;   $headers .= content-transfer-encoding: 8bit . eol . eol;   $headers .= $body . eol;   $headers .= '--' . $bnd . '--' . eol;   foreach ($this->attachments as $att) {
    $headers .= -- . $bndp . eol . $att;   }   $headers .= '--' . $bndp . '--' . eol;   $this->clear_attachments();  } else {   $headers .= 'content-type : multipart/alternative;boundary='.$bnd.'' . eol . eol;   $headers .= '--'.$bnd . eol;   $headers .= 'content-type: text/plain; charset=utf-8' . eol;   $headers .= content-transfer-encoding: 8bit . eol . eol;   $headers .= $body . eol;   $headers .= '--'.$bnd . eol;   $headers .= 'content-type: text/html; charset=utf-8' . eol;   $headers .= content-transfer-encoding: 8bit . eol . eol;   $headers .= $body . eol;   $headers .= '--'.$bnd.'--' . eol;  }  $sent = true;
  foreach ($mail_to as $rcpt_to) {   $rcpt_to = $this->strip_line_breaks($rcpt_to);   if (!$this->smtp_sockopen($rcpt_to)) {    $this->log_write(error: cannot send email to  . $rcpt_to);    $sent = false;    continue;   }   if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headers, $body)) {    $this->log_write(e-mail has been sent to );   } else {    $this->log_write(error: cannot send email to );    $sent = false;   }   fclose($this->sock);  }  $this->log_write({$mail_to} send over;);  return $sent; } public function addattachment($file, $dispo = attachment) {
  $file_data = (file_exists($file)) ? file_get_contents($file) : ;  if ($file_data != ) {   $filename = basename($file);   $ext = pathinfo($filename, pathinfo_extension);   $chunks = chunk_split(base64_encode($file_data));   $parts  = content-type: application/$ext; name=\ . $filename . \ . eol;   $parts .= content-transfer-encoding: base64 . eol;   $parts .= content-disposition:  . $dispo . ; filename=\ . $filename . \ . eol . eol;   $parts .= $chunks . eol . eol;   $this->attachments[] = $parts;  } } private function clear_attachments() {
  unset($this->attachments);  $this->attachments = array(); } /* private functions */
 private function smtp_send($helo, $from, $to, $header, $body = ) {  if (!$this->smtp_putcmd(helo, $helo)) {   //$this->log_write(error: error occurred while sending helo command.);   return false;  }  #auth  if ($this->auth) {   if (!$this->smtp_putcmd(auth login, base64_encode($this->user))) {    //$this->log_write(error: error occurred while sending helo command.);    return false;   }   if (!$this->smtp_putcmd(, base64_encode($this->pass))) {    //$this->log_write(error: error occurred while sending helo command.);    return false;   }  }  if (!$this->smtp_putcmd(mail, from:)) {   //$this->log_write(error: error occurred while sending mail from command.);   return false;  }  if (!$this->smtp_putcmd(rcpt, to:)) {   //$this->log_write(error: error occurred while sending rcpt to command.);   return false;  }  if (!$this->smtp_putcmd(data)) {   //$this->log_write(error: error occurred while sending data command.);   return false;  }  if (!$this->smtp_message($header, $body)) {   //$this->log_write(error: error occurred while sending message.);   return false;  }  if (!$this->smtp_eom()) {   //$this->log_write(error: error occurred while sending . [eom].);   return false;  }  if (!$this->smtp_putcmd(quit)) {   //$this->log_write(error: error occurred while sending quit command.);   return false;  }  return true; } private function smtp_sockopen($address) {  if ($this->smtp_host == ) {   return $this->smtp_sockopen_mx($address);  } else { // bbs.it-home.org   return $this->smtp_sockopen_relay();  } } private function smtp_sockopen_relay() {  $this->log_write(trying to connect  . $this->smtp_host . : . $this->smtp_port . ...);  $this->sock = @fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, $this->time_out);  if (!($this->sock && $this->smtp_ok())) {   $this->log_write(error: connenct error . $errstr .  ( . $errno . ));   return false;  }  $this->log_write(connected ok);  return true; } private function smtp_sockopen_mx($address) {  $domain = preg_replace(/^.+@([^@]+)$/, \1, $address);  if (!@getmxrr($domain, $mxhosts)) {   $this->log_write(error: cannot resolve mx \ . $domain . \);   return false;  }  foreach ($mxhosts as $host) {   $this->log_write(trying to  . $host . : . $this->smtp_port);   $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);   if (!($this->sock && $this->smtp_ok())) {    $this->log_write(connect error , . $errstr .  ( . $errno . ));    continue;   }   $this->log_write(connected to mx host  . $host);   return true;  }  $this->log_write(error: cannot connect to any mx hosts ( . implode(, , $mxhosts) . ));  return false; } private function smtp_message($header, $body) {  fputs($this->sock, $header . \r\n . $body);  return true; } private function smtp_eom() {  fputs($this->sock, \r\n.\r\n);  return $this->smtp_ok(); } private function smtp_ok() {  $response = str_replace(\r\n, , fgets($this->sock, 512));  if (!preg_match(/^[23]/, $response)) {   fputs($this->sock, quit\r\n);   fgets($this->sock, 512);   $this->log_write(error: remote host returned \ . $response . \);   return false;  }  return true; } private function smtp_putcmd($cmd, $arg = ) {  if ($arg != ) $cmd = ($cmd == ) ? $arg : ($cmd .   . $arg);  fputs($this->sock, $cmd . \r\n);  return $this->smtp_ok(); } private function strip_line_breaks($address) {  $address = preg_replace(/([\t\r\n])+/, , $address);  $address = preg_replace(/^.*.*$/, , $address);  return $address; } public function log_write($message) {  $message = date(m d h:i:s ) . get_current_user() . [ . getmypid() . ]:  . $message;  file_put_contents(dirname(__file__) . '/mail.log', $message . php_eol, file_append | lock_ex); }}
复制代码
   
 
   