Solved

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

Posted on 2004-09-06
11
453 Views
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
<?php
$RetVal = mail("MyEmailAddress","Subject","Msg");
var_dump($RetVal);
?>

this works also
<?php
$RetVal = mail("root","Subject","Msg");
var_dump($RetVal);
?>

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

any ideas?
0
Comment
Question by:Xtreme-X
  • 5
  • 5
11 Comments
 
LVL 25

Expert Comment

by:Squinky
Comment Utility
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.
0
 
LVL 2

Author Comment

by:Xtreme-X
Comment Utility
-updatedb
-locate php.ini
/usr/local/Zend/etc/php.ini
/etc/php.ini
/etc/php.ini-zend_optimizer.bak

-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,
/etc/php.d/mysql.ini,
/etc/php.d/odbc.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
0
 
LVL 25

Expert Comment

by:Squinky
Comment Utility
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.
0
 
LVL 2

Author Comment

by:Xtreme-X
Comment Utility
Safe mode is off

The zend optimizer moved php.ini to
/usr/local/Zend/etc/php.ini

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.
0
 
LVL 25

Expert Comment

by:Squinky
Comment Utility
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).
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Author Comment

by:Xtreme-X
Comment Utility
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
<?php
$RetVal = mail("root","Subject","Msg");
var_dump($RetVal);
?>

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
0
 
LVL 25

Expert Comment

by:Squinky
Comment Utility
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.
0
 
LVL 2

Author Comment

by:Xtreme-X
Comment Utility
I dont know if this helps but if I change this line
LoadModule php4_module modules/libphp4.so

to

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
0
 
LVL 25

Accepted Solution

by:
Squinky earned 500 total points
Comment Utility
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).
0
 
LVL 2

Author Comment

by:Xtreme-X
Comment Utility
I Installed PHP5 and it works fine, I also used the mod_php and CLI setup as suggested.

Thanx..:D

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now