Mal
Mal

Reputation: 41

Using PHP's mail() works from command line, not from Apache, any idea why?

I am using XAMPP on OS X (Yosemite), with PHP's standard mail() function to send through Sendmail (which OS X aliases for Postfix). I could send email other ways, like specifying an SMTP server in Postfix config files or using a PHP class, but wanted to get the basic mail() function working first - got lots of legacy websites that rely on it.

Both the command line and Apache's PHP run as the same user (tested with whoami through the exec() function, as well as creating files and looking at permissions). They both use the same php.ini file.

When I run the PHP script from the terminal, it pauses for a bit, I see it being processed in the mail log file, I get the email at the destination and the mail() function returns true.

When I run the script via Apache, it pauses for a bit, there is no hit on the mail logs, no email arrives, the function returns false and there are no errors anywhere... no matter how I alter the reporting level or look in various log files. It just seems to quietly fail.

Is there something stopping Apache from interacting with the mail server in the background? Like something in OS X? Or perhaps a bug in PHP? Or something unique to XAMPP? I tried upgrading to the latest version of XAMPP (which wiped all my config files and broke the DBs grrr) but had the same result. Any ideas?

Upvotes: 2

Views: 1494

Answers (2)

Mal
Mal

Reputation: 41

Okay, I found the solution, for whatever reason the sendmail path in php.ini has to look like this for PHP's mail to work through Apache on OS X Yosemite:

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

Thanks to everyone who tried to find an answer.

Upvotes: 2

Tivie
Tivie

Reputation: 18923

PHP called from your webserver can be configured very differently from your command line PHP as mod_php is a separately compiled module. It does not call the standard php command that you access through the terminal.

When using mod_php, requests/responses pass through apache.

There are a number of ways the setup can vary (and it most surely vary): - Different php.ini (make sure you are really using the same) - Directives set from .htaccess file - Environment variables - Apache configuration can (and does) change the behavior of php.

also...

OSX comes bundled with PHP. Make sure that, in the terminal, you are actually running calling the php.exe from XAMPP (and not the one bundled).


Debug:

  • Check your maillog and see what errors appear there (mail log should be at /var/log/maillog)
  • Check the var_dump of the result of the mail function:

    var_dump(mail('[email protected]', 'foo', 'msg'));

  • Check the sendmail path

Upvotes: 0

Related Questions