Viewed   154 times

I'm trying to figure out why the mail function in PHP fails when called via web browser (i.e. apache), but I can run the same script from the command line using

php -f mailtest.php

This is one of my client's Fedora servers, so I don't grok it completely, but I do have root access should I need to change anything.

from php.ini:

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

Not sure if this could matter, but /usr/sbin/sendmail is a symlink to /etc/alternatives/mta, which is a symlink back to /usr/sbin/sendmail.sendmail. FWIW the apache user does have permission to run sendmail (tested sendmail directly from the command line).

OS: Fedora Core 7 Linux (kernel 2.6.23.17)  
Apache: 2.2.8  
PHP: 5.2.6

Any help here will be greatly appreciated!

 Answers

3

I found the problem. SELinux was preventing apache from being able to use sendmail. To diagnose, I used

$ sestatus -b | grep sendmail  
httpd_can_sendmail                   off

Then to actually fix the problem:

$ restorecon /usr/sbin/sendmail
$ setsebool -P httpd_can_sendmail 1

Read more about it here.

Friday, October 14, 2022
 
eran.e
 
4

If both are installed, all you need to do is run the script using the relevant PHP binary.

So for example:

 // Runs using the PHP binary located at /usr/bin/php
 * * * * * root /usr/bin/php -n "/path/to/script.php"

or

 // Runs using the PHP binary located at /var/php5
 * * * * * root /var/php5 -n "/path/to/script.php"

All you need to know is the full file system path of the PHP CLI binaries, and call the relevant one to run your code.

Saturday, November 19, 2022
 
2

Try putting:

putenv("PYTHONIOENCODING=utf-8");

in the script before calling exec(). googler apparently requires the locale or this environment variable to be set.

Thursday, September 22, 2022
 
ohho
 
1

If this is a Red Hat-derived distribution (RHEL, CentOS, Fedora, ScientificLinux) running SELinux (or any non Red Hat derivative using SELinux), the default policy setting at time of this writing is to prohibit Apache from making external connections to other servers or databases. As root, you must enable the following two SELinux booleans. Use the -P option to persist the change across a reboot.

setsebool -P httpd_can_network_connect=1
setsebool -P httpd_can_network_connect_db=1

Note that httpd_can_network_connect may not be necessary. Try it first turning on only httpd_can_network_connect_db.

Monday, August 8, 2022
2

You can use mail:

$mail -s <subject> <recipients>

You then type your message and end it with a line that has only a period. This signals you are done and sends the message.

You can also pipe your email in from STDIN and it will be sent as the text of an email:

$<mail-generating-program> | mail -s <subject> <recipients>

One small note with this approach - unless your computer is connected to the internet and your DNS settings are set properly, you won't be able to receive replies to your message. For a more robust command-line program you can link to your POP or IMAP email account, check out either pine or mutt.

Tuesday, October 18, 2022
 
ljkyser
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :