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 (or master.cf – thanks Mike Birch)
  • 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>rn" // 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!.

Useful bit, added by Terry Thorne in the comments:

I just thought I’d add for those looking to do this who find that their ISP blocks port 25 you have to route through their server. On Snow Leopard edit /etc/postfix/master.cf and add at the end:
mydomain = yourisp.net
myorigin = $mydomain
relayhost = mail.yourisp.net
Obviously replacing yourisp.net with the domain of your ISP (usually the suffix of your email address) and the mail.yourisp.net with your ISP’s smtp server addresss

Taken from:

http://www.mail-abuse.com/an_rteoutgoing.html

annoying apache apple internet leopard mac osx php postfix problem

code, internet, mac

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~!

Hans Nilsson

Thank you! Good stuff!

Lazylomoguy

Really nice post !
It worked !

Thanks !

tone

great tutorial..

to sixaxis:

got this error too

warning: service not found: biff/udp

try “myhostname = localhost” at main.cf

AlanH

Great stuff! I did hesitate over whether to put quotes around “sendmail -t -i”, but decided to do it. Worked like a charm :-)

Chris

I struggled the better half of this evening finding a solution to config sendmail for my php-cms installed locally, googled sendmail+every-possible-osx-related combination before i found your site googling for just php, mail, leopard.

Thx, dude!

Rasmus L. Knabe

Hi, first of thanks a lot!! – Searched google for help for an hour before i found this site – Much apriciated :)

I’m having some trouble thoug, and I think it’s related to the myhostname. Since I’m running my server loacaly on my Mac, and haven’t got af domain, I’m not shure what to put in stead of “host.domain.tld”?

The Shell gives me this error:

Mar 25 03:59:12 Rasmus postfix/error[17936]: E6C8F39B79FC: to=, relay=none, delay=1050, delays=1046/4.1/0/0, dsn=4.4.3, status=deferred (delivery temporarily suspended: Host or domain name not found. Name service error for name=Rasmus.local type=MX: Host not found, try again)

George

Awesome post, thanks! I still had problems after following your instructions but after reviewing /var/mail.log I tried using a different domain instead of my own one and voila – the mysterious “mail blockage” was cleared and mail(); worked once again.

Cheers again

Jeffrey Gardner

Nice. Been messing with this for days… do you have to start postfix after each restart? or will the daemon just start itself after the first time?

Jasper Tandy

I’m pretty sure that postfix starts with the OS so you shouldn’t need to do anything.

This article drastically needs updating. There are some issues with the sending domain and ip address resolves not matching, but I just don’t have the time right now to sort it out.

If anyone has any amendments to this, I would appreciate the input. I will update and credit anyone who points stuff out to me!

Robert Cox

Jasper Tandy, THANK YOU! I have looked around the web literally all day, paid $20 for MailServe (that I will never use), broke my php.ini file and in a single, simple easy to understand webpage, you got everything working. Awesome. Thank you thank you thank you thank you!

Macky Franklin

First of all, THANK YOU! Second, I wanted to let people know what I did because it seemed to work. I am doing local testing for a site I am developing and all I needed to do is call the PHP mail() function and check that it sent properly.
1. Edit the hostconfig file as mentioned above
2. Edit php.ini as mentioned above
3. Run “postfix start” from Terminal
4. Use mail()!
Basically, I skipped setting a domain because I do not need to receive mail, just send it. Worked like a charm.
Thanks again!

Andy Stratton

You’re my hero. Been fussing with this ever since the latest Leopard update, and drove me nuts since I develop all sites locally.

Thanks again, I owe you.

Fixing OS X (Leopard) sendmail issues – broken PHP mail()

[...] 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 [...]

Benny

Jesper Tandy, can you please contact me on the above email address. I need your help.

Matt Whisenhunt

Jasper,

Hero, Rockstar, Baller!? Thanks for the great post.

mike on dOPe

hey i take the following error >> postfix/master[559]: fatal: open lock file pid/master.pid: unable to set exclusive lock: Resource temporarily unavailable

any help ?

Jasper Tandy

Sounds like you’re not starting postfix as root. Are you running sudo postfix start?

Chris

I’m getting timeouts. I’m trying to send to gmail and yahoo addresses and they both timeout. I followed your steps except for setting myhostname. Do I need to do that if I just want to send out email (and not receive)? I read in a previous comment that they didn’t set their myhostname.

Thanks for any help.

Jasper Tandy

Have you checked your console when sending an email to see what happens? Google’s responses are normally pretty verbose and helpful.

Martin

