首页 > php函数 > php中mail函数发送文件失败解决办法

php中mail函数发送文件失败解决办法

php中mail函数是一个自带的发邮件的函数,但是如果你真的要使用mail函数来发邮件那必须是要给你系统安装sendmail组件才可以哦,下面我就碰到mail发送不了邮件的问题,下面一起来看看解决办法吧。

之前的那台服务器转移过来后,发现网站用mail()发送邮件发不了。但是发现sendmail明明已经安装。

如果没有安装sendmail可以执行

yum install sendmail

对了,主机名要设置一个域名格式的。例如:sangsir.com

不然重启sendmail的时候会特别久才启动起来。(否则要等很久)

 

hostname sangsir.com

接着看看sendmail是否正常运行

service sendmail status

如果没有运行可以启动。status换成start

紧接着,编辑php.ini。可以在web环境下创建一个phpinfo

因为我用的是lnmp。

vi /usr/local/php/etc/php.ini


再查找:

;sendmail_path =


按i编辑,替换为:

sendmail_path = /usr/sbin/sendmail -t -i

我的系统默认的是:

sendmail_path = /usr/sbin/sendmail -t -i -f

这个-f 看来是让mail函数停用了,去掉-f后mail函数工作恢复正常。


紧接着,重启PHP进程。

一般是:service php restart

LNMP是:service php-fpm restart

Apache下可以:service httpd restart


给大家分享个php脚本测试代码:

<?php
$send = mail('yourEmail@lisizhang.com', '邮件标题', '测试邮件内容,如果收到此邮件,表示mail函数成功启用!');
if ($send) {
    echo 'true';
} else {
    echo 'false';
}
?>

如果返回结果false说明邮件发送失败,如果返回true表示邮件发送成功!


文章地址:http://www.phprm.com/function/63837.html

转载随意^^请带上本文地址!

标签:none

发表留言