同样一段代码 两台机器配的php环境全一样 php.ini也全一样。可是 在公司的机器上就能发mail 可在家就发不了 这是什么原因啊。

解决方案 »

  1.   

    家里的IP是不是真实IP啊?
    公司的机器一般的都是真实对外发的,如果不是那可就没有什么好办法,端口映射可能可以,我指的是代理上网!
      

  2.   

    你能保证 两台机器配的php环境全一样 ????
    那是不可能的。
      

  3.   

    今天下午刚在csdn找到了一个
    用了一下还不错
    支持附件
    <?php
    // 需要 php_sockets.dll 扩展模块
    set_time_limit(120); class smtp_mail
    {
    var $host; //主机
    var $port; //端口 一般为25
    var $user; //SMTP认证的帐号
    var $pass; //认证密码
    var $debug = false; //是否显示和服务器会话信息
    var $conn;
    var $result_str; //结果
    var $in; //客户机发送的命令
    var $from; //源信箱
    var $to; //目标信箱
    var $subject; //主题
    var $body; //内容
    var $auth=false;
    var $content_type = "text/plain"; // 调试函数
    function debug_show($strMessage)
    {
    if($this->debug)
    {
    echo $strMessage."<p>\r\n";
    }
    } // 功能:实现SMTP命令的发送
    function docommand()
    {
    socket_write ($this->socket, $this->in, strlen ($this->in));
    $this->debug_show("客户机命令:".$this->in);
    $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
    $this->debug_show($this->result_str);
    } // 功能:初始化SMTP邮件发送
    function smtp_mail($host,$port,$user,$pass,$auth=false,$debug=false)
    {
    $this->host   = $host;
    $this->port   = $port;
    $this->user   = base64_encode($user);
    $this->pass   = base64_encode($pass);
    $this->debug  = $debug;
    $this->auth   = $auth; // 初始化SOCKET
    $this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
    if($this->socket)
    {
    $this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
    $this->debug_show($this->result_str);
    }
    else
    {
    exit("创建SOCKET失败,请检查您的网络连接和参数");
    } // 建立到SMTP服务器的TCP连接
    $this->conn = socket_connect($this->socket,$this->host,$this->port);
    if($this->conn)
    {
    $this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
    $this->debug_show($this->result_str);
    }
    else
    {
    exit("创建SOCKET连接失败,请检查您的网络连接和参数");
    }
    $this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
    $this->debug_show($this->result_str);
    } // 功能:实现真正的邮件传送
    function send($from,$to,$subject,$body,$attache="")
    {
    if($from == "" || $to == "")
    {
    exit("请输入信箱地址");
    }
    if($subject == "")
    {
    $sebject = "无标题";
    }
    if($body == "")
    {
    $body = "无内容";
    } $this->from = $from;
    $this->to = $to;
    $this->subject = $subject;
    $this->body = $body; $All = "From:".$this->from."\r\n";
    $All.= "To:".$this->to."\r\n";
    $All.= "Subject:".$this->subject."\r\n";
    $All.= "Mime-Version: 1.0\r\n"; // 判断是否有附件
    if(is_array($attache))
    {
    // 发送含有附件的内容
    $boundary = "===" . md5(uniqid("")) . "==="; $All.= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"\r\n\r\n";
    $All.= "This is a multi-part message in MIME format.\r\n\r\n";
    $All.= "--" . $boundary . "\r\n";
    $All.= "Content-Type: ". $this->content_type ."\r\n";
    $All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $All.= $this->body . "\r\n\r\n"; while(list($key, $val) = each($attache))
    {
    $fd = fopen($val, "r") or die("不能打开文件".$val."!\r\n");
    $contents = chunk_split(base64_encode(fread($fd,filesize($val))));
    fclose($fd);
    $All.= "--" . $boundary . "\r\n";
    $All.= "Content-Type: application/octet-stream; name=\"".basename($val)."\"\r\n";
    $All.= "Content-Transfer-Encoding: BASE64\r\n";
    $All.= "Content-Disposition: attachment; filename=\"".basename($val)."\"\r\n\r\n";
    $All.= $contents . "\r\n";
    }
    $All.= "--" . $boundary . "--\r\n";
    }
    else
    {
    // 发送无附件的内容
    $All.= "Content-Type: ".$this->content_type."\r\n";
    $All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $All.= $this->body;
    } //以下是和服务器会话
    $this->in       =  "EHLO HELO\r\n";
    $this->docommand(); // 是否需要验证
    if($this->auth)
    {
    $this->in       =  "AUTH LOGIN\r\n";
    $this->docommand(); $this->in       =  $this->user."\r\n";
    $this->docommand(); $this->in       =  $this->pass."\r\n";
    $this->docommand();
    } $this->in       =  "MAIL FROM:".$this->from."\r\n";
    $this->docommand(); $this->in       =  "RCPT TO:".$this->to."\r\n";
    $this->docommand(); $this->in       =  "DATA\r\n";
    $this->docommand(); $this->in       =  $All."\r\n.\r\n";
    $this->docommand(); //结束,关闭连接
    $this->in       =  "QUIT\r\n";
    $this->docommand();
    }
    }
    ?>
      

  4.   

    使用
    require_once("../lib/testmail.php");$smtp = new smtp_mail("smtp服务器地址","25","帐号","密码",true,false);
    $smtp->send("发送人","收件人信箱","主题:HELLO","内容:THIS IS A TEST MAIL",array("../test/!00004.pdg"));
      

  5.   

    好的,我回家试试家里的能不能用。谢谢以上所有的老兄们。to  pswdf(小邪)  真的是一样一样的啊。用的同一个包 配的也全一样to ekingelon(顽石心) 家里是真的ip 公司的机器不是。。可是公司的用用呵呵。这个不都是设的是 smtp 的真实地址就可了吗
      

  6.   

    to feixuehenshui(飞雪恨水)   你的这个代码 我在公司设好后运行 不报错 可是mail没有发出。   我在家中运行 报错Warning: socket_connect() unable to connect [0]: 在一个非套接字上尝试了一个操作。 in E:\armiroot\smtp\my.php on line 63
    创建SOCKET连接失败,请检查您的网络连接和参数
      

  7.   

    http://www.phpe.net/class/36.shtml
    或许有用,试试吧!这个我的是可以的。
      

  8.   

    //使用phpMailer的例子
    require("mail.inc.php");// Instantiate your new class
    $mail = new my_phpmailer;// Now you only need to add the necessary stuff
    $mail->AddAddress("[email protected]", "Josh Adams");
    $mail->Subject = "Here is the subject";
    $mail->Body    = "This is the message body";
    $mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip");  // optional nameif(!$mail->Send())
    {
       echo "There was an error sending the message";
       exit;
    }echo "Message was sent successfully";