Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail() works from command line but not apache

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!

like image 310
matt Avatar asked Sep 22 '09 22:09

matt


People also ask

Why is my mail PHP not working?

If it's still not working: change the sender ($sender) to a local email (use the same email as used for recipient). Upload the modified php file and retry. Contact your provider if it still does not work. Tell your provider that the standard php "mail()" function returns TRUE, but not mail will be sent.

Does PHP mail function work on localhost?

But the PHP mail() function will not work in localhost. In this tutorial, we'll show how you can send email from localhost in PHP. Using this example script you can send email from any localhost server (XAMPP, WAMP, or any others) using PHP. We will use the PHPMailer library to send emails from localhost using PHP.

Does PHP mail function work?

Unless you mean delivering mail to the webmaster, it doesn't make sense for the webmaster to configure things. You're asking the wrong question - there is nothing wrong with the mail() function in PHP - it's not unreliable. The problem is contents and distribution of your mails.

What server does PHP mail use?

Php uses by default, the local mail-server. However you can specify this in your php. ini configuration file.


1 Answers

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.

like image 160
matt Avatar answered Sep 21 '22 16:09

matt