Home  >  Article  >  php教程  >  php中mail函数发送文件失败解决办法

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

WBOY
WBOYOriginal
2016-05-24 13:25:301465browse

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

之前的那台服务器转移过来后,发现网站用mail()发送邮件发不了,但是发现sendmail明明已经安装,如果没有安装sendmail可以执行,代码如下:

yum install sendmail

对了,主机名要设置一个域名格式的,例如:phprm.com,不然重启sendmail的时候会特别久才启动起来,否则要等很久,代码如下:

hostname phprm.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(&#39;yourEmail@lisizhang.com&#39;, &#39;邮件标题&#39;, &#39;测试邮件内容,如果收到此邮件,表示mail函数成功启用!&#39;); 
	if($send){ 
	    echo &#39;true&#39;; 
	}else{ 
	    echo &#39;false&#39;; 
	} 
	

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn