贴两个子程序吧!function login($pop_user, $pop_pass, $server, $user) {  $sock = fsockopen($server,"110");
  $command = array("USER $pop_user", "PASS $pop_pass");
  $status = fgets($sock, 1024);
  for ($i=0; $i<count($command); $i++) {
   fputs($sock, "$command[$i]\r\n");
   if (ereg("^-", fgets($sock, 1024))) {
    fputs($sock, "quit\r\n");
    fclose($sock);
   }
   else $ok++;
  }
  
  if ($ok == "2") {    fputs($sock, "QUIT\r\n");
    fclose($sock);    $token = md5($REMOTE_ADDR.(double)microtime().time());
$time = time();
mysql_query("insert into pop_temp (token, password, user, time) values ('$token', '$pop_pass', '$user', '$time')") or die(mysql_error());
return $token;
  }
}function send($til, $emne, $tekst, $cc, $bcc) { if (!$emne) $emne = "<none>"; global $fra;
 global $user;
 
 $items = array("til", "cc", "bcc");
 for($i=0; $i<count($items); $i++) $$items[$i] = ereg_replace("^, ", "", $$items[$i]);
 
 if ($cc)  $headers .= "CC: $cc\n";
 if ($bcc) $headers .= "BCC: $bcc\n";
 
           $headers .= "X-Mailer: a php script made by Morten - mbn.dk\n";
           $headers .= "X-SenderHost: $GLOBALS[REMOTE_HOST]\n";
           $headers .= "Content-Type: text/plain\n";
           $headers .= "From: $fra\n";
 
 $mail = mail(stripslashes($til), stripslashes($emne), stripslashes($tekst), stripslashes($headers));
 
 if ($mail) {
  $time    = date("d-m-Y H:i:s", time());
  $gemt    = "pop_".$user."_gemt";
  $til     = addslashes($til);
  $fra     = addslashes($fra);
  $emne    = addslashes($emne);
  $tekst   = addslashes($tekst);
  $headers = "Du kan ikke f?oplyst alle headers ved sendt post.";
  mysql_query("insert into $gemt (til,fra,emne,dato,tekst,bakke,headers) values ('$til','$fra','$emne','$time','$tekst','sendt','$headers')");
 }
 if ($mail) return "<p style=\"text-align:center;\">Din mail (".htmlentities(stripslashes(stripslashes($emne))).") til ".htmlentities(stripslashes(stripslashes($til)))." er sendt.</p>";
}function gem($get, $bakke) {
  global $pop_user;
  global $pop_pass;
  global $server;
  global $user;  $sock = fsockopen($server,"110");
  $command = array("USER $pop_user", "PASS $pop_pass");
  for ($i=0; $i<count($command); $i++) {
   fputs($sock, "$command[$i]\r\n");
   if (ereg("^-", fgets($sock, 1024))) {
    fputs($sock, "quit\r\n");
    fclose($sock);
   }
  }
  
  if (ereg("^\+", fgets($sock, 1024))) {
   fputs($sock, "RETR $get\r\n");
   if (ereg("^\+", fgets($sock, 1024))) {
    while (substr($str = fgets($sock, 1024), 0, 2) != chr(13).chr(10)) {
      $all[] = trim($str);
      if (substr($str, 0, 4) == "To: ")      $to      = substr($str, 4);
      if (substr($str, 0, 9) == "Subject: ") $subject = trim(substr($str, 9));
      if (substr($str, 0, 6) == "From: ")    $from    = substr($str, 6);
      if (substr($str, 0, 6) == "Date: ")    $date    = substr($str, 6);
    }  if (!$subject) $subject = "<none>";

    while (substr($str = fgets($sock, 1024), 0, 3) != ".".chr(13).chr(10)) $text[] = trim($str);   $text    = addslashes(implode("\n", $text));
 $all     = addslashes(implode("\n", $all));
 $gemt    = "pop_".$user."_gemt";
 $to      = addslashes($to);
 $from    = addslashes($from);
 $subject = addslashes($subject);
 $date    = addslashes($date);
 $save    = mysql_query("insert into $gemt (til,fra,emne,dato,tekst,headers, bakke) values ('$to', '$from', '$subject', '$date', '$text', '$all', '$bakke')") or die(mysql_error());
   }
  } fputs($sock, "QUIT\r\n");
 fclose($sock);
 
  if ($save) return true;
}function makeReply($get) {  global $pop_user;
  global $pop_pass;
  global $server  $sock = fsockopen($server,"110");
  $command = array("USER $pop_user", "PASS $pop_pass");
  for ($i=0; $i<count($command); $i++) {
   fputs($sock, "$command[$i]\r\n");
   if (ereg("^-", fgets($sock, 1024))) {
    fputs($sock, "quit\r\n");
    fclose($sock);
   }
  }
  
  if (ereg("^\+", fgets($sock, 1024))) {
   fputs($sock, "RETR $get\r\n");
   if (ereg("^\+", fgets($sock, 1024))) {
    while (substr($str = fgets($sock, 1024), 0, 2) != chr(13).chr(10)) {
      if (substr($str, 0, 9)  == "Subject: ")                   $subject = trim(substr($str, 9));
      if (substr($str, 0, 10) == "Reply-To: ")                  $ReplyTo = substr($str, 10);
      if (substr($str, 0, 6)  == "From: ")                      $from    = substr($str, 6);
      if (substr($str, 0, 27) == "Content-Transfer-Encoding: ") $cte     = substr($str, 27);
    }  if (!$subject) $subject = "<none>";

    while (substr($str = fgets($sock, 1024), 0, 3) != ".".chr(13).chr(10)) $text[] = ereg_replace("^--$", "-- ", trim($str)); $text = implode("\n", $text); $return[subject]   = "Re: ".eregi_replace("^([a-z]{2}: )*", "", $subject);
if ($ReplyTo) {
  $return[ReplyTo] = htmlentities($ReplyTo);
}
$return[from]      = htmlentities($from);
     if (eregi("quoted-printable", $cte)) $doQp = "1";
$text              = quote($text, $doQp);
$return[text]      = trim($from)." wrote:\n".trim($text);
   }
  }  fputs($sock, "QUIT\r\n");
  fclose($sock);
 
  return $return;
}function get($get) {  global $pop_user;
  global $pop_pass;
  global $server  $sock = fsockopen($server,"110");
  $command = array("USER $pop_user", "PASS $pop_pass");
  for ($i=0; $i<count($command); $i++) {
   fputs($sock, "$command[$i]\r\n");
   if (ereg("^-", fgets($sock, 1024))) {
    fputs($sock, "quit\r\n");
    fclose($sock);
   }
  }
  
  if (ereg("^\+", fgets($sock, 1024))) {
   fputs($sock, "RETR $get\r\n");
   if (ereg("^\+", fgets($sock, 1024))) {
    while (substr($str = fgets($sock, 1024), 0, 2) != chr(13).chr(10)) {
     if ($GLOBALS[allheaders]) $all[] = trim($str);
  else {
      if (substr($str, 0, 4) == "To: ")                         $to      = substr($str, 4);
      if (substr($str, 0, 9) == "Subject: ")                    $subject = trim(substr($str, 9));
      if (substr($str, 0, 6) == "From: ")                       $from    = substr($str, 6);
      if (substr($str, 0, 6) == "Date: ")                       $date    = substr($str, 6);
      if (substr($str, 0, 27) == "Content-Transfer-Encoding: ") $cte     = substr($str, 27);
  }
   }
   
   if (!$subject) $subject = "<none>";     while (substr($str = fgets($sock, 1024), 0, 3) != ".".chr(13).chr(10)) $text[] = $str;   if (eregi("quoted-printable", $cte)) {
     $to      = qp($to);
     $subject = qp($subject);
     $from    = qp($from);
     $date    = qp($date);
    } for($i=0; $i<count($text); $i++) {
 $text[$i] = q($text[$i]);
     $text[$i] = ereg_replace("(^&gt;)(.)*", "<span style=\"color:gray\">\\0</span>", $text[$i]);
     $text[$i] = ereg_replace("(^\|)(.)*", "<span style=\"color:gray\">\\0</span>", $text[$i]);
}    if ($GLOBALS[allheaders]) {
     $return[] = "<table border=\"1\" cellspacing=\"0\" bordercolor=\"black\">";
   $return[] = "<tr><td nowrap>".q(implode("\n", $all))."<td nowrap></tr>";
   $return[] = "<tr><td nowrap>".implode("\n", $text)."</td></tr>";
   $return[] = "</table>";
    }    else {
     $return[] = "<table border=\"1\" cellspacing=\"0\" bordercolor=\"black\">";
     $return[] = "<tr><th>Til:</th><td nowrap>".q($to)."</td></tr>";
     $return[] = "<tr><th>Fra:</th><td nowrap>".q($from)."</td></tr>";
     $return[] = "<tr><th>Dato:</th><td nowrap>".q($date)."</td></tr>";
     $return[] = "<tr><th>Emne:</th><td nowrap>".q($subject)."</td></tr>";
     $return[] = "<tr><th>&nbsp;</th><td nowrap>".qp(implode("\n", $text))."</td></tr>";
     $return[] = "</table>";
    }
   }
  } fputs($sock, "QUIT\r\n");
 fclose($sock);
 
 if ($return) {
  $return = implode("\n", $return);
  return "$return";
 } 
}function liste() {  global $pop_user;
  global $pop_pass;
  global $server $sock = fsockopen($server,"110");
 $command = array("USER $pop_user", "PASS $pop_pass");
 for ($i=0; $i<count($command); $i++) {
  fputs($sock, "$command[$i]\r\n");
  if (ereg("^-", fgets($sock, 1024))) {
   fputs($sock, "quit\r\n");
   fclose($sock);
  }
 }
 if (ereg("^\+", fgets($sock, 1024))) {
  fputs($sock, "STAT\r\n");
  $str    = fgets($sock, 1024);
  $split  = split(" ", $str);
  $number = trim($split[1]);
  $total = ereg_replace('(\.)?0+$', '', number_format((trim($split[2])/1024),2,".",""))." KB";
  flush();  fputs($sock, "LIST\r\n");
  if (ereg("^\+", fgets($sock, 1024))) {
    while (!ereg("^\.", $str = fgets($sock, 1024))) $list .= $str;
    $split  = split("\n", trim($list));
for($i=0; $i<count($split); $i++) {
  $split_igen = split(" ", $split[$i]);
  $size[$split_igen[0]] = ereg_replace('(\.)?0+$', '', number_format(($split_igen[1]/1024),2,".",""))." KB";
}
  }  if ($number) {
   $return[] = "<tr><th>Nr.</th><th>Emne</th><th>Fra</th><th>Dato</th></tr>";
   for($i=1; $i<=$number; $i++) {    $command = "TOP $i 0";
    fputs($sock, $command."\r\n");
    $str = fgets($sock, 1024);    while (substr($str = fgets($sock, 1024), 0, 2)  != chr(13).chr(10)) {
     if (substr($str, 0, 9)  == "Subject: ")       $subject     = trim(substr($str, 9));
     if (substr($str, 0, 6)  == "From: ")          $from        = q(substr($str, 6));
     if (substr($str, 0, 6)  == "Date: ")          $date        = q(substr($str, 6));
 if (substr($str, 0, 14) == "Content-Type: ")  $ContentType = htmlentities(trim(substr($str, 14)));
    }

if (!$subject) $subject = "<none>";
if ($subject) $subject = q($subject);

if ($ContentType) {
  if (eregi("text/plain", $ContentType)) $color = "navy";
  else $color = "red";
}
else $color = "navy";    if ($from) $return[] = "<tr><td nowrap><input type=\"checkbox\" name=\"i_er[]\" value=\"$i\"> $i ($size[$i])</td>\n
                        <td nowrap><a href=\"".basename($PHP_SELF)."?get=$i\" accesskey=\"$i\" title=\"$ContentType\" style=\"color: $color\">$subject</a></td>\n
<td nowrap>$from</td>\n
<td nowrap>$date</td></tr>";
   }
  }
 } fputs($sock, "QUIT\r\n");
 fclose($sock); if (count($return) > "1") {
  $result  = "<table border=\"1\" cellspacing=\"0\" bordercolor=\"black\">";
  $result .= "<form action=\"./\" method=\"post\">";
  $result .= implode("\n", $return);
  $result .= "<tr><td nowrap colspan=\"4\"><input type=\"submit\" name=\"slet\" value=\"Slet\" class=\"button\">\n";
  $bakker = bakker();
  if ($bakker){
    $result .= "<select name=\"bakke\" class=\"button\">";
    for($i=0; $i<count($bakker); $i++) {
      $result .= "<option value=\"$bakker[$i]\">$bakker[$i]</option>";
    }
    $result .= "</select> <input type=\"submit\" name=\"gem\" value=\"&lt;- Gem\" class=\"button\"></td></tr>";
  }
  $result .= "<input type=\"hidden\" name=\"vis\" value=\"ind\"></table></form>";
 }
 if ($number == "1") return "<p>Du har 1 e-mail, der fylder $total.</p>".$result;
 elseif ($number > "1") return "<p>Du har $number e-mails, der samlet fylder $total.</p>".$result;
 else return "<p>Du har ingen e-mails.</p>".$result;
 
}function slet($del) {  global $pop_user;
  global $pop_pass;
  global $server  $sock = fsockopen($server,"110");
  $command = array("USER $pop_user", "PASS $pop_pass");
  for ($i=0; $i<count($command); $i++) {
   fputs($sock, "$command[$i]\r\n");
   if (ereg("^-", fgets($sock, 1024))) {
    fputs($sock, "quit\r\n");
    fclose($sock);
   }
  }
  if (ereg("^\+", fgets($sock, 1024))) {
   for($q=0; $q<count($del); $q++) {
    fputs($sock, "DELE $del[$q]\r\n");
$result = fgets($sock, 1024);
if (ereg("^\+", $result)) $return[] = $del[$q];
flush();
   }
  }
  
 fputs($sock, "QUIT\r\n");
}怎么用,自己试试吧!
不过它发信是用的Mail函数。下面给你一个用SMTP的类:
<?class smtp_class
{
 var $host_name="";
 var $host_port=25;
 var $localhost="";
 var $timeout=0;
 var $error="";
 var $debug=1;
 var $esmtp=1;
 var $esmtp_host="";
 var $esmtp_extensions=array();
 var $maximum_piped_recipients=100; /* private variables - DO NOT ACCESS */ var $state="Disconnected";
 var $connection=0;
 var $pending_recipients=0; /* Private methods - DO NOT CALL */ Function OutputDebug($message)
 {
  echo $message,"<br>\n";
 } Function GetLine()
 {
  for($line="";;)
  {
   if(feof($this->connection))
   {
    $this->error="reached the end of stream while reading from socket";
    return(0);
   }
   if(($data=fgets($this->connection,100))==false)
   {
    $this->error="it was not possible to read line from socket";
    return(0);
   }
   $line.=$data;
   $length=strlen($line);
   if($length>=2
   && substr($line,$length-2,2)=="\r\n")
   {
    $line=substr($line,0,$length-2);
    if($this->debug)
     $this->OutputDebug("< $line");
    return($line);
   }
  }
 } Function PutLine($line)
 {
  if($this->debug)
   $this->OutputDebug("> $line");
  if(!fputs($this->connection,"$line\r\n"))
  {
   $this->error="it was not possible to write line to socket";
   return(0);
  }
  return(1);
 } Function PutData($data)
 {
  if(strlen($data))
  {
   if($this->debug)
    $this->OutputDebug("> $data");
   if(!fputs($this->connection,$data))
   {
    $this->error="it was not possible to write data to socket";
    return(0);
   }
  }
  return(1);
 } Function VerifyResultLines($code,$responses="")
 {
  if(GetType($responses)!="array")
   $responses=array();
  Unset($match_code);  while(($line=$this->GetLine($this->connection)))
  {
   if(IsSet($match_code))
   {
    if(strcmp(strtok($line," -"),$match_code))
    {
     $this->error=$line;
     return(0);
    }
   }
   else
   {
    $match_code=strtok($line," -");
    if(GetType($code)=="array")
    {
     for($codes=0;$codes<count($code) && strcmp($match_code,$code[$codes]);$codes++);
     if($codes>=count($code))
     {
      $this->error=$line;
      return(0);
     }
    }
    else
    {
     if(strcmp($match_code,$code))
     {
      $this->error=$line;
      return(0);
     }
    }
   }
   $responses[]=strtok("");
   if(!strcmp($match_code,strtok($line," ")))
    return(1);
  }
  return(-1);
 } Function FlushRecipients()
 {
  if($this->pending_sender)
  {
   if($this->VerifyResultLines("250")<=0)
    return(0);
   $this->pending_sender=0;
  }
  for(;$this->pending_recipients;$this->pending_recipients--)
  {
   if($this->VerifyResultLines(array("250","251"))<=0)
    return(0);
  }
  return(1);
 } /* Public methods */ Function Connect()
 {
  $this->error=$error="";
   $this->esmtp_host="";
   $this->esmtp_extensions=array();
  if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port))))
  {
   switch($error)
   {
    case -3:
     $this->error="-3 socket could not be created";
     return(0);
    case -4:
     $this->error="-4 dns lookup on hostname \"".$host_name."\" failed";
     return(0);
    case -5:
     $this->error="-5 connection refused or timed out";
     return(0);
    case -6:
     $this->error="-6 fdopen() call failed";
     return(0);
    case -7:
     $this->error="-7 setvbuf() call failed";
     return(0);
    default:
     $this->error=$error." could not connect to the host \"".$this->host_name."\"";
     return(0);
   }
  }
  else
  {
   if(!strcmp($localhost=$this->localhost,"")
   && !strcmp($localhost=getenv("SERVER_NAME"),"")
   && !strcmp($localhost=getenv("HOST"),""))
     $localhost="localhost";
    $success=0;
    if($this->VerifyResultLines("220")>0)
    {
     if($this->esmtp)
     {
      $responses=array();
      if($this->PutLine("EHLO $localhost")
      && $this->VerifyResultLines("250",&$responses)>0)
      {
       $this->esmtp_host=strtok($responses[0]," ");
       for($response=1;$response<count($responses);$response++)
       {
        $extension=strtoupper(strtok($responses[$response]," "));
        $this->esmtp_extensions[$extension]=strtok("");
       }
       $success=1;
      }
     }
     if(!$success
     && $this->PutLine("HELO $localhost")
     && $this->VerifyResultLines("250")>0)
      $success=1;
   }
   if($success)
   {
    $this->state="Connected";
    return(1);
   }
   else
   {
    fclose($this->connection);
    $this->connection=0;
    $this->state="Disconnected";
    return(0);
   }
  }
 } Function MailFrom($sender)
 {
  if(strcmp($this->state,"Connected"))
  {
   $this->error="connection is not in the initial state";
   return(0);
  }
  $this->error="";
  if(!$this->PutLine("MAIL FROM: &lt;".$sender."&gt;"))
   return(0);
  if(!IsSet($this->esmtp_extensions["PIPELINING"])
  && $this->VerifyResultLines("250")<=0)
   return(0);
  $this->state="SenderSet";
  if(IsSet($this->esmtp_extensions["PIPELINING"]))
   $this->pending_sender=1;
  $this->pending_recipients=0;
  return(1);
 } Function SetRecipient($recipient)
 {
  switch($this->state)
  {
   case "SenderSet":
   case "RecipientSet":
    break;
   default:
    $this->error="connection is not in the recipient setting state";
    return(0);
  }
  $this->error="";
  if(!$this->PutLine("RCPT TO:&lt;".$recipient."&gt;"))
   return(0);
  if(IsSet($this->esmtp_extensions["PIPELINING"]))
  {
   $this->pending_recipients++;
   if($this->pending_recipients>=$this->maximum_piped_recipients)
   {
    if(!$this->FlushRecipients())
     return(0);
   }
  }
  else
  {
   if($this->VerifyResultLines(array("250","251"))<=0)
    return(0);
  }
  $this->state="RecipientSet";
  return(1);
 } Function StartData()
 {
  if(strcmp($this->state,"RecipientSet"))
  {
   $this->error="connection is not in the start sending data state";
   return(0);
  }
  $this->error="";
  if(!$this->PutLine("DATA"))
   return(0);
  if($this->pending_recipients)
  {
   if(!$this->FlushRecipients())
    return(0);
  }
  if($this->VerifyResultLines("354")<=0)
   return(0);
  $this->state="SendingData";
  return(1);
 } Function PrepareData($data,&$output)
 {
  $length=strlen(&$data);
  for($output="",$position=0;$position<$length;)
  {
   $next_position=$length;
   for($current=$position;$current<$length;$current++)
   {
    switch($data[$current])
    {
     case "\n":
      $next_position=$current+1;
      break 2;
     case "\r":
      $next_position=$current+1;
      if($data[$next_position]=="\n")
       $next_position++;
      break 2;
    }
   }
   if($data[$position]==".")
    $output.=".";
   $output.=substr(&$data,$position,$current-$position)."\r\n";
   $position=$next_position;
  }
 } Function SendData($data)
 {
  if(strcmp($this->state,"SendingData"))
  {
   $this->error="connection is not in the sending data state";
   return(0);
  }
  $this->error="";
  return($this->PutData(&$data));
 } Function EndSendingData()
 {
  if(strcmp($this->state,"SendingData"))
  {
   $this->error="connection is not in the sending data state";
   return(0);
  }
  $this->error="";
  if(!$this->PutLine("\r\n.")
  || $this->VerifyResultLines("250")<=0)
   return(0);
  $this->state="Connected";
  return(1);
 } Function ResetConnection()
 {
  switch($this->state)
  {
   case "Connected":
    return(1);
   case "SendingData":
    $this->error="can not reset the connection while sending data";
    return(0);
   case "Disconnected":
    $this->error="can not reset the connection before it is established";
    return(0);
  }
  $this->error="";
  if(!$this->PutLine("RSET")
  || $this->VerifyResultLines("250")<=0)
   return(0);
  $this->state="Connected";
  return(1);
 } Function Disconnect($quit=1)
 {
  if(!strcmp($this->state,"Disconnected"))
  {
   $this->error="it was not previously established a SMTP connection";
   return(0);
  }
  $this->error="";
  if(!strcmp($this->state,"Connected")
  && $quit
  && (!$this->PutLine("QUIT")
  || $this->VerifyResultLines("221")<=0))
   return(0);
  fclose($this->connection);
  $this->connection=0;
  $this->state="Disconnected";
  return(1);
 } Function SendMessage($sender,$recipients,$headers,$body)
 {
  if(($success=$this->Connect()))
  {
   if(($success=$this->MailFrom($sender)))
   {
    for($recipient=0;$recipient<count($recipients);$recipient++)
    {
     if(!($success=$this->SetRecipient($recipients[$recipient])))
      break;
    }
    if($success
    && ($success=$this->StartData()))
    {
     for($header_data="",$header=0;$header<count($headers);$header++)
      $header_data.=$headers[$header]."\r\n";
     if(($success=$this->SendData($header_data."\r\n")))
     {
      $this->PrepareData($body,&$body_data);
      $success=$this->SendData($body_data);
     }
     if($success)
      $success=$this->EndSendingData();
    }
   }
   $disconnect_success=$this->Disconnect($success);
   if($success)
    $success=$disconnect_success;
  }
  return($success);
 }};?>

