views:

83

answers:

2

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" [email protected] 

Is there a way to trace where the problem is coming from? mail() just seems to return true or false.

A: 

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.

Marc B
A: 

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.

MarkR