How to Setup Postfix for Mac OSX and WordPress PHP Mail

I have been running a self hosted website off of my own computer for awhile now and wanted to use the Fast Secure Contact Form Plugin for WordPress. The problem was while the Apache web server was up and running along with PHP and MySQL to handle the back end for WordPress, there was no Mail Server for FS Contact Form to make use of.

Sendmail and Postfix are installed by default on a Mac OSX distribution although disabled by Apple. There are a variety of setup examples on the net; some useful, some not so. It took some tinkering, more than it should have in fact, so hopefully this process that I went through will help someone else.

Postfix will require some setup and one of the better instructions for doing that comes from Brett Hutley’s Blog. So armed with that I fired up the Terminal and went about editing ‘/etc/postfix/main.cf‘.

sudo nano /etc/postfix/main.cf

I use nano or pico (same thing) on occasion and am comfortable with them, but if you prefer emacs or vi go for it, but I mostly use the command line tool for TextWrangler named ‘edit‘. If you download and install TextWrangler.app (free and excellent text & code Editor with syntax coloring for the Mac) and install the command line tools with it, then ‘sudo edit filename‘ will open the file in TextWrangler so you can do your edits there. Way Cool.

I then put all of the following including comments for future reference at the End of /etc/postfix/main.cf:

So far so good, and after following Brett’s instructions for generating the sasl_passwd and setting permissions, I launched PostFix:

sudo postfix start

… and the Console.app mail.log was filled with missing directories in the /private/var/spool directory. I tried creating these as they appeared and then permission errors were encountered. Attempts to set owner and groups got a little farther, but no joy, until after more research I discovered the following:

Well, well. All necessary directories in place and properly configured and a test gave expected results.

telnet localhost 25

This showed access, but then the connection was dropped as my ISP blocks port 25 for SMTP.

date | mail -s 'test email' my@email.addy

This worked as expected and it was forwarded to my GMail account as the instructions in /etc/postfix/main.cf had dictated.

The next thing was to make this permanent between reboots and we need to:

We copy the file out of the System folder as we really shouldn’t be editing anything in the System Folder as System Updates can and will overwrite them.

Next we ‘ sudo edit /Library/LaunchDaemons/org.postfix.master.plist ‘ and add the following just before the closing ‘ /dict’ tag:

I don’t require Postfix to be running all the time; only when the Contact Form on my website is used so I chose these options. You can of course change either to ‘true’ depending upon your requirements. We can then start this up by either rebooting or using ‘launchd‘ with:

Back to WordPress and the FS Contact Form plugin. Test emails with that on my site were now giving errors and after more research the specific error was ‘Could not instantiate mail function’ and the Mail.log in Console.app was complaining about needing a -t switch for email Recipients or else Recipients must be issued from the command line. FS Contact form was passing information to sendmail correctly it seemed, and command line use was out of the question so more digging revealed PHP ‘mail’ needed one final change to ‘sudo edit /usr/local/php5/lib/php.ini‘.

Edit the php.ini file and search for ‘sendmail_path‘. Change the line that is there by adding the -t switch so that the final line looks like this:

sendmail_path = /usr/sbin/sendmail -t

Save the changes and now we need to restart the web server with:

sudo apachectl restart

And FINALLY … a test mail sent with the FS Contact Form plugin within WordPress running off of my web site hit gold!

I need a drink and a pill …

 

One Response to How to Setup Postfix for Mac OSX and WordPress PHP Mail

  1. Brain says:

    Saved, I enjoy your blog! 🙂