I have just followed your steps and upon restarting my powerbook it now just loops through the startup function and never gets past the grey mac with spinning gear phase! What’s happening??? I can’t lose everything that have installed. I have been able to boot up in target mode and can access my hd from another machine with firewire but my machine just keeps rebooting itself. Please help!! Anyone!!

Jasper Tandy

Nothing you’re doing should cause that. Are you able to restore your postfix config and hostconfig when you boot in target mode? The changes you’ve made shouldn’t make a difference, but they seem to have. Just try to undo as much as you did during the process. I’ve never come across this problem, but I’ve never done this on a PPC processor, so something might have gone awry. If you figure it out, post back and let me know.

Robert Cox

Just upgraded to snow leopard and mail no longer sending locally with your directed settings. No errors, no email. HELP!

Jasper Tandy

There may not be errors, but there must still be something going through the mail log. I had a lot of trouble upgrading to SL as all my binaries had to be recompiled for the 64-bit OS. The mail log always gives pretty decent feedback on the status of mail sending, but if you’re getting nothing on that, then I’d recompile PHP under Snow Leopard and start again.

Bill Walker

Our contact form was being filled in for 5 months by potential customer and when they pressed ‘Submit’ they got our successfully submitted page while their message was sent to the dead email box in the sky. Thankfully we had a copy in our PHP database so we could apologize. All works now thanks to your help which was not easy to find. This should be linked by an Apple discussion form.

Jasper Tandy

Glad you found it useful. In my relatively limited experience with mail servers, I’ve found them to be flaky and unpredictable at best. I now run a mail class that saves all mail sent to a database table so you never lose anything. Little extra overhead for peace of mind.

Steve Cooper

Thank you, I’ve been searching for a workable solution for a while :)

Hannes Lilljequist

Haven’t read the article yet. Just wanted to thank you for the ingenious design of this page. Web design is dead, long live web design!

OK, I’ll start reading now.

Jasper Tandy

I can’t decide if you’re being sarcastic, Hannes, but I’m a developer with 0 design skills (except having read a typography book once) so I stick to what I’m good at!!

Hannes Lilljequist

No sarcasm, I found the it refreshing! Like an anti-conventional comment on current web design. Guess you conform a little more to conventions in your code, right?

Terry Thorne

Excellent tutorial – I just thought I’d add for those looking to do this who find that their ISP blocks port 25 you have to route through their server. On Snow Leopard edit /etc/postfix/master.cf and add at the end:
mydomain = yourisp.net
myorigin = $mydomain
relayhost = mail.yourisp.net
Obviously replacing yourisp.net with the domain of your ISP (usually the suffix of your email address) and the mail.yourisp.net with your ISP’s smtp server addresss

Taken from:
http://www.mail-abuse.com/an_rteoutgoing.html

Jasper Tandy

Awesome, thanks for that, Terry. Will tag that on at the end :)

Philip Ashlock

Using a Google Gmail account to send SMTP might simplify configuring SMTP settings (rather than dealing with your ISP and the many restrictions on port 25). I followed this guide to using google for SMTP with postfix on OSX – http://www.riverturn.com/blog/?p=239

Combined with the useful information in your post, that did the trick. This helped kickstart developing with the useful Tank Auth library I’m using with a Codeigniter project.

Thanks for the info!

Robert Cox

Okay, as instructed I looked through the mail.log file and found the issue I was having after OSX sl installation. That issue is has been resolved and is way to embarrassing to post. Point is that I didn’t think to check the log and doing so I was able to diagnose, so thank you.

Mike Birch

I’m on Snow Leopard and found that I needed to edit /etc/postfix/main.cf rather than edit /etc/postfix/master.cf

Jasper Tandy

I have master.cf and main.cf – probably a throwback from upgrading from Leopard to Snow Leopard. Updated the original post.

Micmath

Worked perfectly. Thanks.

Shawn Duncan

If you’re setting this up for a test server that really doesn’t have a domain, consider setting up Postfix to act as a client for your ISP’s email server:
http://postfix.state-of-mind.de/patrick.koetter/smtpauth/smtp_auth_mailservers.html

Shawn Duncan

This page is even clearer about setting up to use your isp’s mail, since it shows how to set the relay host setting and generic mapping:
http://www.postfix.org/SOHO_README.html#client_sasl_enable

Local testing on mac os x – php, mod_rewrite, and mail() | The Almanac

[...] to do was to be able to use the PHP function mail() to send emails from the local server. I found this awesome guide by Jasper Tandy. I followed it to a t and it worked perfectly. If you don’t know where your php.ini file is, [...]

Frank

Thx, made my day! :)