PHP mail() 函数返回 true,但不发送邮件

2022-01-17 00:00:00 email smtp sendmail php

我知道以前有人问过这个问题.

I know that this question was asked before.

当我调用 PHP mail() 函数时,它返回 true.

When I call the PHP mail() function, it returns true.

我检查了我的 php.ini(我正在运行 CentOS):

I checked my php.ini (I'v running CentOS):

SMTP = localhost
smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t -i
mail.add_x_header = On

我在一个论坛上看到我必须安装 sendmail.所以我安装了它.现在具有 mail() 功能的网站不再加载.于是我把sendmail去掉了,mail()函数又返回了true,但是没有发送邮件.

I read in a forum that I have to install sendmail. So I installed it. Now sites with a mail() function doesn't load anymore. So I removed sendmail, and the mail() function returns true again, but doesn't send the mail.

有什么想法吗?

推荐答案

要发送电子邮件,您需要一个 SMTP 服务器(本地或远程).实际上,您的邮件功能只是将邮件传递到您的 SMTP 服务器,而这才是真正发送您的电子邮件的功能.

To send an email you need a SMTP server (local or remote). Actually your mail function just passes the mail to your SMTP server and is this one which really send your email.

在你的 php.ini 中出现这一行

In your php.ini appears this line

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

如果您使用该配置参数(来自 manual):

You should be aware if you use that configuration parameter (from manual):

如果设置,smtp、smtp_port 和 sendmail_from 将被忽略,并且执行指定的命令.

If set, smtp, smtp_port and sendmail_from are ignored and the specified command is executed.

但这里最重要的是您只需卸载 sendmail,这样您就可以期待您的邮件无处可去.我知道 sendmail 给你带来了一些问题,可能是配置问题,但现在你的 php.ini 配置是错误的.

But the most important thing here is you just uninstall sendmail so you can expect your mail goes nowhere. I know sendmail was giving you some problems, possibly configuration problems, but now your php.ini configuration is wrong.

如何解决?

  • 开始从 php.ini 中删除 sendmail_path 参数.

安装一个简单配置的 SMTP 服务器,例如 postfix.

Install a simple to configure SMTP server like postfix.

验证后缀是否在端口 22 上侦听:

Verify postfix is listening at port 22:

netstat -lnt

netstat -lnt

  • 尝试从你的 php mail() 函数发送邮件

    • Try to send a mail from your php mail() function

      验证您的邮件是否已正确发送(检查您的 /var/log/mail.log 或 /var/log/mail/mail.log 文件)

      Verify your mail has been sent correctly (check your /var/log/mail.log or /var/log/mail/mail.log files)

      您也可以验证邮件不在后缀队列中:

      You also can verify the mail is not in the postfix queue:

      后队列-f

相关文章