Solved

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

Posted on 2004-09-06
11
462 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: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.
0
 
LVL 2

Author Comment

by:Xtreme-X
ID: 11997365
-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: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.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 2

Author Comment

by:Xtreme-X
ID: 12003333
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: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).
0
 
LVL 2

Author Comment

by:Xtreme-X
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
<?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: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.
0
 
LVL 2

Author Comment

by:Xtreme-X
ID: 12005314
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:
Marcus Bointon earned 500 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).
0
 
LVL 2

Author Comment

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

Thanx..:D

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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