解决方案 »

  1.   


     够jb长的……更重要的是还没注释 ToT
     
      

  2.   

    喂,我的分呢,白金???
    下面是一个SMTP类,有注释 呵呵(好像很长):支持附件(多个),支持HTML 
    <? 
    /*********************************** 
    PHP MIME SMTP ver 1.0 Powered by Boss_ch, Unigenius soft ware co. Ltd 
    All rights reserved, Copyright 2000 ; 
    本类用 PHP 通过 smtp  sock 操作发送 MIME 类型的邮件,可以发送HTML 格式的正文、附件,采用 base64 编码 ,本版本是针对个人的发送,与多人群发版本不同的是,每发送到一个人,就重新进行一次编码,在接收端的用户看来,只是发送给他一个人的。 针对多人群发的情况,只发送一次,通过多个 RCPT TO 命令发送到不同的人信箱中, 
    说明: 
    请把 $hostname 设为你有权限的默认smtp 服务器或是在 new 时指定把 $charset 改成你的默认字符集 Html 正文中如有图片,请用绝对路径的引用 "httP://host/path/image.gif"; 并连上网,以保证程序能读取到图片的数据信息 .Html 中用到的样式表文件,请不要用 <link >之类的引用,直接把样式表定义放在<style></style>标签中 
    转载请保留此版权信息, Bugs Report : [email protected] 
    *************************************/ 
    if(!isset($__smtp_class__)){ 
    $__smtp_class__=1; 
    class smtp 

    var $hostname=""; 
    var $port=25; 
    var $connection=0; 
    var $debug=1; 
    var $timeout=30; 
    var $err_str; 
    var $err_no; 
    var $autocode=true; 
    var $charset="GB2312"; 
    var $subject=""; 
    var $body=""; 
    var $attach=""; 
    var $temp_text_body; 
    var $temp_html_body; 
    var $temp_body_images; 
    var $bound_begin="=====powered_by_boss_chen_"; 
    var $bound_end="_046484063883_====="; 
    Function smtp($server="smtp.china.com"",$port=25,$time_out=20) 
    {$this->hostname=$server; 
    $this->port=$port; 
    $this->timeout=$time_out; 
    return true; 

    Function outdebug($message) 

    echo htmlspecialchars($message)."<br>\n"; 

    function command($command,$return_lenth=1,$return_code='2') 

    if ($this->connection==0) 

    $this->err_str="没有连接到任何服务器,请检查网络连接"; 
    return false; 

    if ($this->debug) 
    $this->outdebug(">>> $command"); 
    if (!fputs($this->connection,"$command \r\n")) 

    $this->err_str="无法发送命令".$command; 
    return false; 

    else 

    $resp=fgets($this->connection,256); 
    if($this->debug) 
    $this->outdebug("$resp"); 
    if (substr($resp,0,$return_lenth)!=$return_code) 

    $this->err_str=$command." 命令服务器返回无效:".$resp; 
    return false; 

    else 
    return true; 


    Function open() 

    if($this->hostname=="") 
    {$this->err_str="无效的主机名!!"; 
    return false; 

    if ($this->debug) echo "$this->hostname,$this->port,&$err_no, &$err_str, $this->timeout<BR>"; 
    if (!$this->connection=fsockopen($this->hostname,$this->port,&$err_no, &$err_str, $this->timeout)) 

    $this->err_str="连接到 SMTP 服务器失败,错误信息:".$err_str."错误号:".$err_no; 
    return false; 

    else 

    $resp=fgets($this->connection,256); 
    if($this->debug) 
    $this->outdebug("$resp"); 
    if (substr($resp,0,1)!="2") 
    {$this->err_str="服务器返回无效的信息:".$resp."请检查SMTP服务器是否正确"; 
    return false; 

    return true; 


    Function Close() 

    if($this->connection!=0) 

    fclose($this->connection); 
    $this->connection=0; 


    Function Buildhead($from_name,$to_name,$from_mail,$to_mail,$subject) 

    if (empty($from_name)) 
    $from_name=$from_mail; 
    if (empty($to_name)) $to_name=$to_mail; 
    $this->subject="From: =?$this->charset?B?".base64_encode($from_name)."?=<$from_mail>\r\n"; 
    $this->subject.="To: =?$this->charset?B?".base64_encode($to_name)."?=<$to_mail>\r\n"; 
    $subject=ereg_replace("\n","",$subject); 
    $this->subject.="Subject: =?$this->charset?B?".base64_encode($subject)."?=\r\n"; 
    if ($this->debug) echo nl2br(htmlspecialchars($this->subject)); 
    return true; 

    Function parse_html_body($html_body=null) 

    $passed=""; 
    $image_count=0; 
    $this->temp_body_images=array(); 
    while (eregi("\<*img([^\>]+)src[[:space:]]*=[[:space:]]*([^ ]+)",$html_body,$reg)) 

    $pos=@strpos($html_body,$reg[0]); 
    $passed.=substr($html_body,0,$pos); 
    $html_body=substr($html_body,$pos+strlen($reg[0])); 
    $image_tag=$reg[2]; 
    $image_att=$reg[1]; 
    $tag_len=strlen($image_tag); 
    if ($image_tag[0]=="'" or $image_tag[0]=='"') 
    $image_tag=substr($image_tag,1); 
    if (substr($image_tag,strlen($imgage_tag)-1,1)=="'" or substr($image_tag,strlen($imgage_tag)-1,1)=='"') 
    $image_tag=substr($image_tag,0,strlen($imgage_tag)-1); 
    //echo $image_tag."<br>"; 
    $cid=md5(uniqid(rand())); 
    $cid=substr($cid,0,15)."@unigenius.com"; 
    $passed.="<img ".$image_att."src=\"cid:".$cid."\""; 
    $end_pos=@strpos($html_body,'>'); 
    $passed.=substr($html_body,0,$end_pos); 
    $html_body=substr($html_body,$end_pos); 
    // 把图片数据读出来保存到一个数据; 
    $img_file_con=fopen($image_tag,"r"); 
    unset($image_data); 
    while ($tem_buffer=AddSlashes(fread($img_file_con,16777216))) 
    $image_data.=$tem_buffer; 
    fclose($img_file_con); 
    $image_exe_name=substr($image_tag,strrpos($image_tag,'.')+1,3); 
    switch (strtolower($image_exe_name)) 

    case "jpg": 
    case "jpeg": 
    $content_type="image/jpeg"; 
    break; 
    case "gif": 
    $content_type="image/gif"; 
    break; 
    case "png": 
    $content_type="image/x-png"; 
    break; 
    case "tif": 
    $content_type="image/tif"; 
    break; 
    default: 
    $content_type="image/"; 
    break; 

    $this->temp_body_images[$image_count][name]=basename($image_tag); 
    $this->temp_body_images[$image_count][type]=$content_type; 
    $this->temp_body_images[$image_count][cid]=$cid; 
    $this->temp_body_images[$image_count][data]=$image_data; 
    $image_count++; 

    $this->temp_html_body=$passed.$html_body; 
    return true; 

    function build_content($bound_level=0,$text_body,$html_body,$hava_att=false) 

    if ($html_body) 

    if (eregi("\<*img[[:space:]]+src[[:space:]]*=[[:space:]]*([^ ]+)",$html_body,$reg)) 

    $bound_level++; 
    if ($text_body) 

    $this->body.="Content-Type: multipart/related; 
    type=\"multipart/alternative\"; 
    boundary=\""; 
    $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n"; 

    else 

    $this->body.="Content-Type: multipart/related; 
    boundary=\""; 
    $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n"; 
    } // 对于是否 text 正文 、 html正文 有没有,须有不同的 MIME 头 
    if (!$hava_att) $this->body.="This is a multi-part message in MIME format.\r\n\r\n"; 
    // 正文标识,如果是已经有附件的编码,则在正文 中不需要这一句 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->parse_html_body($html_body); 
    if ($text_body) 

    $this->body.="Content-Type: multipart/alternative; 
    boundary=\""; 
    $bound_level++; 
    $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n"; 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->body.="Content-Type: text/plain;\r\n"; 
    $this->body.="charset=\"$this->charset\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64 \r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n"; 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->body.="Content-Type: text/html;\r\n"; 
    $this->body.="charset=\"$this->charset\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64 \r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n"; 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n"; 
    $bound_level--; 

    else 

    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->body.="Content-Type: text/html;\r\n"; 
    $this->body.="charset=\"$this->charset\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64 \r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n"; 
    } //正文编码,有或没有 text 部分,编成不同的格式。 
    for ($i=0;$i<count($this->temp_body_images);$i++) 

    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->body.="Content-Type:".$this->temp_body_images[$i][type]."; 
    name=\""; 
    $this->body.=$this->temp_body_images[$i][name]."\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64\r\n"; 
    $this->body.="Content-ID: <".$this->temp_body_images[$i][cid].">\r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_body_images[$i][data])))."\r\n"; 

    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n"; 
    $bound_level--; 

    else // 有或没有图片,以上是有图片的处理,下面是没有图片的处理 

    $this->temp_html_body=$html_body; 
    if ($text_body) 

    $bound_level++; 
    $this->body.="Content-Type: multipart/alternative; 
    boundary=\""; 
    $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n"; 
    if (!$hava_att) $this->body.="\r\nThis is a multi-part message in MIME format.\r\n\r\n"; 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->body.="Content-Type: text/plain;\r\n"; 
    $this->body.="charset=\"$this->charset\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64 \r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n"; 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->body.="Content-Type: text/html;\r\n"; 
    $this->body.="charset=\"$this->charset\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64 \r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n"; 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n"; 
    $bound_level--; 

    else 

    $this->body.="Content-Type: text/html;\r\n"; 
    $this->body.="charset=\"$this->charset\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64 \r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n"; 
    } //正文编码,有或没有 text 部分,编成不同的格式。 } // end else 

    else // 如果没有 html 正文,只有 text 正文  

    $this->body.="Content-Type: text/plain; 
    charset=\"$this->charset\"\r\n"; 
    $this->body.="Content-Transfer-Encoding: base64 \r\n"; 
    $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n"; 

    } // end function default Function Buildbody($text_body=null,$html_body=null,$att=null) 

    $this->body="MIME-Version: 1.0\r\n"; 
    if (null==$att or (@count($att)==0)) //如果没有附件,查看正文的类型 ; 

    $encode_level=0; 
    $this->build_content($encode_level,$text_body,$html_body); 
    } // 如果没有附件, 
    // ******************************************************** 
    else //如果有附件, 

    $bound_level=0; 
    $this->body.="Content-Type: multipart/mixed; 
    boundary=\""; 
    $bound_level++; 
    $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n"; 
    $this->body.="This is a multi-part message in MIME format.\r\n\r\n"; 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->build_content($bound_level,$text_body,$html_body,true); // 编入正文部分 $num=count($att); 
    for ($i=0;$i<$num;$i++) 

    $file_name=$att[$i][name]; 
    $file_source=$att[$i][source]; 
    $file_type=$att[$i][type]; 
    $file_size=$att[$i][size]; 
    if (file_exists($file_source)) 

    $file_data=addslashes(fread($fp=fopen($file_source,"r"), filesize($file_source))); 
    $file_data=chunk_split(base64_encode(StripSlashes($file_data))); 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n"; 
    $this->body.="Content-Type: $file_type;\r\n name=\"$file_name\"\r\nContent-Transfer-Encoding: base64\r\n"; 
    $this->body.="Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n"; 
    $this->body.=$file_data."\r\n"; 

    } //end for 
    $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n"; 
    } // end else 
    if ($this->debug) echo nl2br(htmlspecialchars($this->body)); return true; 

    function send($from_name,$to_name,$from_mail,$to_mail,$subject,$text_body=false,$html_body=false,$att=false) 

    if (empty($from_mail) or empty($to_mail)) 

    $this->err_str="没有指定正确的邮件地址:发送人:".$from_mail."接收人:".$to_mail; 
    return false; 

    if (gettype($to_mail)!="array") 
    $to_mail=split(",",$to_mail); //如果不是数组,转换成数组,哪怕只有一个发送对象; 
    if (gettype($to_name)!="array") 
    $to_name=split(",",$to_name); //如果不是数组,转换成数组,哪怕只有一个发送对象; 
    $this->Buildbody($text_body,$html_body,$att); 
    // 所有信件的内容是一样的,可以只编一次,而对于不同的收信人,需要不同的 Head 
    if (!$this->open()) return false; 
    if (!$this->command("HELO $this->hostname",3,"250")) return false; 
    // 与服务器建立链接 
    if (!$this->open()) return false; 
    if (!$this->command("HELO $this->hostname",3,"250")) return false; 
    for ($i=0;$i<count($to_mail);$i++) 

    $this->Buildhead($from_name,$to_name[$i],$from_mail,$to_mail[$i],$subject); 
    if (!$this->command("RSET",3,"250")) return false; 
    if (!$this->command("MAIL FROM:".$from_mail,3,"250")) return false; 
    if (!$this->command("RCPT TO:".$to_mail[$i],3,"250")) return false; 
    if (!$this->command("DATA",3,"354")) return false; 
    // 准备发送邮件 
    if ($this->debug) $this->outdebug("sending subject;"); 
    if (!fputs($this->connection,$this->subject)) { $this->err_str="发送邮件头时出错!"; return false;} 
    if ($this->debug) $this->outdebug("sending body;"); 
    if (!fputs($this->connection,$this->body)) { $this->err_str="发送正文时出错!"; return false;} 
    if (!fputs($this->connection,".\r\n")) { $this->err_str="发送正文时出错!"; return false;} //正文发送完毕,退出; 
    $resp=fgets($this->connection,256); 
    if($this->debug) 
    $this->outdebug("$resp"); 
    if (substr($resp,0,1)!="2") 

    $this->err_str="发送完后,服务器没有响应!!"; 
    return false; 
    } // 发送邮件 

    if (!$this->command("QUIT",3,"221")) return false; 
    $this->close(); 
    return true; 

    }//end class define 
    }//end if(!isset($__smtp_class__)) 
    ?> 
      

  3.   

    类怎么用啊?(初学)######################################################################################
    我用 IMAP 函数做的:  Login.php<?
    session_start();
    session_register("account","password");
    ?>
    <html>
    <head>
    <title>xxx 邮局</title>
    <style type="text/css">
    <!--
    .pt9 {  font-family: "宋体"; font-size: 9pt}
    body { font-family: "宋体"; font-size: 9pt; margin-top: 0px }
    a {  font-size: 9pt; color: #0000FF; text-decoration: none}
    td { font-family: "宋体"; font-size: 9pt }
    th {font-size: 9pt }
    a:hover {  color: #666666; text-decoration: underline}
    -->
    </style>
    </head>
    <body topmargin="0" leftmargin="0">
    <img src="images/mail.jpg" border="0">
    <br>
    <?$boxname = "{125.125.125.1}";//局域网,网卡的IP地址设为 125.125.125.1
    $stream = imap_open($boxname,$account,$password) or die("无效的帐号或密码。");
    echo "$account , 欢迎您使用 xxx 邮局";
    ?>
    <br><table border="0" width="100%" cellspacing="0" cellpadding="0">     
      <tr>     
        <td width="33%">
    <a href="listmail.php"><img border=0 src="images/recvmail.gif">
        <td width="33%">
            <a href="sendmail.php"><img border=0 src="images/sendmail.gif">
        <td width="33%">
            <a href="recycle.php"><img border=0 src="images/recycle.gif">
      </tr>
    <hr>
    </table>
    <?
    $mail_number = imap_num_msg($stream);if($mail_number)
    {
    echo "<table border=0 width=\"100%\">";
    echo "<th bgcolor=ccccff width=\"5%\">No.</th><th bgcolor=ccccff width=\"30%\">标题</th><th bgcolor=ccccff width=\"35%\">发信人</th><th bgcolor=ccccff width=\"20%\">日期</th><th bgcolor=ccccff width=\"10%\">大小</th>";
    for($n=1;$n<=$mail_number;$n++)
    {
    $sum_number++;
    if($color=="ddeebb")$color="ffffff";
    else $color=ddeebb;
    $mail_header = imap_headerinfo($stream,$n);
    $subject = $mail_header->subject;
    if(strpos($subject,"=?gb2312?B?")===0)
    {
    $subject=substr_replace($subject,'',0,10);
    $subject=substr_replace($subject,'',-2);
    $subject=imap_base64($subject);
    }
    $from = $mail_header->fromaddress;
    if(strpos($from,"=?gb2312?B?")===0)
    {
    $from = substr_replace($from,'',0,11);
    $from_host = strstr($from,"<");
    $from = substr($from,0,strpos($from,"?="));
    $from = imap_base64($from)." ".$from_host;
    } $from = htmlspecialchars($from);
    $fromarray = $mail_header->from;
    if(is_array($fromarray))
    while(list(,$v)=each($fromarray))
    $from = "<a href=\"sendmail.php?to=".$v->mailbox."@".$v->host."\">".$from."</a>";
    $date = $mail_header->date;
    $date = substr($date,5,17);
    $size = $mail_header->Size;
    $sum_size  = $sum_size + $size;
    $size = $size / 1000;
    $size = number_format($size,0); if(!$subject)$subject="(none)";
    echo "<tr><td bgcolor=$color width=\"5%\">$i</td><td bgcolor=$color width=\"30%\"><a href=readmail.php?number=$n>$subject</a></td><td bgcolor=$color width=\"35%\">$from</td><td bgcolor=$color width=\"20%\">$date</td><td align=right bgcolor=$color width=\"10%\">${size}K</td></tr>";
    }
    $sum_size = number_format($sum_size/1000,0);
    echo "</table>  共 $sum_number 封信 ${sum_size}KB";
    }
    else echo "没有信件";
    imap_close($stream);include "footer.inc";
    ?>####################################################################################
    我的读信部分也是用的 IMAP ,但我希望能做出 263 那样的效果。(有文字、有图片...)另 ,imap_mail()的具体用法是什么?
      

  4.   


     分我已经给你了啊? http://www.csdn.net/expert/topic/501/501574.shtm
     
      

  5.   

    参考 horde 的imp吧
     http://www.horde.org
      

  6.   

    horde 的 imp 不错。