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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23857539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:00:42  来源:igfitidea点击:

PHP mail() function returns true, but doesn't send mail

phpemailsmtpsendmail

提问by Florian

I know that this question was asked before.

我知道这个问题之前有人问过。

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

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

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

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

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

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.

我在一个论坛上读到我必须安装sendmail。所以我安装了它。现在不再加载带有 mail() 函数的站点。所以我删除了sendmail,mail() 函数再次返回true,但不发送邮件。

Any idea?

任何的想法?

采纳答案by Victor Henriquez

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.

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

In your php.ini appears this line

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

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

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

您应该知道是否使用该配置参数(来自手册):

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

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

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

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

How to solve it?

如何解决?

  • Start removing the sendmail_pathparameter from the php.ini.

  • Install a simple to configure SMTP server like postfix.

  • Verify postfix is listening at port 22:

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

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

  • 验证 postfix 正在侦听端口 22:

netstat -lnt

netstat -lnt

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

  • Verify your mail has been sent correctly (check your /var/log/mail.logor /var/log/mail/mail.logfiles)

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

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

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

  • 您还可以验证邮件不在 postfix 队列中:

postqueue -f

后队列 -f

回答by Irfan Kissa

i had a similar problem to this; both mail()and wp_mail()functions were returning TRUE, but no email was being sent to my [email protected]Email account.

我有一个类似的问题;既mail()wp_mail()功能正在返回TRUE,但没有电子邮件被发送到我的[email protected]电子邮件帐户。

It turns out that Yahoo was blocking these emails as spam. I did not have captcha implemented on my form, and therefore many spam emails were being sent to yahoo which is why they blocked the emails. Emails were sent successfully, but Yahoo was marking them as spam.

事实证明,雅虎将这些电子邮件作为垃圾邮件进行了拦截。我没有在表单上实现验证码,因此许多垃圾邮件被发送到雅虎,这就是他们阻止电子邮件的原因。电子邮件已成功发送,但雅虎将其标记为垃圾邮件。

make sure this is not the problem in your case.

确保这不是您的问题。

回答by Python

Please check your DNS, I get the same problem when test on localhost, but working on real host. The problem is fake DNS, your email was blocked by google. If you test with other email service (example: yahoo mail), you will receive as a spam mail. I found it after check mail log

请检查您的 DNS,我在 localhost 上测试时遇到同样的问题,但在真实主机上工作。问题是假 DNS,你的电子邮件被谷歌屏蔽了。如果您使用其他电子邮件服务(例如:雅虎邮件)进行测试,您将收到垃圾邮件。我在检查邮件日志后找到了它

回答by cdvv7788

I had issues setting smtp, so i ended up using gmail. You can send mail with PEAR MAIL as instructed here:

我在设置 smtp 时遇到问题,所以我最终使用了 gmail。您可以按照此处的说明使用 PEAR MAIL 发送邮件:

$from = "NoReply <[email protected]>";
$to = "someone <[email protected]>";
$subject = "my subject";
$body = "my body";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "[email protected]";
$password = "gmailpass";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);

If you use gmail, your $from will be replaced by the email address used to send it.

如果您使用 gmail,您的 $from 将被用于发送它的电子邮件地址替换。