解决阿里云mail函数不能发送邮件
问题根源
其实对于新手来说,要在阿里云的主机上搭建个php环境是非常简单的,现在阿里云免费提供大量的第三方系统镜像,直接集成了基于apache或nginx的php、mysql环境。
在使用某php探针测试邮件功能时,发现无法发送,提示邮件发送失败。但是检测mail函数是正常。后来才知道这是阿里云的“问题”,因为阿里云提供的系统镜像都是绝对的精简版。系统中默认没有sendmail组件。所以解决方法就很简单的了,安装sendmail。
解决办法
我是新手,命令不熟,所以写的很详细,老鸟勿喷哦
1.重新安装 sendmail 组件,我用的是 CentOS ,使用下面的命令安装
yum install sendmail
安装完sendmail后还不能直接使用,还需要再设置一下hosts来解决sendmail启动慢和sendmail发送邮件慢的问题。
修改host文件
执行命令:
vi /etc/hosts
将第一行修改成如下内容
127.0.0.1 localhost localhost.localdomain 【#主机名】
2.使用下面的命令重启php-fpm进程
/etc/init.d/php-fpm restart
3.检测sendmail是否运行正常
/etc/init.d/sendmail status
如果显示正在运行running 就可以。
可能用到的命令
/etc/init.d/sendmail start (启动sendmail)
/etc/init.d/sendmail stop (关闭sendmail)
/etc/init.d/sendmail restart (重启sendmail)
4.配置php.ini,填写sendmail的绝对路径
使用命令打开编辑php.ini
vi /usr/local/php/etc/php.ini
输入?sendmail_path 查找定位(或者手动跳转找到sendmail_path),你会发现默认是下面的代码
;sendmail_path =
按 i 进入编辑,将这行修改为
sendmail_path = /usr/sbin/sendmail -t -i
按 Esc 键退出编辑,输入 :wq 保存退出
5.重启php-fpm进程
/etc/init.d/php-fpm restart
再次测试,成功了!
最活跃的读者