mail(to,subject,message,headers,parameters)
From: [email protected]是headers 邮件头信息php mail()的参考文档
http://cn.php.net/manual/zh/ref.mail.php#ini.smtp
希望对你有帮助

解决方案 »

  1.   

    我本地ip地址是192.168.0.22。那么在php.ini中的smtp=192.168.0.22对吗?
    我用的是mail direct pro服务,windows系统
    能否给出如何写mail()处的代码和有关php.ini相关配置。
    在此谢过!!!!!!!!!!
      

  2.   

    php.ini的配置是这样的[mail function]
    ; For Win32 only.
    SMTP = mail.smtp.com
    smtp_port = 25
    ; For Win32 only.
    sendmail_from = [email protected]
    mail()的例子是这样的
    <?php
    $to      = '[email protected]';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: [email protected]' . "\r\n" .
        'Reply-To: [email protected]' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();mail($to, $subject, $message, $headers);
    ?>如果发不出去 
    在网上有php发邮件的类吧
      

  3.   

    我不明白,是不是运行了该php页,就会把message信息发送到[email protected]邮箱里,那我要发送到[email protected]是不是将此处改一下就行了?
    [email protected]是发件人的邮箱名吗?这个邮箱从哪来?是按完mail direct pro服务后服务自带的吗?
    整个发件过程不太清楚,其高手解答一下
      

  4.   

    会发送到邮箱去的,那是头信息。
    在邮件解析的时候
    除了From,to ,subject,body外还有个head
    先会解析head.在邮件的属性里面都可以找到头信息
    写着从哪里来,到哪里去,回复到哪里等等
    只是作为一个标示。一些服务器会根据这些去判断是否
    为垃圾邮件。
    如果你要发送到[email protected]
    $headers='From:[email protected] Reply-To: [email protected]';
    $subject='hello';
    $message='are you ok?';
    mail('[email protected]', $subject, $message, $headers);
    在php.ini里面配置真正的邮箱用户
    [mail function]
    ; For Win32 only.
    SMTP = mail.smtp.com
    smtp_port = 25
    ; For Win32 only.
    sendmail_from = [email protected]
      

  5.   

    非常感谢您细致的讲解,我已经明白了一些。
    但是高手,我现在已经申请了[email protected]这个邮箱,但是你写的[email protected] 这个是发件人的邮箱吗?他是怎样来的?申请的吗?
    我现在都晕了!!!!!!
    我现在是windowsxp系统,用的是系统自带的stmp服务,申请了一个[email protected]邮箱,用来接收信息。
    我现在想把信息发到[email protected]邮箱里,要怎么做啊!!!。php.ini怎样写啊!!!
    我刚学php网上写的根本看不懂!!高手能写的详细写吗,拜托!!!
      

  6.   

    [email protected] 是发件的人
    只是一个例子而已
    你申请的是什么,就填什么吧!
    个人认为还是在网上有php发邮件的类吧
    这样免去在php.ini里面的配置
    我有个例子 人家写的smtp类,调用这个类就可以了.
    例子如下
      

  7.   

    文件名 smtp.php 
    <?php
    class smtp
    { /* Public Variables */
        var $smtp_port;
        var $time_out;
        var $host_name;
        var $log_file;
        var $relay_host;
        var $debug;
        var $auth;
        var $user;
        var $pass;
    /* Private Variables */
    var $sock;
    /* Constractor */
    function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
     {
       $this->debug = FALSE;
       $this->smtp_port = $smtp_port;
       $this->relay_host = $relay_host;
       $this->time_out = 30; //is used in fsockopen()
       $this->auth = $auth;//auth
       $this->user = $user;
       $this->pass = $pass;
       $this->host_name = "localhost"; //is used in HELO command
       $this->log_file = "";
       $this->sock = FALSE;
    }
        /* Main Function */
        function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
        {   $mail_from = $this->get_address($this->strip_comment($from));
            $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
            $header = "MIME-Version:1.0\r\n";
            if($mailtype=="HTML"){
                $header .= "Content-Type:text/html\r\n";
            }
            $header .= "To: ".$to."\r\n";
            if ($cc != "") {
                $header .= "Cc: ".$cc."\r\n";
            }
            $header .= "From: $from<".$from.">\r\n";
            $header .= "Subject: ".$subject."\r\n";
            $header .= $additional_headers;
            $header .= "Date: ".date("r")."\r\n";
            $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
    list($msec, $sec) = explode(" ", microtime());
     $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
      $TO = explode(",", $this->strip_comment($to));
     if ($cc != "") {
                $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
            }
        if ($bcc != "") {
                $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
     }
            $sent = TRUE;
     foreach ($TO as $rcpt_to) {
                $rcpt_to = $this->get_address($rcpt_to);
    if (!$this->smtp_sockopen($rcpt_to)) {
                    $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
                    $sent = FALSE;
                    continue;
     }
    if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {          $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
                } else {
                    $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
                    $sent = FALSE;
                }
                fclose($this->sock);
                $this->log_write("Disconnected from remote host\n");
            }
    return $sent;
        }
    /* Private Functions */
        function smtp_send($helo, $from, $to, $header, $body = "")
        {
            if (!$this->smtp_putcmd("HELO", $helo)) {            return $this->smtp_error("sending HELO command");
            }
            #auth
            if($this->auth){
                if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
                    return $this->smtp_error("sending HELO command");
                }            if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
                    return $this->smtp_error("sending HELO command");
                }
            }
            if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
                return $this->smtp_error("sending MAIL FROM command");
            }
            if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
                return $this->smtp_error("sending RCPT TO command");
            }
            if (!$this->smtp_putcmd("DATA")) {
                return $this->smtp_error("sending DATA command");
            }
            if (!$this->smtp_message($header, $body)) {
                return $this->smtp_error("sending message");
            }
            if (!$this->smtp_eom()) {
             return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
           }
           if (!$this->smtp_putcmd("QUIT")) {
               return $this->smtp_error("sending QUIT command");
           }
           return TRUE;
       }
        function smtp_sockopen($address)
        {
            if ($this->relay_host == "") {
                return $this->smtp_sockopen_mx($address);
            } else {
                return $this->smtp_sockopen_relay();
            }
        }
       function smtp_sockopen_relay()
        {
            $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
            $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
            if (!($this->sock && $this->smtp_ok())) {
                $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
                $this->log_write("Error: ".$errstr." (".$errno.")\n");
                return FALSE;
            }
            $this->log_write("Connected to relay host ".$this->relay_host."\n");
            return TRUE;
        }
        function smtp_sockopen_mx($address)
        {
            $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
            if (!@getmxrr($domain, $MXHOSTS)) {
                $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
                return FALSE;
            }
            foreach ($MXHOSTS as $host) {
                $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
                $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
                if (!($this->sock && $this->smtp_ok())) {
                 $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
                 $this->log_write("Error: ".$errstr." (".$errno.")\n");
                 continue;
               }
               $this->log_write("Connected to mx host ".$host."\n");
               return TRUE;
            }
            $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
            return FALSE;
        }
        function smtp_message($header, $body)
        {
            fputs($this->sock, $header."\r\n".$body);
            $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
            return TRUE;
        }
        function smtp_eom()
        {    fputs($this->sock, "\r\n.\r\n");
            $this->smtp_debug(". [EOM]\n");
            return $this->smtp_ok();
        }
        function smtp_ok()
        {
            $response = str_replace("\r\n", "", fgets($this->sock, 512));
            $this->smtp_debug($response."\n");
            if (!ereg("^[23]", $response)) {
                fputs($this->sock, "QUIT\r\n");
                fgets($this->sock, 512);
                $this->log_write("Error: Remote host returned \"".$response."\"\n");
                return FALSE;
            }
            return TRUE;
        }
        function smtp_putcmd($cmd, $arg = "")
        {
            if ($arg != "") {
                if($cmd=="") $cmd = $arg;
                else $cmd = $cmd." ".$arg;
            }
            fputs($this->sock, $cmd."\r\n");
            $this->smtp_debug("> ".$cmd."\n");
            return $this->smtp_ok();
        }
        function smtp_error($string)
        {
            $this->log_write("Error: Error occurred while ".$string.".\n");
            return FALSE;
        }
        function log_write($message)
        {
            $this->smtp_debug($message);
            if ($this->log_file == "") {
               return TRUE;
            }
            $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
            if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
                $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
                return FALSE;
            }
            flock($fp, LOCK_EX);
            fputs($fp, $message);
            fclose($fp);
            return TRUE;
        }
      

  8.   

        function strip_comment($address)
        {
            $comment = "\([^()]*\)";
            while (ereg($comment, $address)) {
                $address = ereg_replace($comment, "", $address);
            }
            return $address;
        }
        function get_address($address)
        {
            $address = ereg_replace("([ \t\r\n])+", "", $address);
            $address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
            return $address;
        }
        function smtp_debug($message)
        {
            if ($this->debug) {
            echo $message;
            }
        }
    }?>
    调用smtp类
    <?php
    require ("smtp.php");  
    $smtpserver = "mail.xxxx.xxxx";//SMTP服务器
    $smtpserverport =25;//SMTP服务器端口
    $smtpusermail = "[email protected]";//SMTP服务器的用户邮箱
    $smtpemailto = "[email protected]";//发送给谁
    $smtpuser = "[email protected]";//SMTP服务器的用户帐号
    $smtppass = "xxxx";//SMTP服务器的用户密码
    $mailsubject = "[email protected]";//邮件主题
    $mailbody = "<h1>This is a test mail</h1>";//邮件内容
    $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
    $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 
    $smtp->debug = 1;//是否显示发送的调试信息 1=?ture 0=>faile  
    $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 
    ?>
    试一下吧!把你自己的邮箱配置填在里面,就可以发邮件了
      

  9.   

    您回答的那么详细,可我太笨!!!还要请教您
    我现在在本地安装了windows自带的smtp服务,又也下载了mail direct pro服务器软件。
    我看网上说在命令行里输入telnet localhost 25就可以查处本地smtp服务是否可用,但我输入后没任何反应,如果把端口号写成26它会报错,那它到底能不能用啊!!。
    您给我的代码中SMTP服务器的用户邮箱 ,这和SMTP服务器有关吗?
    如果我想用163邮箱往新浪邮箱发信息。是不是163邮箱邮箱就是你所说的SMTP服务器的用户邮箱吗?
     
      

  10.   

    smtp.163.com 
    这是发163.com邮箱的smtp 
    端口为 25 默认的用户邮箱 是你的 用户名 如:  [email protected] localhost 25
    是检测本地的25端口是否开启
    如果能进入,屏幕上是只有光标而已像邮箱服务器软件,都应该会有这些配置 或则 选项的
      

  11.   

    因为给的代码就是配php.ini的,我只需要运行出来就行了。但我现在老是报错,请高手帮我看看!!!!!php页代码:
    $to      = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
        'Reply-To: [email protected]' . "\r\n" . 
        'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ==============================================================
    php.ini中[mail function]
    ; For Win32 only.
    SMTP =smtp.163.com
    smtp_port = 25; For Win32 only.
    ;sendmail_from [email protected]====================================================================
    究竟是那的错,是配置错误,还是本地邮件服务不能用?
    我在命令行写 telnet localhost 25查看本地邮件服务是否好用,但没任何反应。
      

  12.   

    ;sendmail_from [email protected] 把前面的 ; 去掉
    不知道需不需要密码 我想还应该填上密码
    这样认证才会通过
      

  13.   

    ;sendmail_from [email protected]
    前面的分号已经去掉了。
    还是不好用
      

  14.   

    $to      = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
        'Reply-To: [email protected]' . "\r\n" . 
        'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ==============================================================
    php.ini中[mail function]
    ; For Win32 only.
    SMTP =smtp.163.com
    smtp_port = 25
    sendmail_from [email protected]
     重启一下apache
    如果是windows下的可能你还得找下sendmail在什么地方 然后 再加上 
    sendmail_path= ......... 
      

  15.   

    $to      = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
        'Reply-To: [email protected]' . "\r\n" . 
        'X-Mailer: PHP/' . phpversion(); $send=mail($to, $subject, $message, $headers); 
    if($send)
    {
      echo 'sucessful';
    }else
    {
       echo 'failed';
    }这样检查一下发送成功没 , 
    或则 mail()有个debug 把debug设置为1 检查一下发送的信息
      

  16.   

    我现在不报错了,但是发出的信息都收不到。
    还有你说的那个sendmail文件,一个在windows/system32的sendmail.dll文件。
    一个是在我开发工具中的sendmail.php。
    他俩的后缀名不一样,那在sendmail_path=处,写的是那一个文件的路径阿!!!
    请高手在指点指点!!!!!!!!!!
      

  17.   

    我现在想从[email protected]发信到[email protected]
    运行页面显示成功了。但[email protected]邮箱没信息!!!高手,帮人帮到底吧!!!我代码如下
    php代码:$to      = '[email protected]';  
    $subject = 'the subject';  
    $message = 'hello';  
    $headers = 'From: [email protected]' . "\r\n" .  
        'Reply-To: [email protected]' . "\r\n" .  
        'X-Mailer: PHP/' . phpversion();  $send=mail($to, $subject, $message, $headers);
    if($send) 

      echo 'sucessful'; 
    }else 

       echo 'failed'; 
    }
    ?> 
    ==========================================
    php.ini文件:[mail function]
    ; For Win32 only.
    SMTP =smtp.163.com
    smtp_port = 25; For Win32 only.
    sendmail_from [email protected]; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    sendmail_path =C:\WINDOWS\system32\
      

  18.   

    看了很多列子,做了就会出 Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in,,,应该怎么弄呢