找一个不需要认证的smtp服务器

解决方案 »

  1.   

    class Mail_SMTP
    {
    var $host     = '127.0.0.1'; // SMTP主机名
    var $port     = 25; // 主机的smtp端口,一般是25号端口
    var $timeout  = 5; // 连接主机的最大超时时间 
    var $debug = 0; // 做为标识,是否在调试状态,是的话,输出调试信息
    var $showerr  = true;
    var $handle;function Mail_SMTP($server = '127.0.0.1', $port = 25, $time_out = 5){
    $this ->host       = $server;
    $this ->port       = $port;
    $this ->timeout    = $time_out;
    return true;
    }function errmsg($err,$show=false){
    if ($this->showerr || $show){
    echo $err."<br>";
    exit();
    }
    }function _command($cmd, $response){
    if (!(fputs($this->handle,$cmd . "\r\n"))){
    $this->errmsg("Couldn't send command to server");
    return false;
    }
       while ( substr($server_response,3,1) != ' ' ){
          if( !( $server_response = fgets($this->handle, 256))){
             $this->errmsg("Couldn't get mail server response codes");
          }
       }   if( !( substr($server_response, 0, 3) == $response )){
          $this->errmsg( "Ran into problems sending Mail. Response: $server_response");
       }
    }function connect($user = '',$pass = ''){
    if( !$this->handle = fsockopen($this->host, 25, $errno, $errstr, 20)){
    $this->errmsg( "Could not connect to smtp host : $errno : $errstr");
    }
    $r = fgets($this->handle, 256);
    if ( substr($r, 0, 3) != "220"){
    $this->errmsg("couldn't connection to smtp server");
    return false;
    } if( $user != '' && $pass != ''){
    // Send the RFC2554 specified EHLO.
    // This improved as provided by SirSir to accomodate
    // both SMTP AND ESMTP capable servers
    $this->_command("EHLO \"" . $this->host . "\"", "250");
    $this->_command("AUTH LOGIN","334");
    $this->_command(base64_encode($user), "334");
    $this->_command(base64_encode($pass), "235");
    }
    else{
    // Send the RFC821 specified HELO.
    $this->_command("HELO \"" . $this->host . "\"", "250");
    }
    }function sendmail($header, $body = ''){
    //header
    if (isset($header)){
    if(is_array($header)){
    if(sizeof($header) > 1){
    $header = join("\r\n", $header);
    }
    else{
    $header = $header[0];
    }
    }
    $header = chop($header); $header = preg_replace("/(?<!\r)\n/si", "\r\n", $header); $header_array = explode("\r\n", $header);
    @reset($header_array);
    $headers = "";
    while( list(, $header) = each($header_array)){
    if ( preg_match("/^from:/si", $header)){
    $from = preg_replace("/^from:(.*)/si", "\\1", $header);
    }
    elseif ( preg_match("/^to:/si", $header)){
    $to = preg_replace("/^to:(.*)/si", "\\1", $header);
    }
    elseif ( preg_match("/^subject:/si", $header)){
    $subject = preg_replace("/^subject:(.*)/si", "\\1", $header);
    }
    elseif ( preg_match("/^cc:/si", $header)){
    $cc = preg_replace("/^cc:(.*)/si", "\\1", $header);
    }
    elseif( preg_match("/^bcc:/si", $header )){
    $bcc = preg_replace("/^bcc:(.*)/si", "\\1", $header);
    $header = "";
    continue;
    }
    $headers .= $header . "\r\n";
    }
    $headers = chop($headers);
    }//body
    $body = preg_replace("/(?<!\r)\n/si", "\r\n", $body); if(trim($to) == ""){
    $this->errmsg("No email address specified");
    }
    if(trim($subject) == ""){
    $this->errmsg( "No email Subject specified");
    } $to  = explode(",", $to);
    $cc  = explode(",", $cc);
    $bcc = explode(",", $bcc); $this->_command("MAIL FROM: " . $from, "250"); @reset( $to );
    while( list( , $to_address ) = each( $to )){
    $to_address = trim($to_address);
    if ( preg_match('/[^ ]+\@[^ ]+/', $to_address)){
    $this->_command("RCPT TO: <$to_address>", "250");
    }
    }
    // Ok now do the CC and BCC fields...
    @reset( $bcc );
    while( list( , $bcc_address ) = each( $bcc )){
    $bcc_address = trim( $bcc_address );
    if ( preg_match('/[^ ]+\@[^ ]+/', $bcc_address)){
    $this->_command("RCPT TO: <$bcc_address>", "250");
    }
    }
    @reset( $cc );
    while( list( , $cc_address ) = each( $cc )){
    $cc_address = trim( $cc_address );
    if ( preg_match('/[^ ]+\@[^ ]+/', $cc_address)){
    $this->_command("RCPT TO: <$cc_address>", "250");
    }
    }
    // Ok now we tell the server we are ready to start sending data
    $this->_command("DATA", "354");
    $bodys = $headers . "\r\n\r\n" . $body . "\r\n";
    fputs($this->handle, $bodys);
    $this->_command("\r\n.", "250");
    return TRUE;
    }
    function quit(){
    // Now tell the server we are done and close the socket...
    fputs($this->handle, "QUIT\r\n");
    fclose($this->handle);
    }
    }//end class