Ubuntu, Sendmail and PHP

Sur­pris­ingly, I hadn’t noticed until just yes­ter­day when I was test­ing out a con­tact form for a web­site under devel­op­ment that my PHP mail() wasn’t work­ing prop­erly. I really should have — Word­Press sends out mod­er­a­tion emails for com­ments, and the absence of those in test­ing should have made me think twice, but it didn’t.

So, PHP and Send­mail weren’t play­ing nice. The default com­mand is sendmail -t -i, if you don’t man­u­ally set sendmail_path in your php.ini con­fig­u­ra­tion, but this wasn’t working.

Unfor­tu­nately, due to some degree of stu­pid­ity in the way things are run, you need to use an absolute path for PHP to find where it is (even if just the com­mand “send­mail” works fine from a ter­mi­nal… like I said, stu­pid), so if you want mail, you have to play by its rules. For no appar­ent reason.

This is all with stock Ubuntu ver­sions of Apache2 and PHP, by the way — the stu­pid­ity may go away if you do-it-yourself, but I’m kind of doubt­ing it.

Any­way, assum­ing you’ve got send­mail (or an MTA which pro­vides send­mail hooks — I’m actu­ally using post­fix here) installed, you can sim­ply set this in your php.ini, restart Apache (using apache2ctl restart from a root account), and all should be working:

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

Bingo!

Tags: ,

posted on Monday, March 28th, 2005 at 11:37 am by Josh, filed under Geek, PHP.

16 Responses to “Ubuntu, Sendmail and PHP”

  1. Ronan says:

    Wow, that’s a really use­ful tip. I can’t believe one has to spec­ify a sta­tic path to send­mail within the php.ini. Mad.

    I’ve still got a few issues with localhost.localdomain on php mail, but this was very good to know.

  2. […] cribbed from here and here. Active Direc­tory Aging Baby Bass Birth­day Blogs Books Change Choices Cold […]

  3. John says:

    Just a lit­tle com­ment: PHP requir­ing full path to send­mail (actu­ally, any other binary) actu­ally makes per­fect sense:
    when run­ning send­mail from a ter­mi­nal the binary is found by look­ing into $PATH. PHP can’t count on that being defined for itself, thus it needs the full path to the binary (just as you would need to type if you hadn’t a prop­erly defined $PATH).

    Hope this clar­i­fies a lit­tle bit.

  4. pascal says:

    Won­der­ful,
    I used a full path to /usr/sbin/ssmtp, it worked with a com­mand shell but not with a script. It’s really stu­pid!
    thank you

  5. Matt says:

    Superb, thanks for that, that saved me hours of headache!

    Matt

  6. moata_u says:

    hello i have done the fol­low­ing :
    1.install send­mail and con­fig­ure /etc/mail/sendmail.mc :
    add com­ment to line DAEMON_OPTIONS(‘Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1′)dnl
    2.configure joomla mail server sit­ting = send­mail
    3.php.ini :
    sendmail_path = /usr/sbin/sendmail –i –t

    mail server only work­ing if i send email from local server , but when am try­ing send email from web­site via Inter­net that not work­ing !!!
    *** ANY HELP

  7. Josh says:

    Hi moata_u,

    When you say via Inter­net, do you mean it’s on another server? You’ll need to make sure your web host also has send­mail sup­port — many require you to use SMTP to another mailserver these days.

    Cheers,

    Josh

  8. steveo says:

    I have installed send­mail by:
    sudo apt-get install sendmail

    Then I changed the php.ini file:
    sendmail_path = /usr/sbin/sendmail –i –t

    Email still don’t send from my site

  9. Eric says:

    Thanks much for shar­ing! Worked like a charm.

  10. carlosalvet says:

    I have the same proble as steveo, I have post­fix and changed the php.ini and the return of mail() es success(1) like every­thing is work­ing ok. but the prob­lem is than the mail never arrive to my account, in /var/log/apache2/error.log isn’t problems.

    can some body help me? i don’t know where search the problem.

    Before Hand Thanks!

  11. Felipe says:

    I have the same prob­lem as steveo and carlosalvet .…

  12. Josh says:

    It’s pos­si­ble your ISP has some kind of port block­ing going on pre­vent­ing you from send­ing out­bound mail… do you know what their pol­icy is on this?

  13. Ben Sampson says:

    Finally got mail for PHP working…

    Thank you!

  14. memil says:

    Ben Samp­son! How did you solve? I would like to learn the CMS: Dru­pal and have a prob­lem with php sendmail.

    The XAMPP is installed
    THe send­mail is installed (as how described above)
    The php.ini was mod­i­fied (as how described above)

    I tried with a sim­ple php code:

    When I call this code with my browser, it waits a long time for local­host and finally I get the answer: “Mail Sent” , but the email doesn’t arrive.

    If any­body has the solu­tion, please let me know.

    Thanks!

    p.s.: I use Ubuntu 11.04 64bit

Leave a Reply