PHP, mail() and OSX Leopard

So I couldn’t figure out any way of doing this, and I can’t stand stuff like this beating me so I’ve been messing about with it for the last hour and it’s finally working so I thought I’d share what I’ve done. Maybe this will get spidered, but mostly it’ll be here for my future reference!

The long and short of this situation is that OSX includes sendmail, but it seems to be some sort of alias for postfix, so you should actually be configuring postfix. Right now, I should probably clear up that I’m by no means an expert on any of this and it’s all been trial and error for me so far so if it doesn’t work for you, I can try and help but I can’t promise anything.

There are 4 files I used for the following:

  • /etc/hostconfig
  • /etc/postfix/main.cf
  • php.ini (this could be anywhere depending on your installation, mine’s in /usr/local/php5/lib/)
  • /var/log/mail.log

firstly, sudo nano -w /etc/hostconfig and add the following line:

MAILSERVER=-YES-

then sudo nano -w /etc/postfix/main.cf, find the myhostname variable (by default it’s host.domain.tld), uncomment it and change it to your domain (if you’re on a machine that doesn’t have a DNS, you can make it a domain that you’re responsible for so that it doesn’t get shut down at the receiving end, but please don’t make it google.com or something like that!)

now, open php.ini and look for the sendmail_path variable, uncomment it, make its value sendmail -t -i, save then restart apache. I’m not really sure if this is 100% necessary as there’s a comment above that says this is the default value anyway, but it can’t hurt!

now open a terminal window and execute the next couple of commands:

% sudo postfix start
% tail -f /var/log/mail.log

finally, create a file called mail.php (or whatever!) and add the following to it:

<?php
mail(
  'you@yourdomain.com', // your email address
  'Test', // email subject
  'This is an email', // email body
  "From: Me <me@mydomain.com>\r\n" // additional headers
);
?>

obviously replace you@yourdomain.com with your email address and me@mydomain.com with a valid email address (domain at least, as some mail servers will bounce your email if the sender’s domain isn’t real). Now navigate to your mail.php file (likely http://localhost/mail.php) and watch your terminal window to see that it’s been sent successfully. If it hasn’t, let me know if you fixed it and I’ll update this - it’s annoying to me that there isn’t really an answer to this question that I can find so the more comprehensive this is, the more helpful!.

annoying apache apple internet leopard mac osx php postfix problem

this many: 14! (that's loads)

Jansen:

Thank you very much for this page, i’d worked on that probleme for three days.

Have a nice day, carrying on your attitude.

Jasper Tandy:

no problem at all, glad you found it useful!

echohelloworld » Blog Archive » 033 030708 - Street Level:

[...] I witnessed how temperamental postfix can be, yet again. On our test server, postfix wasn’t starting and hadn’t been for around 35 days. There [...]

Miles:

This was hugely helpful. Thanks for making your notes public…

Jasper Tandy:

glad it helped!

Barry:

Hello, cheers very much for that - I’m astounded this isn’t more commonly available

Jasper Tandy:

I agree - the only good thing about it was that I got paid to figure out how to do it! Glad it was helpful!

sixAxis:

wow great tutorial..
i got this error message on terminal.. i don’t know if it is safe or not..

Jul 12 09:52:59 sixaxis postfix/local[9979]: warning: service not found: biff/udp

Are Rikardsen:

I followed these very useful instructions to make it work on my mac (see “Postfix on a null client”) : http://www.postfix.org/STANDARD_CONFIGURATION_README.html

mauricio:

HI,
Thanks for your tutorial. I tried really hard to make things work (it’s been at least 9 hours since I started) but I’m still unable to use the mail() function with Leopard. I wonder if anyone has any other suggestions.
THank you,

thehexman:

Hi,

I dont exactly have a website, but I am doing development on my mac for a soon-to-be website, and some of the code, when testing it, needs to use the mail() function. what domain could I use? (we dont have servers up yet, which is why im doing it on my mac)

Jasper Tandy:

thehexman: just use any domain in existence. In my experience of setting this up on testing servers, it doesn’t need to be the exact domain that you’re using on that server, it just has to be existing. I use echohelloworld.com for my local development server and it seems to work fine.

mauricio: try opening console and checking out mail.log (it’s under /var/log) - it gives quite decent messages as to why things aren’t working.

Fares Farhan:

You rocks! I’ve been dying to sort out why the mail() function doesn’t work at all on my mac.. great help.. thanks alot..

Tim Glenn:

Great help! Thanks~!