2015-03-07 OS X
Sooner or later you will reach the point where your application will have to send e-mails from your local environment. I will show you how to properly set MAMP and Postfix in OS X to be able to push messages using PHP’s mail function or using terminal command.
One of the further steps will require to provide credentials to your real and existing mailbox. For safety reasons, instead of providing details to you use daily, create new one with a distinguishable name like [email protected]_domain.tld.
Open MAMP PRO, turn off the server if it’s running right now and switch to the Postfix tab. Fill everything as you see on the screen below.
Now edit php.ini. Switch back to the Hosts tab and see which PHP version your virtual host is using. Remember the number and now go to:
/Applications/MAMP/bin/php/php_version_of_your_host_php/conf
Open php.ini and search for a phrase
smtp_port
Change the port number (smtp_port) to the one that is used by your hosting. In my case I had to set it to 587.
[mail function] ; For Win32 only. SMTP = localhost smtp_port = 587
Leave SMTP as it is.
Go to Terminal because now most of the time we’ll spend with command line (but don’t be scared). I can assure you, that the whole workflow is not so difficult. Open
sudo mcedit /etc/postfix/sasl_MAMP_passwd
and put credentials using following syntax:
<smtp_address>:<port_number> <sender_email_address>:<sender_email_password>
something similar to:
smtp.of_your_outgoing_server.com:587 [email protected]_domain.com:password123
Now, backup existing Postfix configuration file
sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
Open
sudo mcedit /etc/postfix/main.cf
and scroll down to the MAMP section.
Variable myorigin should be exactly the same as the domain of the sender e-mail address that you provided in sasl_MAMP_passwd file. Variable relayhost should match SMTP server and port from the same file as well.
### MAMP Postfix Configuration - Start ### myorigin = your_domain.com myhostname = mailer.$myorigin smtpd_sender_restrictions = permit_inet_interfaces # smart host relayhost = smtp.of_your_outgoing_server.com:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_MAMP_passwd smtp_sasl_security_options = noanonymous ### MAMP Postfix Configuration - End ### # DONT REMOVE: MAMP PRO main.cf template compatibility version: 1
Right now we have to reload configuration and (re)generate DB file:
sudo postmap hash:/etc/postfix/sasl_MAMP_passwd
and
sudo postfix start sudo postfix reload
Now restart MAMP and create a PHP script with below content. This trivial code will try to send an e-mail.
<?php mail('[email protected]_address.com', 'Hello world', 'Testing Postfix');
Save the file somewhere under your virtual host as test.php and open it in any browser.
http://env.local/test.php
Now switch back to terminal and type
date | mail -s test [email protected]_address.com
Now check your mail. If everything is done properly, you should receive two e-mails. One called “Hello world” and second called “test” that contains current date.
If you still have problems then watch mail logs by typing
tail -f /var/log/mail.log
You’re done.
See how flexible it is. Obviously the PHP mail function will work only when the server is running but using mail directly in terminal will work all the time. Thanks to that, you can create a bash script (triggered by the Cron) that can notify you for example when your backup is ready.