“mail函数是不是只有在可以匿名使用的服务器上才能发送电子邮件”
是的!

解决方案 »

  1.   

    可以使用PEAR库中的mail.php包,
    包括工厂方法:object &factory (string $backend [, array $param = array()])创建mailer实例
    $backend:"mail","smtp", "sendmail" 
    $param : mail sendmail smtp 
    和boolean send (mixed $recipients, array $headers, string $body)
    具体应用可以再参考手册的详细信息
      

  2.   

    <?php
    class text_mail{
    var $reply;
    var $to;
    var $from;
    var $subject;
    var $body; function text_mail(){
    $this->to="";
    $this->from="";
    $this->subject="";
    $this->body="";
    $this->reply="";
    } function get_mail(){
    $mail="From: $this->from \r\n";
    $mail.="Reply-To: $this->reply \r\n";
    $mail.="To: $this->to \r\n";
    $mail.="Subject: $this->subject \r\n";
    $mail.="Mime-verson:1.0 \r\n";
    $mail.="Content-Type: text/plain;charset=\"GB2312\"\r\n";
    $mail.="Content-Transfer-Encoding: 8bit\r\n";
    $mail.="\r\n";
    $mail.=str_replace("\n.\r\n","\n. \r\n",$this->body);
    return $mail;
    }
    };
    ?>
    <?php
    #===============inc/smtp_mail.class.php==============
    class smtp_mail{
    var $fp=false;
    var $lastmsg="";
    var $authMode=false;
    var $authLastLine;
    var $debugMode=false;
    var $authLoginName;
    var $authLoginPassword; function read_line(){
    $ret=false;
    $line=fgets($this->fp,1024);
    if($this->debugMode){
    echo "RESPONSE: $line <br>";
    flush();
    }
    if(ereg("^([0-9]+).(.*)$",$line,$data)){
    $recv_code=$data[1];
    $recv_msg=$data[2];
    $ret=array($recv_code,$recv_msg);
    }
    return $ret;
    } function dialogue($code,$cmd,$response=false){
    $ret=true;
    if($this->debugMode){
    echo "CWD: $cmd <br>";
    flush();
    }
    fwrite($this->fp,$cmd."\r\n");
    $line=$this->read_line();
    if($line==false){
    $ret=false;
    $this->lastmsg="";
    }
    else{
    $this->lastmsg="$line[0] $line[1]";
    if($line[0]!=$code){
    $ret=false;
    return $ret;
    }
    if($response!==false){
    $response.="\r\n";
    while($line[1]!=$response){
    $line=$this->read_line();
    // $this->lastmsg.="<br>$line[0] $line[1]";
    if($line[0]!=$code){
    $ret=false;
    return $ret;
    }
    }
    }
    return $ret;
    }
    } function error_message(){
    echo "SMTP Protocol failure (".$this->lastmsg.").<br>";
    flush();
    } function crlf_encode($data){
    $data.="\n";
    $data=str_replace("\n","\r\n",str_replace("\r","",$data));
    $data=str_replace("\n.\r\n","\n. \r\n",$data);
    return $data;
    } function handle_email($from,$to,$data){
    $rcpts=explode(",",$to);
    $err=false;
        if(!$this->dialogue(250,"HELO")){
    $err=true;
    }
        if(!$this->dialogue(250,"EHLO",$this->authLastLine)){
    $err=true;
    }
    if($this->authMode){
    if(!$this->dialogue(334,"AUTH LOGIN")) {
    $err=true;
    }
    if(!$this->dialogue(334,base64_encode($this->authLoginName))){
    $err=true;
    }
    if(!$this->dialogue(235,base64_encode($this->authLoginPassword))){
    $err=true;
    }
    }
    if(!$this->dialogue(250,"MAIL FROM: $from") ){
    $err=true;
    }
    for($i=0;!$err&&$i<count($rcpts);$i++){
    if(!$this->dialogue(250,"RCPT TO: $rcpts[$i]")){
    $err=true;
    }
    }
    if($err||!$this->dialogue(354,"DATA")||!fwrite($this->fp,$data)||!$this->dialogue(250,"\r\n.")||!$this->dialogue(221,"QUIT") ){
    $err=true;
    }
    if($err){
    $this->error_message();
    }
    return !$err;
    } function connect($hostname){
    if($this->debugMode){
    echo "CONNECTING $hostname ...";
    }
    $ret=false;
    $this->fp=fsockopen($hostname,25);
    if($this->fp){
    $ret=true;
    if($this->debugMode){
    echo "Succeed!<br>";
    flush();
    }
    }
    else{
    if($this->debugMode){
    echo "Failed!<br>";
    flush();
    }
    }
    return $ret;
    } function send_email($hostname,$from,$to,$data,$crlf_encode=1){
    if(!$this->connect($hostname)){
    echo "cannot open connection!<br>\n";
    flush();
    return false;
    }
    $line=$this->read_line();
    $ret=false;
    if($line&&$line[0]=="220"){
    if($crlf_encode){
    $data=$this->crlf_encode($data);
    }
    $ret=$this->handle_email($from,$to,$data);
    }
    else{
    $this->error_message();
    }
    fclose($this->fp);
    return $ret;
    }
    };
    ?>
    <?php
    #===================testmail.php======================
    //include "inc/mime_mail.php";
    set_time_limit(100);
    #include "inc/text_mail.class.php";
    #include "inc/smtp_mail.class.php";$smtp_server="218.11.169.11";  //这里为smtp服务器地址.
    $from="[email protected]";       //发信人
    $to="[email protected]";   //收信人
    $subject="It Is A Test!";  //题目
    $body="test only! 
    fdsafsdajfkljdklsfsda
    fds
    fdsaf
    dsfsdafds
    f";   //内容$mail=new text_mail;  //初始化mail主体
    $mail->from=$from;
    $mail->to=$to;
    $mail->subject=$subject;
    $mail->body=$body;
    $mail->reply=$from;
    $data=$mail->get_mail();  
    $smtp=new smtp_mail;    //初始化smtp_mail类
    $smtp->debugMode=false;  //debug模式
    $smtp->authMode=false;   //smtp服务器验证模式(如果服务器要验证才需要)
    # 这里几行是当服务器需要验证的时候才用上.
    $smtp->authLastLine="OK";  //ehlo命令列出的最后一行的文字
    $smtp->authLoginName="admin";
    $smtp->authLoginPassword="admin147@";$smtp->send_email($smtp_server,$from,$to,$data);  //发送邮件
    ?>最后出现错误:SMTP Protocol failure (250 , Sender ok ).
    谁知道?
      

  3.   

    改造显示错误函数之后出现:
    SMTP Protocol failure (handle_email221,"QUIT":503 Unexpected command or sequence of commands ).