Fix PHP’s mail() function after latest OS X (Leopard) update.

Jaspert Tandy just saved my life. Okay, maybe not, but he sure did make my day this morning. Why? He has a great post that gives you some simple steps to fix problems with sendmail/postfix on OS X (Leopard).

The problem:

I updated OS X and all of a sudden my local PHP installation isn’t sending mail. No errors, no warnings. Everything functions correctly but the mail never actually makes it to the inbox.

The solution:

Jasper outlines the the solution in his great post. I’m going to mark the steps I took after reading his post, but by all means give the man a thank you, a beer, a hug, or something!

Open terminal, you’ll need it:

  1. Turn on the mail server your hostconfig file:
    Run sudo nano -w /etc/hostconfig in Terminal. Look for the line starting MAILSERVER=, if you don’t see it, add it and make sure it reads:

    MAILSERVER=-YES-

    Terminal - Edit hostconfig

  2. Edit your posfix configuration file:
    First, I normally backup a configuration file before messing with it, since I’m no Terminal guru. If you want to take this precaution too, run this command in Terminal: sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.org

    Now, run sudo nano -w /etc/postfix/main.cf and find the lines defining the myhostname variable. Remove the leading number/pound sign (#) to uncomment the declaration and set it’s value to something you own, or something you can recognize.

    Terminal - Edit postfix config

    Note that if you set this to something obscure like I have, your email messages without from: headers will send from your Apache user (mine’s _www) at this domain. For instance, my emails send from _www@theandystratton.desktop.local. These tend to be blocked by Gmail’s spam filter (and probably most spam filters).

  3. Edit your php.ini just in case

    Open your php.ini file and uncomment the sendmail_path variable by deleting the leading semicolon (;).

    Set its value to sendmail -t -i so that line reads:

    sendmail_path = "sendmail -t -i"

    Restart Apache so the setting applies to you PHP install.

  4. Test your mail() script.

    Run a sample PHP script to see if you’re able to send mail. If you’d like to see what’s going on or get a confirmation from your log files that the email is working, check out Jasper’s tip using tail to show what’s happening with sendmail/postfix in Terminal in real time.

Did it work for you? Send Jasper some love and thank him. This is an aggregation of his solution and a journal of what helped me, in hopes that his solution can reach more people.

Thanks again, Jasper.

19 Comments

  1. Pooh says…

    Thanks a lot.

  2. andy says…

    No worries, again, this solution was adapted from Jasper Tandy’s, but I’m glad my aggregation of it helped you out, that was the goal!

  3. Rasmus L. Knabe says…

    Hi. apparently this doesn’t work for me.. I have previeusly set op me iMac send mail via PHP, but this stopped to work after i installed SN. I have now added the line:

    sendmail_path = “sendmail -t -i”

    to my php.ini.default file (I have no filed just called php.ini?).

  4. andy says…

    What is “SN”? Do you mean you upgraded to Snow Leopard? This post is in reference to the last update to Leopard. Snow Leopard would be an upgrade.

    Also, if you run <?php phpinfo(); ?> you should be able to see the path to the loaded configuration (php.ini) file on your system.

  5. Rasmus L. Knabe says…

    SN is Snow leopard yes, and your right about the upgrade/update :) (Not the server edition).

    The directory for my php.ini files is in /etc Acroding to – But the folder only contains php.ini.default (Wich is the file i have already edited).

  6. andy says…

    I’d run the following in terminal:

    sudo cp /etc/php.ini.default /etc/php.ini

    Then restart Apache. What’s probably happening is PHP is loading with its default compiled settings because it’s not finding php.ini, I don’t think it will pick up the settings in php.ini.default –

  7. Rasmus L. Knabe says…

    Sorry my bad.. it works just fine, but i forgot to forward port 25 from my router… Thanks for a great tutorial.. :)

  8. andy says…

    Ha, no worries, glad you’re up and running!

  9. Rasmus L. Knabe says…

    By the way.. Do you have any idea if this guide allso aplys to OS X Leopard Server (It wont work there iegther :( )

  10. Seth says…

    I’ve done this, but the e-mails aren’t going through.

    To make sure the problem isn’t PHP, I put the mail() in an if statement, and it passes.
    No errors appear in mail.log

    Suggestions?

  11. andy says…

    Not quite sure. This was mainly a re-post and modification of another post’s steps to fixing the issue. Are you on Leopard or Snow Leopard?

  12. Masuka says…

    Reallt Thanks… It work beatifully…..

  13. andy says…

    Of course! Be sure the thank @jaspertandy

  14. ZoRdAK says…

    Before reading this article, I tried many things and spent a lot of time on postfix/sendmail…

    But in 30second (time to read your article :p ) I success !!!

    Thank you a lot ;)

  15. Laith says…

    HELP!!! i keep getting connection refused when I check the

    tail -f /var/log/mail.log

    and I see “_www”

    what do I do?

    my form is a simple form generated by http://www.formmail-maker.com/generator.php

  16. andy says…

    _www is probably the user account of your local Apache server/PHP script attempting to send mail. I’m honestly far from a sysadmin and can’t support this. I wish I could, I mainly posted this as what I ended up doing based on some other posts I found, and what worked for me.

  17. Laith says…

    Do you know how i can change my user account of the local Apache server/PHP and or should i change this?

  18. Sendmail / Postfix broken since installing FiOS : Apache and PHP says…

    [...] I’ve had issues before with my jury rigged Macports-powered MAMP setup. The biggest time suck has been postfix/sendmail [...]

  19. Klederson says…

    Dude tks a lot i hate sendmail at Mac OS and never remember how to config it.

RSS feed for comments on this post. TrackBack URL

Leave a Comment

July 6, 2009

Filed in Development

There are 19 comments »


« Back to the Blog