简体   繁体   English

sendmail工作正常,但PHP mail()失败

[英]sendmail working but PHP mail() is failing

I cant seem to send an email using PHP's mail(). 我似乎无法使用PHP的邮件()来发送电子邮件。 I have also tried PHPMailer and Swiftmail with no success. 我也尝试了PHPMailer和Swiftmail,但没有成功。 However, the following command on the server delivers mail successfully. 但是,服务器上的以下命令成功传递邮件。

cat test.txt | mail -s "test mail" my@email.com 

Is there a way to trace where the problem is coming from? 有没有办法追踪问题的根源? mail() just seems to return true or false. mail()似乎返回true或false。

On PHP >= 5.2, there's error_get_last() to retreive whatever error was returned by the last function call. 在PHP> = 5.2上,有error_get_last()检索上一次函数调用返回的任何错误。 There's details on how to get equivalent information from older versions of PHP on the linked page. 在链接的页面上有有关如何从旧版本的PHP中获取等效信息的详细信息。

As for PHPMailer, there's the $mailer->ErrorInfo property which contains the last error occured. 至于PHPMailer,有$mailer->ErrorInfo属性,其中包含最近发生的错误。 SwiftMailer must have something similar. SwiftMailer必须具有类似的内容。 Most likely if mail's working from the command line but not from within PHP or the mailing libraries, there's a misconfiguration at play. 如果邮件是通过命令行工作的,而不是PHP或邮件库中的工作,则很可能是配置错误。 I'm guessing your host doesn't have the PHP sendmail_path ini parameter configured. 我猜您的主机没有配置PHP sendmail_path ini参数。

Enable all errors, warnings and notices with error_reporting(E_ALL). 使用error_reporting(E_ALL)启用所有错误,警告和通知。 Have the errors go somewhere useful or register a callback which does some useful stuff. 将错误放在有用的地方或注册一个执行一些有用的操作的回调。

Then you'll see what's happening. 然后,您会看到发生了什么事。 PHP error handling is useless by default (and its defaults are different everywhere so you have to override it in practice). PHP错误处理默认无用(和它的默认值是不同的无处不在,所以你必须覆盖它在实践中)。

In all likelihood it's configured to do SMTP to localhost, which your MTA does not allow relaying from. 它很可能已配置为对本地主机执行SMTP,您的MTA不允许从本地主机进行中继。 Change its config to use sendmail instead, or fix your MTA to allow relaying from localhost. 更改其配置以改为使用sendmail,或修复您的MTA以允许从本地主机进行中继。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM