Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-09-06
Medium Priority
Last Modified: 2012-05-05
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?
Question by:Xtreme-X
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11997075
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.

Author Comment

ID: 11997365
-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
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11997821
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12003333
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.
LVL 25

Expert Comment

by:Marcus Bointon
ID: 12003924
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).

Author Comment

ID: 12004535
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
LVL 25

Expert Comment

by:Marcus Bointon
ID: 12004862
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.

Author Comment

ID: 12005314
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
LVL 25

Accepted Solution

Marcus Bointon earned 2000 total points
ID: 12005516
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).

Author Comment

ID: 12012304
I Installed PHP5 and it works fine, I also used the mod_php and CLI setup as suggested.



Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question