sendmail working but PHP mail() is failing

对着背影说爱祢 提交于 2020-01-17 07:59:38

问题


I cant seem to send an email using PHP's mail(). I have also tried PHPMailer and Swiftmail with no success. 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.


回答1:


On PHP >= 5.2, there's error_get_last() to retreive whatever error was returned by the last function call. There's details on how to get equivalent information from older versions of PHP on the linked page.

As for PHPMailer, there's the $mailer->ErrorInfo property which contains the last error occured. SwiftMailer must have something similar. 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. I'm guessing your host doesn't have the PHP sendmail_path ini parameter configured.




回答2:


Enable all errors, warnings and notices with 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).

In all likelihood it's configured to do SMTP to localhost, which your MTA does not allow relaying from. Change its config to use sendmail instead, or fix your MTA to allow relaying from localhost.



来源:https://stackoverflow.com/questions/2819865/sendmail-working-but-php-mail-is-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!