PHP mail() works from command line but not apache

16,543

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.

Share:
16,543
matt
Author by

matt

Updated on June 12, 2022

Comments

  • matt
    matt almost 2 years

    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!

  • matt
    matt over 14 years
    All users have permission to execute sendmail. I confirmed by running sendmail as the apache user.
  • matt
    matt over 14 years
    nothing in the error log. PHP is being run as an apache module.
  • matt
    matt over 14 years
    also I've turned on all errors (i.e. error_reporting(E_ALL); ) and don't see any errors or warnings there either.
  • Josh
    Josh over 14 years
    Glad you figured it out. You should accept your own answer -- grants you rep, a badge, and lets others know the question is answered.
  • aurora
    aurora almost 13 years
    thanks, you led me in the right direction: i had a similar issue and after reading your post i got the idea that it might have something todo with my apparmor installation ... indeed, it was the problem.
  • Kalaschni
    Kalaschni over 10 years
    tell me why this is set to off by default??
  • elquimista
    elquimista over 8 years
    setsebool -P httpd_can_sendmail 1 didn't work for me. I tried with -V option and it worked like a charm.