• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

PHP mail() function works from bash prompt but not from apache

Php version 4.3.8
Apache version 2.0.50
Fedora C2

The function works from bash under the same credentials as apache
$RetVal = mail("MyEmailAddress","Subject","Msg");

this works also
$RetVal = mail("root","Subject","Msg");

however if I use apache it will always return false and sendmail will not be called ie...no logged events.

any ideas?
  • 5
  • 5
1 Solution
Marcus BointonCommented:
Could it be that your CLI/CGI and mod_php versions of PHP are using a different php.ini? Compare the output of phpinfo() through apache with the output from php -i on a command line. The ini file location and sendmail settings should be clearly visible.

Obligatory tip for anyone using mail(): don't. Use phpmailer instead: phpmailer.sourceforge.net.
Xtreme-XAuthor Commented:
-locate php.ini

-php -i | grep ini
<tr><td class="e">Configuration File (php.ini) Path </td><td class="v">/usr/local/Zend/etc/php.ini </td></tr>
<tr><td class="e">Scan this dir for additional .ini files </td><td class="v">/etc/php.d </td></tr>
<tr><td class="e">additional .ini files parsed </td><td class="v">/etc/php.d/ldap.ini,
<tr><td class="e">Supported handlers </td><td class="v">cdb cdb_make db4 inifile flatfile  </td></tr>

-php -i | grep sendmail
<tr><td class="e">sendmail_from</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr>
<tr><td class="e">sendmail_path</td><td class="v">/usr/sbin/sendmail -t -i</td><td class="v">/usr/sbin/sendmail -t -i</td></tr>
<tr><td class="e">Path to sendmail </td><td class="v">/usr/sbin/sendmail -t -i </td></tr>

They both match
Marcus BointonCommented:
OK. I notice that PHP is not seeing /etc/php.ini, which is the usual default location. You don't have safe mode on or anything?

One small observation - looks like you're using an old version of PHP; php -i has produced plain-text output for several versions now.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Xtreme-XAuthor Commented:
Safe mode is off

The zend optimizer moved php.ini to

and made a simlink
/etc/php.ini -> /usr/local/Zend/etc/php.ini

-php -v
PHP 4.3.8 (cgi) (built: Aug 27 2004 10:12:57)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
    with Zend Extension Manager v1.0.3, Copyright (c) 2003-2004, by Zend Technologies
    with Zend Optimizer v2.5.3, Copyright (c) 1998-2004, by Zend Technologies

http://www.php.net/ seems to show 4.3.9RC2 as the latest?

phpmailer looks pretty good and I will probably use it but I would still like to have the mail() function work.
Marcus BointonCommented:
Ah, I see you're using the CGI version rather than CLI or mod_php. Does the owner of the PHP CGI have sufficient permissions to access sendmail? It's quite normal to run it setuid, an I guess that may be overridden if you run a script locally as root?

Turn up your error reporting to E_ALL and see if there are any low-level messages that were not reported before.

4.3.8 is fine, you may just have CLI HTML output turned on in php.ini.

PHPMailer normally acts as a front-end for mail() (so you still need to fix this), but can use its SMTP class to work around problems with the built-in function (particularly on Windows where a local MTA is a rarity).
Xtreme-XAuthor Commented:
I temporarily gave the user 'www' bash logon

logged in and run the script from the prompt & it works
-php test.php
Content-type: text/html
X-Powered-By: PHP/4.3.8

Sending mail........bool(true)

test.php contains
$RetVal = mail("root","Subject","Msg");

in apache I have these settings
User www
Group www
DirectoryIndex index.php
AddType application/x-httpd-php .php
LoadModule php4_module modules/libphp4.so

httpd process
-ps aux | grep httpd
root      3375  0.0  2.9 16072 7460 ?        Ss   Sep07   0:02 /usr/sbin/httpd
www       4498  0.0  3.1 16208 7988 ?        S    04:05   0:00 /usr/sbin/httpd
www       4499  0.0  3.1 16352 8016 ?        S    04:05   0:00 /usr/sbin/httpd
www       4500  0.0  2.9 16208 7604 ?        S    04:05   0:00 /usr/sbin/httpd
www       4501  0.0  3.0 16208 7728 ?        S    04:05   0:00 /usr/sbin/httpd
www       4502  0.0  3.1 16208 7988 ?        S    04:05   0:00 /usr/sbin/httpd
www       4503  0.0  3.0 16208 7876 ?        S    04:05   0:00 /usr/sbin/httpd
www       4504  0.0  3.1 16204 7932 ?        S    04:05   0:00 /usr/sbin/httpd
www       4505  0.0  3.1 16204 7908 ?        S    04:05   0:00 /usr/sbin/httpd
www       6123  0.0  0.2  4072  592 pts/2    S+   19:01   0:00 grep httpd

when look at test.php through apache I get
Sending mail........bool(false)

I have the following settings in php.ini
error_reporting  =  E_ALL
display_errors = On
display_startup_errors = Off
log_errors = On
ignore_repeated_errors = Off
track_errors = Off

I get no additional warnings or messages
Marcus BointonCommented:
OK, so you're using mod_php, but you're also using PHP CGI rather than CLI?

I also just thought - The CGI version would be producing HTML output for php -i because it's expecting to be run as a CGI, not CLI.

Aside from that I really can't see anything wrong. Perhaps you should try recompiling PHP? It defaults to creating a CLI sapi as well as mod_php if you set --with-apxs in configure. You might need to delete the old CGI version manually.

It shouldn't really make any difference, but are you using real sendmail or another MTA with sendmail wrappers? I always use PHPMailer with IsQmail set, so it talks directly to qmail-inject rather than via a sendmail interface.
Xtreme-XAuthor Commented:
I dont know if this helps but if I change this line
LoadModule php4_module modules/libphp4.so


AddHandler cgi-script .php

and try to run it I get a 404 Access forbidden! Error.
shouldnt it work but through cgi?

I will also try a reinstall
Marcus BointonCommented:
You're likely to run into problems with ScriptAlias, which may prevent the CGI from running from other locations. I find it's much easier to use the mod_php and CLI setup - I run my own servers so the added tweakability of CGI is fairly unnecessary.

While you're trying reinstalls, you could give PHP5 a go, see if that helps. I find a recompile/install only takes a couple of minutes (config.nice is your friend), and you can switch between multiple versions by re-running make install (though stop apache before doing so or it will probably crash).
Xtreme-XAuthor Commented:
I Installed PHP5 and it works fine, I also used the mod_php and CLI setup as suggested.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now