我在本机WINDOWS下建立了个虚拟机,电脑是在LAN中,不作为独立主机,请问这样是不是就无法用mail发送邮件?如下代码
<?php
mail("[email protected]","test","123");
?>执行时不提示错误,说明Mail函数是被支持,请教原因,谢谢!

解决方案 »

  1.   

    phpinfo()看看有没有开启mail函数。还有请确认你的服务器能发送邮件。
      

  2.   

    即使mail返回true,也不一定表示邮件被对方接收。
      

  3.   


    这样是没有办法发出邮件的,你需要一个SMTP服务器IP地址(包括端口号),还需要有相应的邮箱和密码,例如可以使用QQ邮箱来发送邮件,给个参考文章: 使用PHP发送邮件的两种方法
    今天研究了一下使用PHP来发送电子邮件,总结了一下,有这么两种方法: 一、使用PHP内置的mail()函数看了一下手册,就直接开始写代码了,如下 view plain
    $to = "[email protected]";  
    $subject = "Test";  
    $message = "This is a test mail!";  
    mail($to,$subject,$message);  
      结果就直接报错,如下:Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:/www/Zend/email/email.php on line 10 看来本地需要有SMTP服务器,那就使用别人的试试吧,又改了下代码:view plain
    $to = "[email protected]";  
    $subject = "Test";  
    $message = "This is a test mail!";  
    ini_set('SMTP','smtp.163.com');  
    ini_set('smtp_port',25);  
    ini_set('sendmail_from',"[email protected]");  
    mail($to,$subject,$message);  
     结果还是错误:Warning: mail() [function.mail]: SMTP server response: 553 authentication is required,smtp2,DNGowKD7v5BTDo9NnplVBA--.1171S2 1301220947 in D:/www/Zend/email/email.php on line 9 看来是需要验证信息,怎么写验证信息呢?在哪里配置呢?上网找了半天也没找出个所以然,最后看了别人一些技术文章后得出结论(由于对SMTP邮件什么的不是非常了解,所以也不知道这个结论是否是正确的):使用mail()函数发送邮件就必须要有一台无需SMTP验证就可以发信的邮件服务器。但现在的SMTP邮件服务器基本上都是需要验证的,所以要想使用它发邮件就只能自己在本地搭一个不需要验证的SMTP服务器。这就比较麻烦了,我是不想整,有兴趣的同学可以自己试试搭一个,用windows自带的IIS就可以,或者从网上下载其他的SMTP服务器软件,我就不多说。 结论:使用mail()函数发送邮件,就必须要有一台不需要验证的SMTP服务器。 这样的话配置工作会多一点,但是使用的时候就比较省事了,几行代码就可以。  二、使用SMTP邮件类 这种方法就比较常见了,尤其对于广大自己没有服务器,从网上购买虚拟主机的同学,第一种方法不现实,所以还是自己使用SMTP协议来发送邮件吧。不过要完成这项工作的话,就需要你对SMTP协议有一定的了解,喜欢事必躬亲的同学可以自己动手写一个,喜欢拿来主义的同学就可以从网上下载了,有很多,自己找吧。下面我举例说明一下在CodeIgniter里面如何使用它内置的邮件类发送邮件吧view plain
    $this->load->library('email');  
    $to = "[email protected]";  
    $subject = "test";  
    $message = "hello!";  
      
    $config["protocol"]     = "smtp";  
    $config["smtp_host"]    = "smtp.163.com";  
    $config["smtp_user"]    = "[email protected]";  
    $config["smtp_pass"]    = "password";  
    $config["mailtype"]     = "html";  
    $config["validate"]     = true;  
    $config["priority"]     = 3;  
    $config["crlf"]         = "/r/n";  
    $config["smtp_port"]    = 25;  
    $config["charset"]      = "utf-8";  
    $config["wordwrap"]     = TRUE;  
    $this->email->initialize($config);  
    $this->email->from('[email protected]', 'xxxx');  
    $this->email->to($to);       
    $this->email->subject($subject);  
    $this->email->message($message);   
    $this->email->send();  
     由于这些类都是高度封装的,所以使用起来也很简单。 结论:这种方式发送邮件无需装任何软件,但是需要你写更多的代码,而且要对SMTP比较熟悉。 但是如果你不自己写,而是直接使用别人写好的现成的代码的话,那这种方法无疑是最省事的: 不需要自己搭建SMTP服务器,也不需要写很多的代码。
      

  4.   

    mail — 发送邮件说明
    bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
    发送一封电子邮件。 参数to 
    电子邮件收件人,或收件人列表。 本字符串的格式必须符合 » RFC 2822。例如: [email protected] 
    [email protected][email protected] 
    User <[email protected]
    User <[email protected]>, Another User <[email protected]> subject 
    电子邮件的主题。 Caution 
    本项不能包含任何换行符,否则邮件可能无法正确发送。 message 
    所要发送的消息。 行之间必须以一个 LF(\n)分隔。每行不能超过 70 个字符。 Caution 
    (Windows 下)当 PHP 直接连接到 SMTP 服务器时,如果在一行开头发现一个句号,则会被删掉。要避免此问题,将单个句号替换成两个句号。 <?php
    $text = str_replace("\n.", "\n..", $text);
    ?> additional_headers(可选项) 
    String to be inserted at the end of the email header. This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). Note: When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini. Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. 
    Note: If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822. 
    additional_parameters (optional) 
    The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option. The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users. 注意:additional_headers非常重要,包括格式编码规定等很多重要信息都包括在里面