Friday, December 19, 2008

Local SMTP Server setup for PHP on Mac OS X

I've been coming up to speed with PHP 6 on a Mac OS. One of the challenges I ran into was utilizing PHP's mail() command to send emails to my gmail and yahoo accounts for testing. There are plenty of directions out there on configuring Postfix on Mac OS X (default smtp server on OS X since since 10.3, I believe) but you'll have to configure dns settings (enable reverse lookups, etc.) in order for most public smtp servers to accept your smtp servers' connections. Failing to do this will cause Postfix errors like - "connect to gmail-smtp-in.l.google.com[*.*.*.27]: Connection refused (port 25)" -.

To get around this I did the following:
1) enable Postfix utilizing the "Activating Postfix on OS X 10.4 (Tiger)" from David Reitter's web site (works on 10.5 too) -
Utilizing the setup he outlines will allow you to send to test smtp servers you utilize internally but most properly configured external smtp servers will reject your smtp servers connection as I mentioned earlier.

2) setup postfix to utilize gmail or other publicly available smtp servers (if you have a gmail account of course...) by following these steps:
- add the following lines in "/etc/postfix/main.cf" by using the following command:
sudo pico /etc/postfix/main.cf
Add These Lines:
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes


3) create the file "sasl_passwd" referenced in the main.cf you just edited using the following command:
sudo pico /etc/postfix/sasl_passwd
Add this line:
[smtp.gmail.com]:587 user.name@gmail.com:password (if your using gmail)

4) restrict file permissions with:
sudo chmod 400 /etc/postfix/sasl_passwd

5) convert the password file (sasl_passwd) into the correct format for postfix:
sudo postmap /etc/postfix/sasl_passwd

6) download a copy of "cacert.pem" that you trust and install into:
/etc/postfix/cacert.pem

7) start postfix:
sudo postfix start

send a test email from php using sendmail and it should work.

I've also used information from Christer Edwards on his Ubuntu Tutorials site.