命令的形式
#!/usr/bin/php -q
<?
$email="[email protected]";
send_email($email);
function send_email($email){
$header="Content-type: text/html; charset=gb2312\r\n";
$header .= 'From:  <[email protected]>' . "\r\n";
$from="[email protected]";
$subject="给您发送信息";
$message="<html><body>";
$message .= "<br><br>你好<br><br>";
   
$message .="</body></html>";
mail($email,$subject,$message,$header) or die("Sorry,Failure");
}?>这样是可以发送命令的
使用
1.php
<?
$email="[email protected]";
send_email($email);
function send_email($email){
$header="Content-type: text/html; charset=gb2312\r\n";
$header .= 'From:  <[email protected]>' . "\r\n";
$from="[email protected]";
$subject="给您发送信息";
$message="<html><body>";
$message .= "<br><br>你好<br><br>";
   
$message .="</body></html>";
mail($email,$subject,$message,$header) or die("Sorry,Failure");
}?>这样的形式,通过浏览器打开则不能发送邮件。不知道为什么?

解决方案 »

  1.   

    我也不是很清楚原因.帮你顶了.我在PHP下面用的是 phpmailer .
      

  2.   

    权限问题?apache不能访问sendmail?
    猜的
      

  3.   

    谢谢。真的很急。还不知道原因,难道是配置有问题。
    但也不对啊。
    用命令的形式是可以发送邮件的。
    网页的形式则不行。
    我使用的是sendmail
      

  4.   

    那我想问一下。
    就是网页的形式运行php,它的使用的访问用户是什么?apache?还是此php的拥有者?就那我上面的程序来说。
    $ls -al
    ..............
    -rw-rw-r--   1 test test    401 Aug  6 08:42 mail_test_html.php
    -rwxr-xr-x   1 test  test   7408 Jun 14 07:01 mail_test.php
    .................这2个程序中发送邮件内容都是一样的 。只是mail_test.php 以命令的形式发送邮件。mail_test_html.php网页打开发送邮件。
    为什么 ./mail_test.php   能成功
    而 http://www.XXX.com/mail_test_html.php 就不能发送成功呢?
      

  5.   

    谢谢上面各位的回答,问题我已经解决了。我用的是RHEL4 
    关掉selinux 就可以了:)vi /etc/selinux/config
    找到
    SELINUX=enforcing
    改为
    SELINUX=disabled然后重启系统,问题就解决了.
      

  6.   

    对了,selinux是不允许apache调用mail.
    它们不是一个域的.
    不过简单的关掉,只是个偷懒的办法.
    selinux能够让linux的安全提高一个级别.好像是从c到b+
      

  7.   

    楼主用pear的mail类吧,很好用的